我有一个带有自定义模块的php代码。如何从代码将产品添加到购物车。我有id_number(产品ID)和id_customer(客户ID)
<?php
$product id = 1167;
// how to proceed to add information to cart
需要将此产品(id_number)更新到我的购物车(已登录用户)
答案 0 :(得分:0)
我在模块中使用此代码段将产品添加到购物车,但是您可以在已覆盖的控制器中使用它:
public function add2cart(){
$id = 1167;
$qty = 1;
if($id!=''){
$this->createCart();
$this->context->cart->updateQty($qte, $id, null, '');
$this->context->cart->save();
$this->context->cookie->__set('id_cart', $this->context->cart->id);
echo 'Product added to cart!';
exit;
}
}
private function createCart()
{
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
if (is_null($this->context->cart->id_lang)) {
$this->context->cart->id_lang = $this->context->cookie->id_lang;
}
if (is_null($this->context->cart->id_currency)) {
$this->context->cart->id_currency = $this->context->cookie->id_currency;
}
if (is_null($this->context->cart->id_customer)) {
$this->context->cart->id_customer = $this->context->cookie->id_customer;
}
if (is_null($this->context->cart->id_guest)) {
if (empty($this->context->cookie->id_guest)){
$this->context->cookie->__set(
'id_guest',
Guest::getFromCustomer($this->context->cookie->id_customer)
);
}
$this->context->cart->id_guest = $this->context->cookie->id_guest;
}
if (is_null($this->context->cart->id)) {
$this->context->cart->add();
$this->context->cookie->__set('id_cart', $this->context->cart->id);
}
}
在我的initContent函数中,我调用函数add2cart():
$this->add2cart();