'How to retrieve the values of the checkboxes 'Available carriers' that we check

In order to reproduce a list of checkboxes like the one of the 'available carriers' in the Delivery section of the product page in the admin which allows to record the product-carrier associations, I ask for your help because I can't find how to retrieve the selected checkboxes to be able to use the setter of the association table ps_product_carrier defined in the product.php class

it would be in a JavaScript?

[Screen Admin Product page checkboxes list][1] [1]: https://i.stack.imgur.com/4C79g.png



Solution 1:[1]

To get the carriers you need to use the getCarriers() function from the product class.

If the list is empty, all carriers are available. Therefore, if you want to display them you will need to use the Carrier::getCarriers() function to retrieve the list

If the list contains data, then that product has the specific carriers set.

So working directly on the front won't be enough as the variable is not loaded by default in the templates. What I would do is to create a micro-module with just the function to get that data.

Then you can use either a standard hook or create your own to display the data exactly where you want.

An example could be:

In the TPL add a custom hook call:

{hook h='displayProductCarriers' product=$product}

Then in your micro module:

public function hookDisplayProductCarriers($params)
{
    if (isset($params['product'])) {
       // Depending on the PS version product will be an array or an instance
       if (is_array($params['product'])) {
          $p = new Product((int)$params['product']['id_product']);
       } elseif (is_object($params['product'])) {
           $p = $params['product']);
       }
       $carriers = $p->getCarriers();
       if (empty($carriers)) {
           // Product has no specific carrier assigned
           // Get all carriers here and assign them to smarty
           $carriers = Carrier::getCarriers();
       }
       $this->context->smarty->assign('product_carriers', $carriers);
       return $this->display(__FILE__, 'views/templates/hook/product-carriers.tpl');
    }
}

That will allow the usage of the variable inside the template product-carriers.tpl there you can iterate through the {$product_carriers} variable and display whatever you need.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pol Rué