如何使用API或在magento中以编程方式解决从wishlist到cart的多个项目addtocart。
以下是我的API代码,它适用于单个项目,但不能超过一个。
$customer_id = $this->getRequest()->getParam('customerId');
$itemCollection = Mage::getModel('wishlist/item')->getCollection()
->addCustomerIdFilter($customer_id);
//echo "<pre>";print_r($itemCollection->getData());die();
$cartData = array();
$customer = Mage::getModel('customer/customer')->load($customer_id);
$quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
if($itemCollection) {
foreach($itemCollection as $item) {
if($product_id != 0) {
if($item->getProduct()->getId() == $product_id) {
$qty = $item->getQty();
// Get customer session
$session = Mage::getSingleton('customer/session');
$product = Mage::getModel('catalog/product')->load($product_id);
$type = $product->getTypeId();
if($type == 'simple') {
try {
$quote->addProduct($product, $qty);
$quote->collectTotals()->save();
$success = 1;
$cartData['response'] = "Product Added To Cart Successfully!";
$item->delete(); break;
}
catch(Exception $ex) {
$success = 0;
$cartData['error'] = $ex->getMessage();
}
}
else {
$success = 0;
$cartData['error'] = "Sorry product not available....!!!";
}
}
else {
$success = 0;
$cartData['error'] = "Sorry product not available....!!!";
}
}
else {
//echo $item->getProduct()->getId();
//$qty = $item->getQty();
$successmsg = ''; $commma = ''; $errormsg = '';
// Get customer session
//$session = Mage::getSingleton('customer/session');
$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
//echo "<pre>";print_r($product->getData());
$type = $product->getTypeId();
if($type == 'simple') {
try {
$quote->addProduct($product,$item->getQty());
$quote->collectTotals()->save();
$success = 1;
$successPro[] = 1;
$item->delete();
}
catch(Exception $ex) {
$success = 0;
$errorPro[] = $ex->getMessage();
}
}
if(count($successPro) > 0) {
$successmsg = count($successPro)." Products Added into cart Successfully";
}
if(count($errorPro) > 0 && count($successPro) > 0) {
$commma = ' , ';
}
if(count($errorPro) > 0) {
$errormsg = count($errorPro)." Products not Added into cart.";
}
$cartData['response'] = $successmsg.$commma.$errormsg;
}
}
}
请帮我解决这个问题。