A
A
Anton2015-08-24 13:09:39
Kohana
Anton, 2015-08-24 13:09:39

Related products to the product, how to implement the search for related products?

Hello, I decided to finalize related products, the range is very large, about 2000 products, go to the product, then press the button add related products, then there is a list of products, a list of all products, put a tick on the product that will be attached to this product, the product will be related .
It is necessary to implement a search by the name of the goods, so that it is easier to find the goods.
Also implement pagination.
Display a photo of the product, display the category of the product. and the cost of the goods.
All this is in the products, photos, price, product category, everything is shown and working, there is also pagination, I want to implement all this in related products.
promotion of related products.

public function action_related()
{
$id = (int) $this->request->param('id');
$products = ORM::factory('product')->where('id','!=',$id)->order_by("sorts","ASC")->find_all();
$ralated = ORM::factory('related')->where('product_id','=',$id)->find_all()->as_array('id','rproduct_id');
$pArray = array();
foreach ($products as $product):
$pArray[$product->id]['prname'] = $product->prname;
if (in_array($product->id, $ralated)) 
{
$pArray[$product->id]['related'] = true;
}
else
{
$pArray[$product->id]['related'] = false;
}
endforeach;
$product = ORM::factory('product', $id);
if(!$product->loaded()) {
$this->request->redirect('admin/products');
}
$content = View::factory('admin/product/related', array(
'products' => $pArray,
'id' => $id,
'product' => $product,
));
        
$this->template->page_title = 'Сопутствующие товары';
$this->template->content = $content;	
}

there is also this code
public function action_setrelated()
{
$id = $_POST['data']['rproduct_id'];
    
$ralated = ORM::factory('related')->where('rproduct_id','=',$id)->find();
$item = ORM::factory('related', $ralated->id);
if($item->loaded())
{
$item->delete();
}
else
{
$data = $_POST['data'];
$products = ORM::factory('related');
$products->values($data);
$products->save();
}
die;
}

From the products, search by the name of the products, copied and added to the template of related products.
<div class="control-group" style="display:inline-block">
<label class="control-label"><strong>Поиск по названию товара</strong></label>
<form method=post action="/admin/products/related">
<input name=cat type=hidden value="<?=$id?>">
<input name=keyword type=text value='' style="width:350px" placeholder="Поиск по названию товара">
<input style="margin-top:-11px;" type='submit' value='Найти' class="btn btn-success" name="submit">
</form></div>

<form method=post action="/admin/products/"> я заменил на <form method=post action="/admin/products/related">

When entering related products, a link with the product id is generated. when we go into the product and click on add a related product in it, a link of this kind where all the products that can be attached to the product by clicking on the checkboxes.
site.ru/admin/products/related/9550
9550 - product id to which we attach related products.
So when searching, you need to add the id at the end of the search.
As I understand it, something like this I
enter the name of the product in the search and click search, nothing happens, I stay there on the page. and see all the goods.
Also tell me how to display pagination and product category and photo.
These codes are responsible for the output in the products.
product category output
<?php
if($product->category)
{									
foreach($category as $cat)
{

if($cat->id == $product->category){
?><a href="/admin/products/index/<?php echo $product->category ?>"><?php echo $cat->name ?></a><?
break;
} else {											
}
}
}							
?>

product photo output
<img src="/media/uploads/<?=isset($product->main_img->image) ? $product->main_img->image : 'noimage.jpg' ?>" style="width:100px">

output of the cost of goods
<?php echo Helper_Cenaot::get_cenaot_name($product->cenaot);?>

<?php if($product->price):?>
<strong>
<?php /*echo $product->price;*/ echo number_format($product->price, 0, ' ', '.')?>
</strong> <span>руб</span>
<?php else:?>
<div style="display:none"><p style="font-size:14px; margin: 0; padding: 0;" title="Цена Договорная">ДОГОВОРНАЯ</p></div>
<?php endif;?>

action for displaying products in the section all products
public function action_index() {
    
$id = (int) $this->request->param('id');
$settings_limit = $setting =ORM::factory('setting')->where('id', '=', '1')->find()->admin_per_page;
$limit = Session::instance()->get('limit', $settings_limit);
$category = ORM::factory('category')->order_by('id','ASC')->find_all();

if($category){
$categories = array();
foreach($category as $cat){
$categories[$cat->id] = $cat->name;
}
}
if(empty($id))
{
$count = ORM::factory('product')->count_all();
$pagination = Pagination::factory(array('total_items' => $count, 'items_per_page' => $settings_limit));
$products = ORM::factory('product')->limit($pagination->items_per_page)->offset($pagination->offset)->order_by("sorts", "DESC")->find_all();

if(isset($_POST['submit'])){
$data = Arr::extract($_POST, array('cat','keyword'));
$key = $data['keyword'];
if(!empty($data['cat'])){
$products = ORM::factory('product')->where('category','=',$data['cat'])->and_where('prname','LIKE',"%$key%")->order_by("sorts", "DESC")->find_all();
}else{
$products = ORM::factory('product')->where('prname','LIKE',"%$key%")->order_by("sorts", "DESC")->find_all();
}
$id = $data['cat'];
$pagination = "";
}

$content = View::factory('admin/product/v_product_index', array(
'products' => $products,
'pagination' => $pagination,
'category' => $category,
'categories' => $categories,
'id' => $id,
));
}
else
{
$selected_cat = ORM::factory('category', $id);
$this->_get_tree($selected_cat);
$products = DB::select()->from('products');
foreach ($this->_tree as $cat_id)
{
$products->or_where('category', '=', $cat_id);
}
$count = (count($products->execute()->as_array()));
$pagination = Pagination::factory(array('total_items' => $count, 'items_per_page' => $limit));
$products->limit($pagination->items_per_page)->offset($pagination->offset)->order_by("sorts", "DESC");
$products = $products->execute()->as_array();

$content = View::factory('admin/product/v_product_index_category', array(
'products' => $products,
'pagination' => $pagination,
'category' => $category,
'count' => $count,
'categories' => $categories,
'id' => $id,
));

}

$this->template->page_title = 'Товары';
$this->template->content = $content;	
}

Help me realize what I want to realize in related products, you can agree on a price for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Ivanov, 2015-08-24
@Writerim

Freelance for this question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question