[英]WooCommerce custom cart item price on add-to cart via the URL
I have a simple product with a default price value of 100.我有一个简单的产品,默认价格值为 100。
As you choose a product it must go directly to checkout, this I handle simply like this:当你选择一个产品时,它必须直接去结账,我的处理方式是这样的:
/checkout/?add-to-cart=78
However, it must be possible to override the price of this product, as it's a voulenteer donation amount.但是,必须可以覆盖此产品的价格,因为它是志愿者捐赠金额。
I am trying this:我正在尝试这个:
/checkout/?add-to-cart=78&donation=200
And in my functions.php I have:在我的functions.php中,我有:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart ) {
$id = $cart['data']->get_id();
if ($id === 78 && $_GET['donation']) {
$price = $_GET['donation'];
}
else {
$price = $cart['data']->get_price();
}
$cart['data']->set_price( $price );
}
}
When the page is first loaded, it has set the price to 200. However, the JS doing an ajax call after reload to this url:当页面第一次加载时,它已将价格设置为 200。但是,JS 在重新加载到此 url 后执行 ajax 调用:
/?wc-ajax=update_order_review
Resets the price to 100.将价格重置为 100。
How can I stop it from resetting the price?我怎样才能阻止它重置价格?
You need to set the donation as custom cart item data on add-to-cart event, avoiding losing it when checkout get refreshed by ajax.您需要在 add-to-cart 事件中将捐赠设置为自定义购物车项目数据,避免在 ajax 刷新结帐时丢失它。
In the following code, we catch the donation value that we set as custom cart item data, then we add it to the corresponding cart item price.在以下代码中,我们捕获我们设置为自定义购物车项目数据的捐赠值,然后将其添加到相应的购物车项目价格中。
This happen via the url with something like: /checkout/?add-to-cart=78&donation=200
这通过 url 发生,例如:
/checkout/?add-to-cart=78&donation=200
add_filter( 'woocommerce_add_cart_item_data', 'catch_and_save_submited_donation', 10, 2 );
function catch_and_save_submited_donation( $cart_item_data, $product_id ){
if( isset($_REQUEST['donation']) ) {
// Get the WC_Product Object
$product = wc_get_product( $product_id );
// Get an set the product active price
$cart_item_data['active_price'] = (float) $product->get_price();
// Get the donation amount and set it
$cart_item_data['donation'] = (float) esc_attr( $_REQUEST['donation'] );
$cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique
}
return $cart_item_data;
}
add_action( 'woocommerce_before_calculate_totals', 'add_donation_to_item_price', 10, 1);
function add_donation_to_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $item ) {
// Add the donation to the product price
if ( isset( $item['donation']) && isset( $item['active_price']) ) {
$item['data']->set_price( $item['active_price'] + $item['donation'] );
}
}
}
Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.