我创建了一个模块,在其中我需要获取或创建cart对象,以便将产品添加到购物车中。
在“添加到购物车”的操作中,如果上下文已经创建了购物车,我就调用这个购物车,当我添加产品时,购物车图标旁边的"1“会立即出现。
if ($this->context->cookie->id_cart){
$cart = $this->context->cart;
$cart->my_custom_field = Tools::getValue('svgTemplateResult'); // Here I add a value to a new field I made
$cart->update();
}
// Update the shopping cart
$cart->updateQty(1, $this->getProductId(), $id_product_attribute = null, $id_customization = false, $operator = 'up', $id_address_delivery = 0, $shop = null, $auto_add_cart_rule = true);

如果上下文中没有购物车,我需要像我在某个地方看到的那样创建它:
if ($cart->id == null){
$cart = new 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();
$cart->my_custom_field = Tools::getValue('svgTemplateResult'); // Here I add a value to a new field I made
$cart->update();
$this->context->cookie->id_cart = (int)($cart->id);
}
// Update the shopping cart
$cart->updateQty(1, $this->getProductId(), $id_product_attribute = null, $id_customization = false, $operator = 'up', $id_address_delivery = 0, $shop = null, $auto_add_cart_rule = true);但是"1“并不会立即出现在购物车图标旁边。我需要刷新页面才能看到它的出现。
如果我不刷新页面,但在“添加到购物车”按钮上单击另一个时间,它会立即刷新数字(因为购物车在上下文中,所以我们首先选择),并且我在购物车中有2倍的产品。
我能用这个做什么?
发布于 2022-10-06 16:55:37
您将需要使用JS调用刷新cart图标,只需在模块中检查/复制产品页面中的“添加到购物车”按钮时通常会发生什么。
您将看到对ps_shoppingcart前端控制器的ajax调用:
/module/ps_shoppingcart/ajax有效载荷是非常基本的:
id_customization: 0
id_product: {id_product}
id_product_attribute: {id_product_attribute}
action: add-to-cart否则,使用“纯”PHP,您唯一的选择是在每次修改购物车之后使用Tools::redirect()刷新页面。
发布于 2022-10-25 10:10:10
问题解决了,我只需在上下文中创建购物车(如果还没有),然后单击“Addto购物车”,例如在模块前端控制器的public function initContent()中。
https://stackoverflow.com/questions/73964284
复制相似问题