[英]Prestashop 1.5 How to add products to cart from module
I just started learning prestashop module development and I'm curious about few things. 我刚刚开始学习prestashop模块开发,并且对几件事感到很好奇。
I'd like to create a module similar to t-shirt designer where users can select any t-shirt that they like from module and customize it. 我想创建一个类似于T恤设计器的模块,用户可以从该模块中选择自己喜欢的任何T恤并对其进行自定义。 Would it be possible to pass products to cart that do not exist in prestashop?
是否可以将产品传递到prestashop中不存在的购物车? I mean I need that customized products would be only visible in my t-shirt module and I don't want to add them manually via backend as products.
我的意思是我需要定制的产品仅在我的T恤模块中可见,并且我不想通过后端作为产品手动添加它们。 I just want to pass customized product to cart.
我只想将定制产品传递给购物车。 Are there any prestashop built-in functions to do so?
是否有任何prestashop内置函数可以这样做?
You can add the product into cart using the following below code: 您可以使用以下代码将产品添加到购物车:
$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.