J'ajoute le produit au panier de mon module personnalisé en utilisant le code suivant, mais cela ne fonctionne pas. S'il vous plaît tout organisme aide à ajouter un produit au panier.Comment fonctionne le travail ajouter au panier fonction du module personnalisé dans prestashop 1.7
// fichier JS
$.ajax({
url : url+'&action=savecustomdataAction',
type : "POST",
data : { customdata : customdata, qty : 1, pid : 9 },
success : function(response) {
if(response.message == true) {
$('#addtocart_form').submit();
}
}
});
// Mon script tpl
<script>
var url ="{url entity='module' name='appcustomizer' controller='ajaxfunc' params = []}";
</script>
// Le code suivant sont dans mon fichier contrôleur
public function displayAjaxsavecustomdataAction() {
$customData = Tools::getValue('customdata');
$idProduct = Tools::getValue('pid'); // for me it's always one
$qty=Tools::getValue('qty'); // always add one item
$attribute = 1;
global $cookie;
$CustomOptions='';
$context = Context::getContext();
$temp = $this->context->cookie->__set('customoptions',$customData);
$cookie->write();
// get cart id if exists
if ($this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
}
// create new cart if needed
if (!isset($cart) OR !$cart->id)
{
$cart = new Cart($this->context->cookie->id_cart);
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$this->context->cookie->id_cart = (int)($cart->id);
}
// get product to add into cart
$productToAdd = new Product((int)($idProduct), true, (int)($this->context->cookie->id_lang));
$cart = $this->context->cart;
$updateQuantity = $cart->updateQty((int)($qty), (int)($idProduct),(int)($attribute),$customData, false);
$cart->update();
header('Content-Type: application/json');
die(Tools::jsonEncode(['message' => true]));
}