在将产品添加到购物车之间切换两个引号之间

时间:2014-08-06 12:55:00

标签: magento magento-1.8

我想在将产品添加到引号之前在两个引号之间切换。为此,我在CartController中覆盖了addAction。

在行动中,如果我有任何

,我会取消引用ID
    $session = Mage::getSingleton('checkout/session');
    $quote_id = $session->getQuoteId();

    //unset the current quote id IsActive(0) and save
    $quote = Mage::getModel('sales/quote')->load($quote_id);
    $quote->setIsActive(0);
    $quote->save();

    Mage::getSingleton('checkout/session')->setQuoteId(null);

我在url中获得了一个额外的参数

    $params = $this->getRequest()->getParams();
    $free_home_trial = 0;
    if(isset($params['freehometrial'])){
        $free_home_trial = (int)$params['freehometrial'];
    }

由此我检查要加载的报价。

    if($free_home_trial){
        //check freetrial session Mage::getSingleton('checkout/session')->getFreeTrial()
        //if it is not null then load it
        $freetrial = Mage::getSingleton('core/session')->getFreeTrial();

        if(!is_null($freetrial)){
            $quote = Mage::getModel('sales/quote')->load($freetrial);
            $quote->setIsActive(1)->save();
            //$session->setData($quote);
            $session->setQuoteId($freetrial);
        }
     }else{
        //check for normal session
        //if it is not null then load it
        $normalsession = Mage::getSingleton('core/session')->getNormalCart();

        // var_dump($freetrial);die;
        if(!is_null($normalsession)){
            $quote = Mage::getModel('sales/quote')->load($normalsession);
            $quote->setIsActive(1)->save();
            $session->setQuoteId($normalsession);
        }
     }

$this->_getSession()->setCartWasUpdated(true);之后如果会话不存在,我将保存这些会话

   //Create session free or normal based on the url parameter

        if($free_home_trial){
            if(is_null(Mage::getSingleton('core/session')->getFreeTrial())){
                Mage::getSingleton('core/session')->setFreeTrial(Mage::getSingleton('checkout/session')->getQuoteId());
            }
        }else{
            if(is_null(Mage::getSingleton('core/session')->getNormalCart())){
                Mage::getSingleton('core/session')->setNormalCart(Mage::getSingleton('checkout/session')->getQuoteId());
            }
        }

我不知道为什么它对freeTrial和normal都使用相同的引用id,即使我在引用时将IsActive保存为0并在会话中将引号id设置为null。

提前致谢。

更多澄清

我想要两辆车,这就是为什么我想要两个报价并在它们之间切换的原因。当客户点击第一个添加到购物车,然后产品应添加到第一个购物车,当客户点击第二个添加到购物车,然后产品应添加到第二个购物车。为此,我只重写了CartController的添加动作。

1 个答案:

答案 0 :(得分:0)

您可以使用合并功能

切换购物车
$quoteA = Mage::getModel('sales/quote')->load('5');
$quoteB = Mage::getModel('sales/quote')->load('77');

$quoteB->merge($quoteA);

// Also?
$quoteB->collectTotals()->save();

Zend_Debug::dump($quoteB->getAllItems());