簡體   English   中英

Prestashop 1.5如何從模塊將產品添加到購物車

[英]Prestashop 1.5 How to add products to cart from module

我剛剛開始學習prestashop模塊開發,並且對幾件事感到很好奇。

我想創建一個類似於T恤設計器的模塊,用戶可以從該模塊中選擇自己喜歡的任何T恤並對其進行自定義。 是否可以將產品傳遞到prestashop中不存在的購物車? 我的意思是我需要定制的產品僅在我的T恤模塊中可見,並且我不想通過后端作為產品手動添加它們。 我只想將定制產品傳遞給購物車。 是否有任何prestashop內置函數可以這樣做?

您可以使用以下代碼將產品添加到購物車:

        $this->id_product = (int) Tools::getValue('id_product', null);
        $this->id_product_attribute = (int) Tools::getValue('id_product_attribute', Tools::getValue('ipa'));
        $this->customization_id = (int) Tools::getValue('id_customization');
        $this->qty = abs(Tools::getValue('qty', 1));
        $this->id_address_delivery = (int) Tools::getValue('id_address_delivery');

        if (!$this->context->cart->id) {
                if (Context::getContext()->cookie->id_guest) {
                    $guest = new Guest(Context::getContext()->cookie->id_guest);
                    $this->context->cart->mobile_theme = $guest->mobile_theme;
                }
                $this->context->cart->add();  //create cart if not available
                if ($this->context->cart->id) {
                    $this->context->cookie->id_cart = (int) $this->context->cart->id;
                }
            }

        $this->context->cart->updateQty(   //to add product into cart
              $this->qty,
              $this->id_product,
              $this->id_product_attribute,
              $this->customization_id,
              Tools::getValue('op', 'up'),
              $this->id_address_delivery
         );  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM