New Module - Auto Related Products
Posted: Wed Dec 28, 2022 12:04 pm
@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 ???
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.',
],
];
}
}