Well, going back to the basics in official documentation got me moving forward.
In case this can help someone spare some precious time, I'll list all steps here.
I created a module in root/modules with 2 files:
customajaxrequests > customajaxrequests.php
customajaxrequests > controllers > front > ajax.php
customajaxrequests.php content:
<?php if (!defined('_PS_VERSION_')) { exit; } class CustomAjaxRequests extends Module { public function __construct() { $this->name = 'customajaxrequests'; $this->tab = 'front_office_features'; $this->version = '1.0.0'; $this->author = 'Name'; $this->need_instance = 0; $this->ps_versions_compliancy = [ 'min' => '1.7', 'max' => _PS_VERSION_ ]; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Custom ajax requests'); $this->description = $this->l('Handle custom ajax requests.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); if (!Configuration::get('CUSTOMAJAXREQUESTS_NAME')) { $this->warning = $this->l('No name provided'); } } public function install() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } if (!parent::install()) { return false; } return true; } public function uninstall() { if (!parent::uninstall() || !Configuration::deleteByName('CUSTOMAJAXREQUESTS_NAME')) { return false; } return true; } }
ajax.php content:
<?php class CustomajaxrequestsAjaxModuleFrontController extends ModuleFrontController { public function initContent() { $this->ajax = true; parent::initContent(); } public function displayAjax() { $combinationId = (int)Tools::getValue('product_attribute_id'); $product = new Product((new Combination($combinationId))->id_product); $attibuteValues = $product->getAttributeCombinationsById($combinationId,$this->context->language->id); die(Tools::jsonEncode(array($attibuteValues))); } }
I installed the module in Prestashop Backend.
in themes/mytheme/templates/catalog/product.tpl I added:
<script> var ajaxRequestUrl = "{$link->getModuleLink('shapesajaxrequests', 'ajax', array())}"; </script>
in themes/mytheme/assets/js/custom.js I added:
window.addEventListener('load', function() { prestashop.on('updatedProduct',function(event) { $.ajax({ type: 'POST', url: ajaxRequestUrl, cache: false, data: { ajax: true, method: 'test', product_attribute_id: event.id_product_attribute }, success: function (result) { console.log(result); } }); }); });
The good news is that it seems to work quite fine as I can pass the current product id and the selected combination id to the controller to get some data back in console like:
[[{"id_product_attribute":"5","id_product":"2","reference":"","supplier_reference":"","location":"","ean13":"","isbn":"","upc":"","mpn":"","wholesale_price":"0.000000","price":"0.000000","ecotax":"0.000000","quantity":0,"weight":"0.000000","unit_price_impact":"0.000000","default_on":null,"minimal_quantity":"1","low_stock_threshold":null,"low_stock_alert":"0","available_date":"0000-00-00","id_shop":"1","id_attribute_group":"2","is_color_group":"0","group_name":"Longueur de tige","attribute_name":"20","id_attribute":"1","position":"0"},{"id_product_attribute":"5","id_product":"2","reference":"","supplier_reference":"","location":"","ean13":"","isbn":"","upc":"","mpn":"","wholesale_price":"0.000000","price":"0.000000","ecotax":"0.000000","quantity":0,"weight":"0.000000","unit_price_impact":"0.000000","default_on":null,"minimal_quantity":"1","low_stock_threshold":null,"low_stock_alert":"0","available_date":"0000-00-00","id_shop":"1","id_attribute_group":"1","is_color_group":"1","group_name":"Couleur","attribute_name":"Blue","id_attribute":"11","position":"1"}]]
But yes, there is still a but...
The bad news is that I did not manage to get the info I need from there yet as I need to get the combination original images.
So I have some more work to get done in public function displayAjax().
Any hint would be appreciated here.