Php 不加载到购物车的选项
好的,我有一个模块可以工作。在模块中,我添加了多个产品,其中包含以下选项:Php 不加载到购物车的选项,php,magento,Php,Magento,好的,我有一个模块可以工作。在模块中,我添加了多个产品,其中包含以下选项: public function indexAction() { $prod_count = $this->getRequest()->getParam('prod_count'); $cart = Mage::getModel('checkout/cart'); $cart->init(); for($i = 1; $i <= $prod_count ;$i++
public function indexAction() {
$prod_count = $this->getRequest()->getParam('prod_count');
$cart = Mage::getModel('checkout/cart');
$cart->init();
for($i = 1; $i <= $prod_count ;$i++){
$prod_id = $this->getRequest()->getParam('prod_'.$i.'_id');
$prod_count = $this->getRequest()->getParam('product_'.$i.'_count');
$product = Mage::getModel('catalog/product')->load($prod_id);
$options = array('options' => NULL);
for($u = 1; $u <= $prod_count; $u++){
$op_id= $this->getRequest()->getParam('option_id_'.$u.'_'.$i);
$op_type_id = $this->getRequest()->getParam('option_type_id_'.$u.'_'.$i);
$options['options'][] = array( $op_id => $op_type_id);
}
$copy = array();
for($r = 0; $r < count($options['options']); $r++){
array_push($copy,$options['options'][$r]);
var_dump($copy);
}
echo $prod_id.'<br><br>';try {
$params = array(
'product' => $prod_id, // This would be $product->getId()
'qty' => 1,
'options' => $copy
);
$request = new Varien_Object();
$request->setData($params);
$cart->addProduct($product, $request);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->save();
}catch (Exception $ex) {
echo $ex->getMessage();
}
}
if ($this->getRequest()->isXmlHttpRequest()) {
exit('1');
}
$session= Mage::getSingleton('checkout/session');
$this->_redirect('checkout/cart');
}
public function indexAction(){
$prod_count=$this->getRequest()->getParam('prod_count');
$cart=Mage::getModel('checkout/cart');
$cart->init();
对于($i=1;$i getRequest()->getParam('prod'.$i'.'u id');
$prod_count=$this->getRequest()->getParam('product_'.$i.'u count');
$product=Mage::getModel('catalog/product')->load($prod_id);
$options=array('options'=>NULL);
对于($u=1;$u getRequest()->getParam('option_id.'$u.'$i');
$op_type_id=$this->getRequest()->getParam('option_type_id'.$u'.$i);
$options['options'][]=数组($op\u id=>$op\u type\u id);
}
$copy=array();
对于($r=0;$r
”;尝试{
$params=数组(
'product'=>$prod\u id,//这将是$product->getId()
“数量”=>1,
“选项”=>$copy
);
$request=新的Varien_对象();
$request->setData($params);
$cart->addProduct($product,$request);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->save();
}捕获(例外$ex){
echo$ex->getMessage();
}
}
如果($this->getRequest()->isXmlHttpRequest()){
退出(“1”);
}
$session=Mage::getSingleton('checkout/session');
$this->_重定向('checkout/cart');
}
但当我在我的网站上执行此操作时,它只会将没有选项的产品添加到商店中,是的,我确保检查是否有选项。有人知道原因吗?我认为您应该检查
$copy
变量的数据。参数的格式应为:
Array
(
[product] => 171 // product id
[options] => Array
(
[4] => 1111 // <option_id> => <selected value>
[3] => 7 // <option_id> => <selected value>
)
[qty] => 1
)
只需像这样传递$params
:
$cart->addProduct($product, $params);
妈的,你说得对。我发誓我花了时间确保$copy的格式正确。谢谢!
$cart->addProduct($product, $params);