douli4852 2016-09-29 06:46
浏览 47
已采纳

当magento购物车中存在其他产品时,自动将商品添加到购物车

I want to add item(one fix) to cart automatically when another product is present but quantity changes when main product quantity changed. Ex. if I add Product A with qty 1 in cart then product B with qty 1 will be add to cart same if I add Product A with qty 2 in cart then product B with qty 2. Please don't recommend any extension. Thanks

  • 写回答

1条回答 默认 最新

  • duancao2082 2016-09-29 08:41
    关注

    You need to override cart controller. Register your module, then in your modules config.xml

    <?xml version="1.0"?>
     <config>
       <modules>
         <Kreativ_Buynow>
           <version>1.0.0</version>
         </Kreativ_Buynow>
       </modules>
       <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Kreativ_Buynow before="Mage_Checkout">Kreativ_Buynow</Kreativ_Buynow>
                    </modules>
                 </args>
            </checkout>
        </routers>
       </frontend>
     </config>
    

    Then create CartController.php in controllers folder

    require_once 'Mage/Checkout/controllers/CartController.php';
    class Kreativ_Buynow_CartController extends Mage_Checkout_CartController{
     public function addAction(){ 
    
         foreach ($this->_getCart()->getQuote()->getAllItems() as $items) {
            if($items->getProductId() == 933){
                $value = $items->getQty();
                $prd = Mage::getModel('catalog/product')->load(943);
                $this->_getCart()->addProduct($prd,$value)->save();
                $this->_getSession()->setCartWasUpdated(true);
            }
         }
         return parent::addAction();
     }
    

    Where I assumed if product with id 933 is present, then you are going to add product with id 943 in the cart.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀