Post by guiperalta » Thu Sep 16, 2021 10:39 pm

I want to display products related to the one being display on product/product page based on this products categories, instead of relying only on the manually linked related products. This is a great time saver specially on stores with a lot of different products in catalog. I found some paid extensions, some with really great features, but I need something simple and don´t want to mess too much with my core code as my store is already highly modified.

Here is my solution:
On catalog/controller/product/product.php, after line ~394:

Code: Select all

$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
Insert this chunk of code:

Code: Select all

########## INSERT RELATED PRODUCTS BASED ON PRODUCT CATEGORIES ===== BEGIN ##########
        //create an array to insert all new related products found on queries
        $all_related_products_from_categories = [];
        //set total quantity of related products - note that the manual related products
        //set on product admin page will be displayed first, so the total amount of products can be higher than this
        $total_products_to_show = 20;
        //find this products categories
        $product_categories = $this->model_catalog_product->getCategories($data['product_id']);
        //find each category´s products
        foreach($product_categories as $pc){
            //get category´s products
            $related_products_from_category = $this->model_catalog_product->getProducts(['filter_category_id' => $pc['category_id']]);
            //insrt into array
            foreach($related_products_from_category as $rpc){
                $all_related_products_from_categories[] = $rpc;
            }
        }
        
        //if the quantity of products found before is lower than the desired amount of related products, get random products to add
        if(count($all_related_products_from_categories) < $total_products_to_show){
            //find random products (needs improvement - right now it is only getting the first products - need to find a way to be really random)
            $random_products = $this->model_catalog_product->getProducts(['limit' => $total_products_to_show-count($all_related_products_from_categories)]);
            //merge with products from categories
            $auto_related_products = array_merge($all_related_products_from_categories, $random_products);
        } else {
            //if the desired amount is already met, just keep going
            $auto_related_products = $all_related_products_from_categories;
        }
        
        $count = 0;
        //foreach product found on previous queries
        foreach($auto_related_products as $arp){
            //break loop if reaches the desired amount
            if($count > $total_products_to_show){ break; }
            //check if product has stock and it is not the same as product on page
            if($arp['product_id'] == $this->request->get['product_id'] || $arp['quantity'] < 1){ continue; }
            //insert in $results array, which will be used at the related
            //products section of the page alongside the manually defined related products of this product
            $results[$arp['product_id']] = $arp;
            $count++;
        }
########## INSERT RELATED PRODUCTS BASED ON PRODUCT CATEGORIES ===== END ##########
Tested on OC 3.0.3.3 and 3.0.3.7. I know it needs a lot of improvements, please give any suggestions you have.

Newbie

Posts

Joined
Thu Mar 12, 2020 1:57 am

Post by straightlight » Thu Sep 16, 2021 10:56 pm

Wrong forum section. However, better to build an event for it than adding these codes into the catalog product controller file directly.

Besides, as this line already describes:
//find random products (needs improvement - right now it is only getting the first products - need to find a way to be really random)
It may not provide what you're specifically looking for since it will only be getting the first products. In addition, this variable does not exist:

Code: Select all

$total_products_to_show-count
nor should it use dashes with underscores. Underscores are the best practices with PHP.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by paulfeakins » Fri Sep 17, 2021 5:41 pm

If you can't find an extension, you could pay a developer such as ourselves or post a job in the Commercial Support Forum.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: ravikumar22, Semrush [Bot] and 84 guests