将购物车复制到另一个购物车以更改 id_cart

Copy cart into another cart in order to change the id_cart

我想给购物车对象一个新的id,我有以下代码:

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,但没有真正替换或更改购物车对象。什么是最好的方法?

试试:

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_cart 应该更改为新创建的购物车的 id_cart