php Woocommerce:基于用户输入的自定义价格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21742630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce: custom price based on user input
提问by Bullyen
I did not want to post here but I could not find the answer I was looking for and I do not have enough reputation to comment on other VERY SIMILAR questions to get my exact answer.
我不想在这里发帖,但我找不到我正在寻找的答案,而且我没有足够的声誉来评论其他非常相似的问题以获得我的确切答案。
I found an almost perfect answer from this post: WooCommerce: Add product to cart with price override?
我从这篇文章中找到了一个几乎完美的答案:WooCommerce:使用价格覆盖将产品添加到购物车?
using the code:
使用代码:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price');
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
and it works great...if you set a hard coded custom price.
并且效果很好……如果您设置了硬编码的自定义价格。
My question is: How can I set a custom price based on user input?
我的问题是:如何根据用户输入设置自定义价格?
I have tried every way I can think of to pass information (I even tried using cookies, globals, sessions) and none of them worked how I wanted and all of them were, at BEST, hacks.
我已经尝试了所有我能想到的传递信息的方式(我什至尝试使用 cookie、全局变量、会话),但它们都没有按照我想要的方式工作,而且在 BEST,所有这些都是黑客。
The specific product in question is a donation and the customer wants the user to be able to set the donation price (rather than just creating a variable product with set price points).
有问题的特定产品是捐赠,客户希望用户能够设置捐赠价格(而不仅仅是创建具有固定价格点的可变产品)。
On the donation page when the user submits the donation form I am adding the donation product to the cart like so:
在用户提交捐赠表时的捐赠页面上,我将捐赠产品添加到购物车中,如下所示:
$donation_success = $woocommerce->cart->add_to_cart($donation_id);
My donation product has a set price of 0.00 so when it is added to the cart it has a price of 0.00 (I don't know if the price is set at this point or later)
我的捐赠产品的定价是 0.00,所以当它被添加到购物车时,它的价格是 0.00(我不知道这个价格是在此时还是之后设置)
I have tried passing in information at this point using the last variable in the add_to_cart method which accepts an array of arguments but I couldn't seem to get that to work either.
我已经尝试在此时使用 add_to_cart 方法中的最后一个变量传递信息,该方法接受一个参数数组,但我似乎也无法让它工作。
I am sure the answer is simple but I have been trying for hours to do this right and I cannot get it to work. I am out of ideas.
我相信答案很简单,但我已经尝试了几个小时来正确地做到这一点,但我无法让它发挥作用。我没有想法。
The actual code I am using at the moment is slightly different than was suggested by the answerer of the above post but works basically the same:
我目前使用的实际代码与上述帖子的回答者所建议的略有不同,但工作原理基本相同:
add_action( 'woocommerce_before_calculate_totals', 'woo_add_donation');
function woo_add_donation() {
global $woocommerce;
$donation = 10;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->id == 358 || $cart_item['data']->id == 360){
$cart_item['data']->set_price($donation);
}
}
}
Thanks in advance for any helpful advice!
在此先感谢您提供任何有用的建议!
采纳答案by Bullyen
I found a solution which is not elegant but works for my purposes.
我找到了一个不优雅但适合我的目的的解决方案。
I was trying to use cookies before but I didn't set the cookie path so it was only available to the page it was set on.
我之前尝试使用 cookie,但我没有设置 cookie 路径,因此它只能用于设置它的页面。
I am now setting a cookie with the donation price:
我现在正在设置一个带有捐赠价格的 cookie:
setcookie("donation", $_GET['donation'], 0, "/");
Then I am setting the price like so:
然后我像这样设置价格:
add_action( 'woocommerce_before_calculate_totals', 'woo_add_donation');
function woo_add_donation() {
global $woocommerce;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->id == 358 && ! empty($_COOKIE['donation'])){
$cart_item['data']->set_price($_COOKIE['donation']);
}
}
}
回答by batman42ca
I have been looking for exactly the same thing. I found this WooCommerce plugin (not free) for this
我一直在寻找完全相同的东西。我为此找到了这个 WooCommerce 插件(不是免费的)
Initially I wasn't sure what search terms to use to find plugins like this but it looks like "WooCommerce name your price" brings up links to other sources of similar plugins.
最初我不确定使用什么搜索词来查找这样的插件,但看起来“WooCommerce 说出你的价格”会提供指向其他类似插件来源的链接。
回答by Amin Darvand
[this is a comment] Where do you set the cookie? My first guess is that the refreshes in the same page, using the GET method, and provides a PHP code-block with the $_GET['donation'] to set the cookie with. And then, once the cookie is set, you redirect the page to Woocommerce Cart page to continue the shopping process. If you're doing it easier way, please let me know. Thanks.
[这是评论] 你在哪里设置cookie?我的第一个猜测是使用 GET 方法在同一页面中刷新,并提供了一个带有 $_GET['donation'] 的 PHP 代码块来设置 cookie。然后,设置 cookie 后,您将页面重定向到 Woocommerce Cart 页面以继续购物过程。如果您使用更简单的方法,请告诉我。谢谢。
Sorry, I couldn't post this as a comment to the selected answer due to the lack of points.
抱歉,由于缺少积分,我无法将此作为对所选答案的评论发布。

