我在我的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;
希望能帮助到你。
在购物车对象中,您唯一可以真正改变并产生影响的是活动价格。
更改购物车项目中的常规价格或销售价格无效。
尝试以下操作,更改第 2 个购物车及以后的价格,数据将正确传递到 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
编码:
// 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 文件中。它应该有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句