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