Magento - 将免费儿童产品添加到购物车

时间:2016-06-13 14:49:41

标签: php magento module cart observers

我一直试图将客户选择的产品(简单产品)添加到购物车,价格变为免费。目前我的工作除了两个要求外:

将儿童添加到购物车中的主要产品 该产品正在添加到购物车中,但作为附加产品而不是添加到主要产品的子产品。我已经尝试了各种方法,没有运气,这是当前的代码:

$product_gift->setFinalPrice( 0 );
$product_gift->setCustomPrice( 0 );
$product_gift->setPrice( 0 );
$product_gift->setOriginalPrice( 0 );
$product_gift->setPriceCalculation( 0 );
$product_gift->setSpecialPrice( 0 );

将价格变为零这根本没有用,但这是基于在线阅读的内容。

class Magestore_Promotionalgiftcustom_Model_Observer
{
    protected function _initProduct($productId)
    {
        if ($productId) 
        {
            $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
            if ($product->getId()) 
            {
                return $product;
            }
         }
        return false;
    }

    public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
    {
        if(!Mage::helper('promotionalgiftcustom')->enablePromotionalgiftcustom()){
            return;
        }

        $event = $observer->getEvent();  //Gets the event
        $product = $event->getProduct();

        $params = Mage::app()->getRequest()->getParams();
        $cart = Mage::getSingleton('checkout/cart');
        $added = array();

        if( Mage::getModel('promotionalgift/catalogrule')->validateItem( $product->getId() ) ){

            if(isset($params['gifts'])){

                foreach ($params['gifts'] as $gift) {     

                    if(!in_array($product->getId(), $added)) {
                        $product_gift = $this->_initProduct($gift);

                        if ($product_gift->getTypeId() == 'simple') {
                            if (!$product_gift->isSaleable()) {
                                continue;
                            }

                            $qty_4gift = 1;
                            $product_gift->setFinalPrice( 0 );
                            $product_gift->setCustomPrice( 0 );
                            $product_gift->setPrice( 0 );
                            $product_gift->setOriginalPrice( 0 );
                            $product_gift->setPriceCalculation( 0 );
                            $product_gift->setSpecialPrice( 0 );

                            $params = array(
                                'product' => $product_gift->getId(),
                                'qty' => $qty_4gift
                            );
                            $request = new Varien_Object();
                            $request->setData($params);
                            $cart->addProduct($product_gift, $request);
                            $cart->save();

                            Mage::getSingleton('checkout/session')->setCartWasUpdated(false);

                        }
                        $added = $product->getId();
                    }
                }
            }
        }
    }
}

除了上面强调的两点之外,观察者正在开展工作。以下是完整的观察者代码:

$product_gift->setPrice(0);
$product_gift->setFinalPrice(0);

我设法通过这两种方法将价格变为零:

Main Tabs -> Home (Mail) -> Add-ins

1 个答案:

答案 0 :(得分:1)

经过几个小时的测试和黑客攻击,我终于开始工作了。以下是任何感兴趣的人的解决方案:

protected function _initProduct( $productId ) {
    if ( $productId ) {
        $product = Mage::getModel( 'catalog/product' );
        $product->load( $productId );
        if ( $product->getId() ) {
            return $product;
        }
    }
    return false;
}

public function checkoutCartProductAddAfter( Varien_Event_Observer $observer ) {

    if ( ! Mage::helper( 'promotionalgiftcustom' )->enablePromotionalgiftcustom() ) {
        return;
    }

    $event   = $observer->getEvent();
    $product = $event->getProduct();
    $quote_item = $event->getQuoteItem();

    $params = Mage::app()->getRequest()->getParams();
    $cart   = Mage::getSingleton( 'checkout/cart' );

    if ( Mage::getModel( 'promotionalgift/catalogrule' )->validateItem( $product->getId() ) ) {

        $cart->save();

        if ( isset( $params['gifts'] ) ) {

            foreach ( $params['gifts'] as $gift ) {

                $product_gift = $this->_initProduct( $gift );

                if ( $product_gift->getTypeId() == 'simple' ) {
                    if ( ! $product_gift->isSaleable() ) {
                        continue;
                    }

                    $qty_4gift = 1;

                    $params  = array(
                        'product'           => $product_gift->getId(),
                        'qty'               => $qty_4gift,
                        'parent_product_id' => $product->getId()
                    );

                    $request = new Varien_Object();
                    $request->setData( $params );
                    $cart->addProduct( $product_gift, $request );
                }
            }

            $cart->save();

            Mage::getSingleton( 'checkout/session' )->setCartWasUpdated( false );
        }
    }

    if($quote_item->getOptionByCode('info_buyRequest')){
        $info_buyRequest = unserialize($quote_item->getOptionByCode('info_buyRequest')->getValue());
        if($info_buyRequest['parent_product_id']){
            $customPrice = 0;
            $quote_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);
            $quote_item->save();
        }
    }

}