【发布时间】:2022-10-25 18:10:10
【问题描述】:
我制作了一个模块,我需要在其中获取或创建 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 倍的产品在购物车。
我能用这个做什么?
【问题讨论】:
标签: php module prestashop cart