magento duplicate product in cart

1.6k views Asked by At

I want to duplicate a product in cart. So it is a new position in the cart with all options of the previous position.

  1. I doesn't get a quoteId?
  2. 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?

1

There are 1 answers

0
gebi84 On

finally i got it to work:

<?php
#_test/duplicate.php
require_once __DIR__.'/../app/Mage.php';
require_once __DIR__.'/../lib/Debug/Console.php';

Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));

/**
 * @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());

$quoteItem->setQuote($quote);
$quoteItem->setStoreId($storeId);

foreach ($options as $option) {
    $quoteItem->addOption($option);
}
$buyRequest = $quoteItem->getBuyRequest();

$cart->addProduct($product, $buyRequest);

$cart->save();