简体   繁体   English

如何在购物车中添加两个产品?点击添加到购物车按钮?

[英]How can i add two product in cart onlclick add to cart buton?

I want to add two product in cart when i click the add to cart button. 单击添加到购物车按钮时,我想在购物车中添加两个产品。

I have modified the code of 'Add to cart' button. 我已经修改了“添加到购物车”按钮的代码。 But it is adding only one product in cart. 但是它只在购物车中添加一种产品。

<button type="button" 
title="<?php echo $this->__('Add to Cart') ?>" 
class="button btn-cart" 
onclick="setLocation(
'<?php echo (string)Mage::helper('checkout/cart')->getAddUrl(
Mage::getModel('catalog/product')->load($Id1,$Id2))?>'
)">
<span><span>
<?php echo $this->__('Add to Cart') ?>
</span></span>
</button>

Only Id1 product is added in the cart.But i want id1 and id2 product both. 购物车中仅添加了ID1产品。但是我想同时使用ID1和ID2产品。

the function load() has two parameters: id and field. 函数load()有两个参数:id和field。

Mage::getModel('catalog/product')->load($Id1,$Id2)

this is not correct, because the second parameters is not interpreted as an ID. 这是不正确的,因为第二个参数未解释为ID。

You probably should make an observer on the 'checkout_cart_add_product_complete' event, but for answering your question you can simply modify your code to get it works: 您可能应该使“ checkout_cart_add_product_complete”事件成为观察者,但是要回答您的问题,您只需修改代码即可使其生效:

declare your _ids and the _addToCart urls: 声明您的_id和_addToCart网址:

<?php   
    $id1 = your_1st_id;
    $id2 = ypur_2nd_id;

    $_addCart1 = (string)Mage::helper('checkout/cart')
    ->getAddUrl(Mage::getModel('catalog/product')
    ->load($id1));

    $_addCart2 = (string)Mage::helper('checkout/cart')
    ->getAddUrl(Mage::getModel('catalog/product')
    ->load($id2));
?>

declare the button, you will need a js function to pop-out two links with one click: 声明按钮,您将需要一个js函数来一键弹出两个链接:

    <button type="button"
        title="<?php echo $this->__('Add to Cart') ?>"
        class="button btn-cart"
        onclick="pageloader();">
    <span><span><?php echo $this->__('Add to Cart') ?></span></span>
    </button>

and the js function can be like this: js函数可以像这样:

<script>
function pageloader()
{

    window.open("<?php echo $_addCart1; ?>", "_blank");
    window.open("<?php echo $_addCart2; ?>", '_blank');

}

</script>

all this code can be placed in a template.phtml file, but this in my opinion is not the correct way for doing what you are asking for. 所有这些代码都可以放置在template.phtml文件中,但是我认为这不是完成所要求的正确方法。 I recommend to create an Observer and elaborate your data there. 我建议创建一个观察者,并在那里详细说明您的数据。 Hope to solve your problems. 希望能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不刷新产品列表的情况下将产品添加到购物车? - how can i add products to the cart wihtout refreshing the product list? 我如何才能将Magento添加到购物车ajax推送正确的网址并将产品添加到购物车? - How can I get Magento add to cart ajax push the right url and add the product to cart? Magento | 我如何在自定义模块中的购物车中添加产品? - Magento | How i can add a product in shopping cart in a custom module? 如何将每个产品作为新商品添加到购物车 - How can I add every product as a new item into cart 我如何直接将特定产品添加到Wordpress中的持久购物车 - How can i directly add particular product to persistent cart in wordpress 如果我单击已在购物车中的产品的“添加到购物车”按钮,如何重定向到购物车页面 - How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart 如何将产品尺寸添加到购物车? - How to add product size to cart? 如何在购物车中添加产品说明 - How to add instruction with product in Cart 如何将产品再次添加到购物车或“添加到购物车”按钮不起作用? - How to add the product again to the cart or add to cart button not working? Paypal,添加到购物车-我可以同时销售产品和订阅吗? - Paypal, Add to Cart - Can I sell a product AND a subscription at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM