I want to duplicate a product in cart. So it is a new position in the cart with all options of the previous position.
- I doesn't get a quoteId?
- I get always an error: Call to a member function getStoreId()
Here my testcode:
<?php
#_test/duplicate.php
require_once __DIR__.'/../app/Mage.php';
Mage::app();
/**
* @var $quote Mage_Sales_Model_Quote
* @var $quoteItem Mage_Sales_Model_Quote_Item
* @var $newQuoteItem Mage_Sales_Model_Quote_Item
* @var $cart Mage_Checkout_Model_Cart
* @var $product Mage_Catalog_Model_Product
*/
$quoteItem = Mage::getModel('sales/quote_item')->load(356);
$cart = Mage::getSingleton('checkout/cart');
$storeId = Mage::app()->getStore()->getId();
$quote = Mage::getSingleton('checkout/session')->getQuote();
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->load($quoteItem->getProductId());
echo 'quoteId: '.$quote->getId().'<br />';
echo 'quoteItem: '.$quoteItem->getId().'<br />';
echo 'storeId: '.$storeId.'<br />';
echo 'productId: '.$product->getId().'<br />';
$options = Mage::getResourceModel('sales/quote_item_option_collection');
$options->addItemFilter($quoteItem->getId());
foreach ($options as $option) {
$quoteItem->addOption($option);
}
$buyRequest = $quoteItem->getBuyRequest();
//var_dump($buyRequest);
$quoteItem->setQuote($quote);
$quoteItem->setStoreId($storeId);
$newItem = clone $quoteItem;
//doesn*t work:
//Fatal error: Call to a member function getStoreId() on a non-object in \app\code\core\Mage\Sales\Model\Quote\Item\Abstract.php on line 65
$quote->addItem($newItem);
if ($quoteItem->getHasChildren()) {
foreach ($item->getChildren() as $child) {
$newChild = clone $child;
$newChild->setParentItem($newItem);
$quote->addItem($newChild);
}
}
edit: I also tried:
$cart->addOrderItem($newItem, 1);
Then I get this error:
Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Please specify product option(s).' in \app\Mage.php on line 603
How can i copy also the required options?
finally i got it to work: