Magento 根据数量自动添加项目

发布于 2024-12-15 12:06:19 字数 257 浏览 2 评论 0原文

我正在设计一个带有按钮的自定义产品页面,单击该按钮时我需要发出一个带有“是”或“否”选项的警报。

如果选择“是”,我需要发生以下情况。

根据产品数量(即 1 和 1 之间)将另一个产品添加到购物车中。 2 项目在 3 和 3 之间添加产品 A 4 件产品 B,介于 5 和 5 之间12产品C等。

您知道实现此目标的最佳方法吗?

它必须是警报样式的弹出窗口(首选 ajax 弹出窗口),不能是产品页面上的复选框。

谢谢!

I'm designing a custom product page with a button that when clicked I need to have an alert come up with a "Yes" or "No" option.

If "Yes" is selected I then need the following to happen.

Add another product into the cart based on the products quantity i.e. between 1 & 2 items add product A between 3 & 4 Items product B, between 5 & 12 product C and so on.

Any idea of the best way to accomplish this?

It has to be a alert style popup (ajax popup preferred) cannot be a checkbox on the product page.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-12-22 12:06:19

所以我找到了解决问题的方法...我正在使用简单模态(TheBlackBenzKid 暗示我),我要么从自定义按钮调用,要么使用“添加到购物车”按钮调用。这反过来将重定向到一个 php 页面,该页面将重定向到购物车。对于 php 页面,我将只包含将商品放入购物车的代码,任何人都可以弄清楚如何根据自己的需要对其进行自定义。

<?php
// Include Magento application (URL to Mage.php)
require_once ( "app/Mage.php" );
umask(0);

//specified quantity my own variable I'm using for quantities
$spqty = 9;

// Initialize Magento
Mage::app("default");

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));


$session = Mage::getSingleton("customer/session");

// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');


if ($spqty <= 2) {

// insert item to cart where "98" is the product ID (NOT SKU!) Where "2" is the quantity
$cart->addProduct(98, array('qty' => 2));


} elseif ($spqty >= 4 ){

// you can add multiple products at the same time by adding this line multiple times
$cart->addProduct(96, array('qty' => 3));

}

// save the cart
$cart->save();

// very straightforward, set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


// redirect to index.php
header("Location: index.php/checkout/cart");

我还从这个人的博客中找到了一些信息,我将链接到文章

如何从外部站点将产品添加到 Magento

我很乐意回答任何有关此的问题...

So I've come across a solution to my problem... I'm using a Simple Modal (That TheBlackBenzKid hinted me to) that I'm either going to call from a custom button or with the add to cart button. This in turn will redirect to a php page that will redirect to the cart. For the php page I'll just include the code to put a item into the cart from there anyone could figure out how to customize it to there own needs.

<?php
// Include Magento application (URL to Mage.php)
require_once ( "app/Mage.php" );
umask(0);

//specified quantity my own variable I'm using for quantities
$spqty = 9;

// Initialize Magento
Mage::app("default");

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));


$session = Mage::getSingleton("customer/session");

// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');


if ($spqty <= 2) {

// insert item to cart where "98" is the product ID (NOT SKU!) Where "2" is the quantity
$cart->addProduct(98, array('qty' => 2));


} elseif ($spqty >= 4 ){

// you can add multiple products at the same time by adding this line multiple times
$cart->addProduct(96, array('qty' => 3));

}

// save the cart
$cart->save();

// very straightforward, set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


// redirect to index.php
header("Location: index.php/checkout/cart");

I also found some of this information from this guys blog I'll link to the article

How to add a product from an external site into Magento

I'm happy to answer any questions on this...

幸福还没到 2024-12-22 12:06:19

这不是最好的答案,但可以帮助您入门的代码:
您可以使购物车功能使用:

<input type="button" onClick="javascript:nValidateForm();"/>

和您的表单代码:

<form name="m2mform" id="m2mform" method="post" onSubmit="javascript:nValidateForm();">

然后只需在页面 XML 标头中调用外部 JavaScript 并将其添加到购物车,以便始终检查 JS 文件并验证弹出窗口。

This is not the best answer, but code to get you started:
You could make the cart function use:

<input type="button" onClick="javascript:nValidateForm();"/>

And your form code:

<form name="m2mform" id="m2mform" method="post" onSubmit="javascript:nValidateForm();">

And then just call an external JavaScript in your page XML headers and add it to cart so that JS file will always be checked and validate the popup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文