当magento购物车中存在其他产品时,会自动将商品添加到购物车 [英] Add item to cart automatically when another product is present in magento cart
问题描述
我想在存在其他产品时自动将项目(一种解决方案)添加到购物车,但在主要产品数量发生变化时数量也会发生变化.前任.如果我将数量为1的产品A添加到购物车中,则数量为1的产品B也将添加到购物车中,如果我将数量为2的产品A添加至购物车,然后将数量为2的产品B添加到购物车中,请不要推荐任何扩展名.谢谢
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
推荐答案
您需要覆盖购物车控制器.注册您的模块,然后在您的模块中config.xml
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>
然后在controllers文件夹中创建CartController.php
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();
}
我假设如果存在ID为933的产品,那么您将在购物车中添加ID为943的产品.
Where I assumed if product with id 933 is present, then you are going to add product with id 943 in the cart.
这篇关于当magento购物车中存在其他产品时,会自动将商品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!