将购买的分组产品添加到购物车时出现问题。
我需要为添加到购物车的所有产品设置自定义选项,同时将已分组的产品添加到购物车。
我上次尝试过的事情(取得了一些成功):
<checkout_cart_product_add_after>
<observers>
<customoptions>
<type>singleton</type>
<class>Company_CustomOptions_Model_Observer</class>
<method>addCustomOptionGroupSku</method>
</customoptions>
</observers>
</checkout_cart_product_add_after>
和
public function addCustomOptionGroupSku(Varien_Event_Observer $observer) {
$product = $observer->getProduct ();
if ($product->isGrouped ()) {
$quoteItem = $observer->getQuoteItem ();
$additionalOptions = array (
'options' => array (
'label' => 'GROUPSKU',
'value' => $product->getSku ()
)
);
$quoteItem->addOption ( new Varien_Object ( array (
'product' => $quoteItem->getProduct (),
'code' => 'additional_options',
'value' => serialize ( $additionalOptions )
) ) );
}
}
我创建了一个包含两个产品的分组产品。 但该代码仅将自定义选项“GROUPSKU”添加到购物车中的某个项目。另一个没有动过。
如何获取即将添加到购物车中的所有QuoteItem?
PS:我还将此问题添加到StackExchange的Magento部分:https://magento.stackexchange.com/questions/51883/add-custom-options-while-adding-grouped-product-to-cart
答案 0 :(得分:0)
我找到了一个不需要观察者的解决方案。
我不得不重写Mage_Sales_Model_Quote
。更具体的方法addProductAdvanced()
坏消息:您不能简单地使用parent::addProductAdvanced()
然后自己动手。您必须复制原始代码并填写代码。
以下是我所做的事情(请注意以/********
开头的评论):
public function addProductAdvanced(Mage_Catalog_Model_Product $product, $request = null, $processMode = null) {
if ($request === null) {
$request = 1;
}
if (is_numeric ( $request )) {
$request = new Varien_Object ( array (
'qty' => $request
) );
}
if (! ($request instanceof Varien_Object)) {
Mage::throwException ( Mage::helper ( 'sales' )->__ ( 'Invalid request for adding product to quote.' ) );
}
$cartCandidates = $product->getTypeInstance ( true )->prepareForCartAdvanced ( $request, $product, $processMode );
/**
* Error message
*/
if (is_string ( $cartCandidates )) {
return $cartCandidates;
}
/**
* If prepare process return one object
*/
if (! is_array ( $cartCandidates )) {
$cartCandidates = array (
$cartCandidates
);
}
$parentItem = null;
$errors = array ();
$items = array ();
foreach ( $cartCandidates as $candidate ) {
// Child items can be sticked together only within their parent
$stickWithinParent = $candidate->getParentProductId () ? $parentItem : null;
$candidate->setStickWithinParent ( $stickWithinParent );
$item = $this->_addCatalogProduct ( $candidate, $candidate->getCartQty () );
/******** own modification for custom option GROUPSKU*/
if($product->isGrouped()){
$additionalOptions = array (
'options' => array (
'label' => 'GROUPSKU',
'value' => $product->getSku ()
)
);
$item->addOption ( new Varien_Object ( array (
'product' => $item->getProduct (),
'code' => 'additional_options',
'value' => serialize ( $additionalOptions )
) ) );
}
/******** own modification end*/
if ($request->getResetCount () && ! $stickWithinParent && $item->getId () === $request->getId ()) {
$item->setData ( 'qty', 0 );
}
$items [] = $item;
/**
* As parent item we should always use the item of first added product
*/
if (! $parentItem) {
$parentItem = $item;
}
if ($parentItem && $candidate->getParentProductId ()) {
$item->setParentItem ( $parentItem );
}
/**
* We specify qty after we know about parent (for stock)
*/
$item->addQty ( $candidate->getCartQty () );
// collect errors instead of throwing first one
if ($item->getHasError ()) {
$message = $item->getMessage ();
if (! in_array ( $message, $errors )) { // filter duplicate messages
$errors [] = $message;
}
}
}
if (! empty ( $errors )) {
Mage::throwException ( implode ( "\n", $errors ) );
}
Mage::dispatchEvent ( 'sales_quote_product_add_after', array (
'items' => $items
) );
return $item;
}
这样,使用分组产品添加到购物车的所有产品现在都具有自定义选项。