Page 1 of 2

New Module - Auto Related Products

Posted: Wed Dec 28, 2022 12:04 pm
by tessthepup
@raiwa

After and enlightening response from Rainer I am working on a module for the product_info page based on the also_purchased_products and the query from the index_nested new products module.

The purpose of the module is for stores that have thousands of products (like the one I am working on) it would be unfeasible to manually relate all products using Rainers 'Related Products' addon which I have used before.

So automatation seems the way forward.

I have created the module and it installs ok but for some reason it does not show on the product_info page.

I am pretty sure it has something to do with the query maybe the $s or $d ???

Code: Select all

  class cm_pi_auto_related_products extends abstract_executable_module {

    const CONFIG_KEY_BASE = 'MODULE_CONTENT_PRODUCT_INFO_AUTO_RELATED_PRODUCTS_';

    public function __construct() {
      parent::__construct(__FILE__);
    }

    public function execute() {
      $auto_related_products_query = $GLOBALS['db']->query(sprintf(<<<'EOSQL'
 SELECT DISTINCT %s
 FROM products p
   LEFT JOIN specials s ON p.products_id = s.products_id
   INNER JOIN products_description pd ON p.products_id = pd.products_id
   INNER JOIN products_to_categories p2c ON p.products_id = p2c.products_id
   INNER JOIN categories c ON p2c.categories_id = c.categories_id
   LEFT JOIN (SELECT products_id, COUNT(*) AS attribute_count FROM products_attributes GROUP BY products_id) a ON p.products_id = a.products_id
 WHERE p.products_status = 1 AND c.parent_id = %d AND pd.language_id = %d
 ORDER BY rand()
 EOSQL
	          , Product::COLUMNS,
	            (int)$GLOBALS['current_category_id'],
	            (int)$_SESSION['languages_id'],
	            (int)MODULE_CONTENT_PRODUCT_INFO_AUTO_RELATED_PRODUCTS_CONTENT_LIMIT));

	        $auto_related_products = mysqli_num_rows($auto_related_products_query);
	        if ($auto_related_products > 0) {
	          $card = [
	            'show_buttons' => true,
        ];
}
      if (mysqli_num_rows($auto_related_products_query) > 0) {
        $tpl_data = [ 'group' => $this->group, 'file' => __FILE__ ];
        include 'includes/modules/content/cm_template.php';
      }
    }

    protected function get_parameters() {
      return [
        'MODULE_CONTENT_PRODUCT_INFO_AUTO_RELATED_PRODUCTS_STATUS' => [
          'title' => 'Enable Auto Related Products Module',
          'value' => 'True',
          'desc' => 'Should this module be shown on the product info page?',
          'set_func' => "Config::select_one(['True', 'False'], ",
        ],
        'MODULE_CONTENT_PRODUCT_INFO_AUTO_RELATED_PRODUCTS_CONTENT_WIDTH' => [
          'title' => 'Content Width',
          'value' => '12',
          'desc' => 'What width container should the content be shown in?',
          'set_func' => "Config::select_one(['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], ",
        ],
        'MODULE_CONTENT_PRODUCT_INFO_AUTO_RELATED_PRODUCTS_CONTENT_LIMIT' => [
          'title' => 'Number of Products',
          'value' => '4',
          'desc' => 'How many products (maximum) should be shown?',
        ],
        'MODULE_CONTENT_PRODUCT_INFO_AUTO_RELATED_PRODUCTS_SORT_ORDER' => [
          'title' => 'Sort Order',
          'value' => '120',
          'desc' => 'Sort order of display. Lowest is displayed first.',
        ],
      ];
    }

  }

Re: New Module - Auto Related Products

Posted: Wed Dec 28, 2022 1:20 pm
by tessthepup
ok after a cup of tea and a break I changed

Code: Select all

AND c.parent_id = %d
to

Code: Select all

AND p2c.categories_id = %d
and it worked.

Just need to solve why the max products display is not working

Re: New Module - Auto Related Products

Posted: Wed Dec 28, 2022 1:26 pm
by Kofod95
tessthepup wrote: Wed Dec 28, 2022 1:20 pm Just need to solve why the max products display is not working
I think you are missing LIMIT %d in the query

//Daniel

Re: New Module - Auto Related Products

Posted: Wed Dec 28, 2022 1:36 pm
by tessthepup
Kofod95 wrote: Wed Dec 28, 2022 1:26 pm
tessthepup wrote: Wed Dec 28, 2022 1:20 pm Just need to solve why the max products display is not working
I think you are missing LIMIT %d in the query

//Daniel
That was it, thank you Daniel

Re: New Module - Auto Related Products

Posted: Wed Dec 28, 2022 1:46 pm
by tessthepup
I have attached the files if anyone can find it useful.

Thanks to @raiwa and @Kofod95

Re: New Module - Auto Related Products

Posted: Tue Mar 21, 2023 7:04 am
by lecarlb
Has anyone got this to work on earlier versions of Phoenix? I'm attempting to install on 1.0.7.18

I can't get it to show on the product page. Admin is working.

Here's a screenshot of the error:
Ohio-State-Buckeyes-Party-Bowl-MVP-CO-by-Fremont-Die-B-Town-Sports.png
Thank you.

Re: New Module - Auto Related Products

Posted: Tue Mar 21, 2023 8:07 am
by ecartz
lecarlb wrote: Tue Mar 21, 2023 7:04 am I'm attempting to install on 1.0.7.18
In 1.0.7.18, you would have to go through and change all the $GLOBALS['db']->query to tep_db_query or get the database classes from 1.0.8.1 and load them.

Re: New Module - Auto Related Products

Posted: Tue Mar 21, 2023 6:02 pm
by tessthepup
@lecarlb

Did you manage to fix it?

Re: New Module - Auto Related Products

Posted: Thu Mar 23, 2023 1:09 am
by lecarlb
tessthepup wrote: Tue Mar 21, 2023 6:02 pm @lecarlb

Did you manage to fix it?
Not yet. Been a little busy. I will get to it in the next day and report back. Thanks you all for your speedy responses.

Re: New Module - Auto Related Products

Posted: Thu Mar 23, 2023 4:38 am
by lecarlb
ecartz wrote: Tue Mar 21, 2023 8:07 am
lecarlb wrote: Tue Mar 21, 2023 7:04 am I'm attempting to install on 1.0.7.18
In 1.0.7.18, you would have to go through and change all the $GLOBALS['db']->query to tep_db_query or get the database classes from 1.0.8.1 and load them.
Hello @ecartz I tried doing the changes and it didn't work. Where do I get 1.0.8.1 database classes and load them? Thanks