如何在prestashop 1.7中从自定义模块添加到购物车功能

时间:2017-06-07 11:59:55

标签: php prestashop cart prestashop-1.7

我使用以下代码将产品从我的自定义模块添加到购物车,但它无效。请任何身体帮助将产品添加到购物车。

// 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();
      }
    }
  });

//我的tpl脚本

    <script>
     var url ="{url entity='module' name='appcustomizer' controller='ajaxfunc' params = []}";
    </script>

//以下代码在我的控制器文件中

     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]));
      }

1 个答案:

答案 0 :(得分:1)

使用以下代码从自定义模块添加到购物车。它的工作

我的Javascript文件

var pid = $('#product_id').val();
$.ajax({
  url : url+'&action=savecustomdataAction',
  type : "POST",
  data : { customdata : customdata, qty : 1, pid : pid },

  success : function(response) {
    if(response.message == true) {
      $('#addtocart_form').submit();
    }
  }
});

我的.tpl文件

<script>
  var url ="{url entity='module' name='appcustomizer' controller='ajaxfunc' params = []}";
</script>
<form name="addtocart" id="addtocart_form" method="POST" action="{$urls.pages.cart}">
  <input type="hidden" name="id_product" id="product_id" value="{Tools::getValue('pid')}" />
  <input type="hidden" name="token" value="{$static_token}">

   // Your Code Here...

<input type="submit" id="addtocart" class="cart-btn" value="Add To Cart" /> 

我的控制器文件

 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 = 74; //enter the id_product_attribute

   // 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->update();
    $cart = $this->context->cart;
    $updateQuantity = $cart->updateQty((int)($qty), (int)($idProduct),(int)($attribute), false);

    $cart->update();
   header('Content-Type: application/json');
   die(Tools::jsonEncode(['message' => true]));
  }