我正在使用core commerce extension
。我创建了该产品,并希望使用productID
以编程方式将产品添加到购物车。
答案 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);
}