设置反映在 Woocommerce 3 订单上的折扣购物车商品价格
Set a discounted cart item price that is reflected on Orders in Woocommerce 3
我在 functions.php 中使用此代码对我的产品应用 10% 的折扣,从购物车中的第二个开始:
function add_discount_price_percent( $cart_object ) {
global $woocommerce;
$pdtcnt=0;
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
$pdtcnt++;
$oldprice = 0;
$newprice = 0;
if($pdtcnt>1) { // from second product
$oldprice = $cart_item['data']->price; //original product price
// echo "$oldprice<br />";
$newprice = $oldprice*0.9; //discounted price
$cart_item['data']->set_sale_price($newprice);
$cart_item['data']->set_price($newprice);
$cart_item['data']->set_regular_price($oldprice);
}
}
WC()->cart->calculate_totals();
}
add_action( 'woocommerce_before_cart', 'add_discount_price_percent', 1);
add_action( 'woocommerce_before_checkout_form', 'add_discount_price_percent', 99 );
价格在购物车和结帐页面中均正确显示,但是当我使用 PayPal 沙盒测试我的付款时,我看到并且必须支付全价,因为折扣被忽略了。
如果我在提交按钮之前回应折扣价,我会得到正确的价格:
function echo_discount_before_checkout_submit() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( WC()->cart->get_cart() as $key => $value ) {
echo $value['data']->price . "<br />";
}
}
add_action( 'woocommerce_review_order_before_submit', 'echo_discount_before_checkout_submit', 99 );
我怎样才能将正确的折扣价发送到 PayPal?
编辑:@LoisTheAtzec 的回复非常好,但如果数量超过 2,即使是第一件产品,我也需要获得 10% 的折扣:我正在尝试此代码,但我无法获得正确的值。
// If it is the first product and quantity is over 1
if ($count === 1 && $cart_item['quantity'] >= 2) {
// get unit price
$unit_price = $cart_item['data']->get_price();
// get quantity to discount (total - 1)
$discounted_quantity = $cart_item['quantity'] - 1;
// get total discount amount (on total quantity - 1)
$discounted_amount = ($unit_price * $discounted_quantity) * 0.9;
// add first non discounted price to total discount amount
$total_discounted_price = $unit_price + $discounted_amount;
// distribute discount over total quantity and get new unit price
$distributed_unit_discount = $total_discounted_price / $cart_item['quantity'];
// set new unit price
$cart_item['data']->set_price($distributed_unit_discount);
}
更新 09-06-2018
我在登录用户时遇到了一个奇怪的行为,这可能取决于插件之间的某些冲突或我使用的主题 (Avada):折扣应用了两次,所以我不得不阻止将此代码添加到我的函数中:
// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 999, 1);
function add_discount_percentage_on_2nd_item($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
希望对您有所帮助。
支付网关接受 "regular_price" 并且您已在其中传递旧价格。
尝试用新的价格变量设置regular_price,也许可以。
//update _regular_price
$wpdb->update(
$wpdb->postmeta,
array( 'meta_value' => $default_product_price ),
array( 'meta_key' => '_regular_price' )
);
或按照上述代码更新数据库中的价格。一定能帮到你。
In cart object the only thing that you can really change and has an effect is the active price.
Changing regular or sale price in cart items has no effect.
尝试以下将更改第二个购物车及以后购物车的价格,并且数据将正确传递到 Paypal:
// Calculate and save as custom cart item data the discounted price
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 3);
function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
// HERE set the percentage rate to be applied to get the new price
$percentage = 10; // 10%
$_product_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product($_product_id); // The WC_Product Object
$base_price = (float) $product->get_price(); // Get the product active price
// Save the calculated discounted price as custom cart item data
$cart_item_data['discounted_price'] = $base_price * ( 100 - $percentage ) / 100;
return $cart_item_data;
}
// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$count = 0;
// Loop through cart items
foreach($cart->get_cart() as $cart_item) {
$count++; // Increasing
// On 2nd cart item or more set the calculated discounted price
if ($count >= 2 && isset($cart_item['discounted_price']))
$cart_item['data']->set_price($cart_item['discounted_price']);
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
添加 - 如果购物车内容数量超过 2,则所有商品均可享受折扣。
您将使用与上面相同的第一个钩子函数代码。
您将用以下内容替换第二个挂钩函数:
// Set a discounted price on cart items when cart content count is over 2
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get the total items count
$total_count = $cart->get_cart_contents_count();
// if total count is below 2 we exit
if( $total_count < 2 )
return; // Exit
// Loop through cart items
foreach($cart->get_cart() as $cart_item) {
// Set the calculated discounted price
if (isset($cart_item['discounted_price']))
$cart_item['data']->set_price($cart_item['discounted_price']);
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。它应该有效。
加法 2
- 只有第一个购物车商品是全价,下一个数量是第一个商品的折扣)
- 所有 newt 购物车商品都打折。
代码:
// Calculate and save as custom cart item data the discounted price
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 3);
function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
// HERE set the percentage rate to be applied to get the new price
$percentage = 10; // 10%
$_product_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product($_product_id); // The WC_Product Object
$base_price = (float) $product->get_price(); // Get the product active price
// Save the normal active product price as custom cart item data
$cart_item_data['normal_price'] = $base_price;
// Save the calculated discounted price as custom cart item data
$cart_item_data['discounted_price'] = $base_price * ( 100 - $percentage ) / 100;
return $cart_item_data;
}
// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Initializing variables
$count = 0;
$first_item = true
// Loop through cart items
foreach($cart->get_cart() as $cart_item) {
// 1. First cart item
if ( isset($cart_item['discounted_price']) && isset($cart_item['normal_price']) && $first_item ){
if( $cart_item['quantity'] > 1 ){
$normal_price = (float) $cart_item['normal_price'];
$discount_price = (float) $cart_item['discounted_price'];
$quantity = (int) $cart_item['quantity'];
// The first item is at full price and others at discounted price
$cart_item['data']->set_price( $normal_price + ( $discount_price * ($quantity - 1) ) );
}
$first_item = false; // We switch it to false as it is the first cart item
}
// 2. All next items (at discounted price
elseif ( isset($cart_item['discounted_price']) && ! $first_item ){
// Set the discounted price
$cart_item['data']->set_price($cart_item['discounted_price']);
}
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。它应该有效。
我在 functions.php 中使用此代码对我的产品应用 10% 的折扣,从购物车中的第二个开始:
function add_discount_price_percent( $cart_object ) {
global $woocommerce;
$pdtcnt=0;
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
$pdtcnt++;
$oldprice = 0;
$newprice = 0;
if($pdtcnt>1) { // from second product
$oldprice = $cart_item['data']->price; //original product price
// echo "$oldprice<br />";
$newprice = $oldprice*0.9; //discounted price
$cart_item['data']->set_sale_price($newprice);
$cart_item['data']->set_price($newprice);
$cart_item['data']->set_regular_price($oldprice);
}
}
WC()->cart->calculate_totals();
}
add_action( 'woocommerce_before_cart', 'add_discount_price_percent', 1);
add_action( 'woocommerce_before_checkout_form', 'add_discount_price_percent', 99 );
价格在购物车和结帐页面中均正确显示,但是当我使用 PayPal 沙盒测试我的付款时,我看到并且必须支付全价,因为折扣被忽略了。
如果我在提交按钮之前回应折扣价,我会得到正确的价格:
function echo_discount_before_checkout_submit() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( WC()->cart->get_cart() as $key => $value ) {
echo $value['data']->price . "<br />";
}
}
add_action( 'woocommerce_review_order_before_submit', 'echo_discount_before_checkout_submit', 99 );
我怎样才能将正确的折扣价发送到 PayPal?
编辑:@LoisTheAtzec 的回复非常好,但如果数量超过 2,即使是第一件产品,我也需要获得 10% 的折扣:我正在尝试此代码,但我无法获得正确的值。
// If it is the first product and quantity is over 1
if ($count === 1 && $cart_item['quantity'] >= 2) {
// get unit price
$unit_price = $cart_item['data']->get_price();
// get quantity to discount (total - 1)
$discounted_quantity = $cart_item['quantity'] - 1;
// get total discount amount (on total quantity - 1)
$discounted_amount = ($unit_price * $discounted_quantity) * 0.9;
// add first non discounted price to total discount amount
$total_discounted_price = $unit_price + $discounted_amount;
// distribute discount over total quantity and get new unit price
$distributed_unit_discount = $total_discounted_price / $cart_item['quantity'];
// set new unit price
$cart_item['data']->set_price($distributed_unit_discount);
}
更新 09-06-2018
我在登录用户时遇到了一个奇怪的行为,这可能取决于插件之间的某些冲突或我使用的主题 (Avada):折扣应用了两次,所以我不得不阻止将此代码添加到我的函数中:
// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 999, 1);
function add_discount_percentage_on_2nd_item($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
希望对您有所帮助。
支付网关接受 "regular_price" 并且您已在其中传递旧价格。
尝试用新的价格变量设置regular_price,也许可以。
//update _regular_price
$wpdb->update(
$wpdb->postmeta,
array( 'meta_value' => $default_product_price ),
array( 'meta_key' => '_regular_price' )
);
或按照上述代码更新数据库中的价格。一定能帮到你。
In cart object the only thing that you can really change and has an effect is the active price.
Changing regular or sale price in cart items has no effect.
尝试以下将更改第二个购物车及以后购物车的价格,并且数据将正确传递到 Paypal:
// Calculate and save as custom cart item data the discounted price
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 3);
function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
// HERE set the percentage rate to be applied to get the new price
$percentage = 10; // 10%
$_product_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product($_product_id); // The WC_Product Object
$base_price = (float) $product->get_price(); // Get the product active price
// Save the calculated discounted price as custom cart item data
$cart_item_data['discounted_price'] = $base_price * ( 100 - $percentage ) / 100;
return $cart_item_data;
}
// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$count = 0;
// Loop through cart items
foreach($cart->get_cart() as $cart_item) {
$count++; // Increasing
// On 2nd cart item or more set the calculated discounted price
if ($count >= 2 && isset($cart_item['discounted_price']))
$cart_item['data']->set_price($cart_item['discounted_price']);
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
添加 - 如果购物车内容数量超过 2,则所有商品均可享受折扣。
您将使用与上面相同的第一个钩子函数代码。
您将用以下内容替换第二个挂钩函数:
// Set a discounted price on cart items when cart content count is over 2
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get the total items count
$total_count = $cart->get_cart_contents_count();
// if total count is below 2 we exit
if( $total_count < 2 )
return; // Exit
// Loop through cart items
foreach($cart->get_cart() as $cart_item) {
// Set the calculated discounted price
if (isset($cart_item['discounted_price']))
$cart_item['data']->set_price($cart_item['discounted_price']);
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。它应该有效。
加法 2
- 只有第一个购物车商品是全价,下一个数量是第一个商品的折扣)
- 所有 newt 购物车商品都打折。
代码:
// Calculate and save as custom cart item data the discounted price
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 3);
function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
// HERE set the percentage rate to be applied to get the new price
$percentage = 10; // 10%
$_product_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product($_product_id); // The WC_Product Object
$base_price = (float) $product->get_price(); // Get the product active price
// Save the normal active product price as custom cart item data
$cart_item_data['normal_price'] = $base_price;
// Save the calculated discounted price as custom cart item data
$cart_item_data['discounted_price'] = $base_price * ( 100 - $percentage ) / 100;
return $cart_item_data;
}
// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Initializing variables
$count = 0;
$first_item = true
// Loop through cart items
foreach($cart->get_cart() as $cart_item) {
// 1. First cart item
if ( isset($cart_item['discounted_price']) && isset($cart_item['normal_price']) && $first_item ){
if( $cart_item['quantity'] > 1 ){
$normal_price = (float) $cart_item['normal_price'];
$discount_price = (float) $cart_item['discounted_price'];
$quantity = (int) $cart_item['quantity'];
// The first item is at full price and others at discounted price
$cart_item['data']->set_price( $normal_price + ( $discount_price * ($quantity - 1) ) );
}
$first_item = false; // We switch it to false as it is the first cart item
}
// 2. All next items (at discounted price
elseif ( isset($cart_item['discounted_price']) && ! $first_item ){
// Set the discounted price
$cart_item['data']->set_price($cart_item['discounted_price']);
}
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。它应该有效。