Prestashop 将购物车复制到另一个购物车以更改购物车的id
我想给cart对象一个新id,我有以下代码:Prestashop 将购物车复制到另一个购物车以更改购物车的id,prestashop,Prestashop,我想给cart对象一个新id,我有以下代码: if ($payment->order_result->return->failures->failure == 'field.ordernumber.exists') { $dup = $this->context->cart->duplicate(); $this->context->cart->delete(); $this->context->ca
if ($payment->order_result->return->failures->failure == 'field.ordernumber.exists') {
$dup = $this->context->cart->duplicate();
$this->context->cart->delete();
$this->context->cart = new Cart($dup['cart']->id);
}
购物车没有被“替换”,我尝试了几种方法:使用$GLOBALS of global关键字,但没有任何东西真正替换或更改购物车对象。最好的方法是什么?尝试以下方法:
if ($payment->order_result->return->failures->failure == 'field.ordernumber.exists') {
$context = Context::getContext();
$cart_products = $context->cart->getProducts();
$this->context->cart->delete();
$newCart = new Cart();
if (!$context->cart->id) {
$guest = new Guest();
$context->cart->mobile_theme = $guest->mobile_theme;
$context->cart->add();
if ($context->cart->id)
$context->cookie->id_cart = (int)$context->cart->id;
}
foreach ($cart_products as $product) {
Db::getInstance()->insert('cart_product', array(
'id_product' => (int)$product->id,
'id_product_attribute' => (int)0,
'id_cart' => (int)$newCart->id,
'quantity' => (int)$product->quantity,
'date_add' => date('Y-m-d H:i:s')
));
}
}
受@ethercreation答案的启发,我更改了以下内容以解决我的问题: 来自
$dup = $this->context->cart->duplicate();
$this->context->cart->delete();
$this->context->cart = new Cart($dup['cart']->id);
至
$dup = $this->context->cart->duplicate();
$this->context->cart->delete();
$this->context->cookie->id_cart = $dup['cart']->id;
基本部分是
$this->context->cart
无法更改。需要复制购物车,然后应更改cookie中的当前购物车。因此,cookie$this->context->cookie->id\u cart
应更改为新创建购物车的id\u cart
我知道你在做什么。我从你的回答中得到了灵感。基本部分是$context->cookie->id\u cart=(int)$context->cart->id代码>。请关闭问题;)为了便于记录,我对我的问题给出了完整的答案。我得等到明天才能接受。