购物车中的magento重复产品

时间:2015-06-11 14:02:27

标签: php magento magento-1.7

我想在购物车中复制产品。 因此,它是购物车中的新位置,具有前一个位置的所有选项。

  1. 我没有报价?
  2. 我总是出错:调用成员函数getStoreId()
  3. 这是我的测试代码:

    <?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);
        }
    }
    

    编辑: 我也尝试过:

      

    $ cart-&gt; addOrderItem($ newItem,1);

    然后我收到此错误:

      

    致命错误:未捕获的异常&#39; Mage_Core_Exception&#39;有消息&#39;请指定产品选项。&#39;在第603行的\ app \ Mage.php

    我如何复制所需的选项?

1 个答案:

答案 0 :(得分:1)

最后我开始工作了:

<?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();