如何在Concrete 5中将产品添加到购物车

时间:2017-12-25 19:14:34

标签: concrete5

我正在使用core commerce extension。我创建了该产品,并希望使用productID以编程方式将产品添加到购物车。

1 个答案:

答案 0 :(得分:0)

这对我有用

    $pr = CoreCommerceProduct::getByID(10); //id of product
    $cart = CoreCommerceCurrentOrder::get();

    // add the product to the cart
    $pr = $cart->addProduct($pr, $qty);

    $eventResult = Events::fire('core_commerce_add_to_cart', $cart, $pr, $qty);

    // append attributes to order product
    $attribs = $pr->getProductConfigurableAttributes();
    foreach ($attribs as $at) {
        $at->saveAttributeForm($pr);
    }

    $pr->rescanOrderProductPricePaid();

    // decide wether to increment quantity or add additional line item
    if ($other = $cart->orderContainsOtherProduct($pr)) {
        $cart->removeProduct($pr);
        if (!$other->productIsPhysicalGood()) {
            $newQuant = 1;
        } else {
            $newQuant = $other->getQuantity() + $qty;
        }
        $other->setQuantity($newQuant);
    }