所以我有一个捆绑产品,其中包含6个产品。我已编写代码将此捆绑包添加到购物车,以编程方式将所有6个选项添加到捆绑包中...但我收到此Call to a member function getPosition() on a non-object
错误...它发生在shakeSelections
函数中,但它没有意义,因为所有的对象和东西都需要去,比如$a
和$b
需要是Mage_Catalog_Model_Product
数据类型,它们只是getOption方法不存在于其中......有什么我做错了吗?
这是我自定义控制器中的自定义addAction:
public function addAction() {
$prdId = $this->getRequest()->getParam('product');
$cart = Mage::getModel('checkout/cart');
$cart->init();
$_return = array('code' => 0, 'message' => 'Something went wrong...');
if (!empty($prdId)) {
$product = new Mage_Catalog_Model_Product();
$product->load($prdId);
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
$typeInstance = $product->getTypeInstance(true);
$optionCollection = $typeInstance->getOptionsCollection($product);
$selectionCollection = $typeInstance->getSelectionsCollection(
$typeInstance->getOptionsIds($product),
$product
);
$this->_options = $optionCollection->appendSelections($selectionCollection, false,
Mage::helper('catalog/product')->getSkipSaleableCheck()
);
$bundle_option = array();
foreach ($this->_options as $key => $value) {
foreach ($value['selections'] as $selKey => $selection) {
$bundle_option[$selection->option_id][] = $selection['selection_id'];
}
}
$params = array(
'product' => $prdId,
'related_product' => '',
'bundle_option' => $bundle_option,
'qty' => '1',
);
$cart->addProduct($product, $params);
$cart->save();
}
}
}
网址基本上是:/forty/cart/add/product/11
其中11是捆绑产品的ID。
答案 0 :(得分:4)
这里代码中的一些修改应该是可行的
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = $this->getRequest()->getParams();
$productId = $this->getRequest()->getParam('product');
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
if($product->getTypeId() == "bundle"){
$bundled_items = array();
$optionCollection = $product->getTypeInstance()->getOptionsCollection();
$selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
$options = $optionCollection->appendSelections($selectionCollection);
foreach($options as $option) {
$_selections = $option->getSelections();
foreach($_selections as $selection) {
$bundled_items[$option->getOptionId()][] = $selection->getSelectionId();
}
}
$params = array('bundle_option' => $bundled_items,'qty' => 1,'product'=>$productId);
}
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
$product = new Mage_Catalog_Model_Product();
$product->load($productId);
$cart->addProduct($product, $params);
$cart->save();
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
希望这对您有用。
如果我能为您提供更多帮助,请告诉我。