1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
更新
您必須將所有相關(guān)代碼替換為以下代碼,該代碼將根據(jù)購(gòu)物車(chē)重量進(jìn)行折扣,顯示一條顯示剩余重量的自定義消息,以獲得更好的百分比折扣(顯示百分比)。
對(duì)于消息:由于它是基于重量的折扣,因此無(wú)法顯示剩余成本。相反,您可以顯示獲得更好折扣所需的剩余重量。
這是代碼:
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_discount', 30, 1 );
function shipping_weight_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$cart_weight = $cart->get_cart_contents_weight();
$cart_subtotal = $cart->get_subtotal(); // Or $cart->subtotal;
$percentage = 0;
if ( $cart_weight >= 10 && $cart_weight < 30 )
{
$percentage = 5;
$remaining_weight = 30 - $cart_weight;
$next_percent = 7.5;
}
elseif ( $cart_weight >= 30 && $cart_weight < 70 )
{
$percentage = 7.5;
$remaining_weight = 70 - $cart_weight;
$next_percent = 10;
}
elseif ( $cart_weight >= 70 && $cart_weight < 130 )
{
$percentage = 10;
$remaining_weight = 130 - $cart_weight;
$next_percent = 12.5;
}
elseif ( $cart_weight >= 130 && $cart_weight < 200 ) {
$percentage = 12.5;
$remaining_weight = 200 - $cart_weight;
$next_percent = 15;
}
elseif ( $cart_weight >= 200 )
{
$percentage = 15;
$next_percent = false;
}
else
{
$next_percent = 5;
$remaining_weight = 10 - $cart_weight;
}
// Apply a calculated discount based on weight
if( $percentage > 0 ) {
$discount = $cart_subtotal * $percentage / 100;
$cart->add_fee( sprintf( __( 'Weight %s discount', 'woocommerce' ), $percentage.'%'), -$discount );
}
if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
return;
// Display a custom message
if ( is_cart() && $next_percent ) {
wc_add_notice( sprintf(
__("If you order for %s extra, you will receive a %s discount.", "woocommerce"),
wc_format_weight($remaining_weight), $next_percent.'%'
), 'notice' );
}
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中。經(jīng)過(guò)測(cè)試并有效。
購(gòu)物車(chē)中顯示的消息的屏幕截圖:
- 1 回答
- 0 關(guān)注
- 122 瀏覽
添加回答
舉報(bào)