这是一个非常酷的片段,您们中许多人应该使用!在我的电子商务体验中,接近 “免运费” 门槛的客户将尝试向购物车添加更多产品以获得免运费。这是纯粹的心理学。以下是我们在 WooCommerce 购物车页面上显示简单信息的方法。

目标:在购物车页面中添加 “免费运送门槛”

(请原谅丹麦语,但您得到点!)

WooCommerce:添加通知 @ cart 以显示剩余金额达到任何免费运送阈值

/**
* @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Cart
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=442
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.0.5
*/

function bbloomer_free_shipping_cart_notice_zones() {

global $woocommerce;

// Get Free Shipping Methods for Rest of the World Zone & populate array $min_amounts

$default_zone = new WC_Shipping_Zone(0);
$default_methods = $default_zone->get_shipping_methods();

foreach( $default_methods as $key => $value ) {
    if ( $value->id === "free_shipping" ) {
      if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
    }
}

// Get Free Shipping Methods for all other ZONES & populate array $min_amounts

$delivery_zones = WC_Shipping_Zones::get_zones();

foreach ( $delivery_zones as $key => $delivery_zone ) {
  foreach ( $delivery_zone['shipping_methods'] as $key => $value ) {
    if ( $value->id === "free_shipping" ) {
	if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
	}
  }
}

// Find lowest min_amount

if ( is_array($min_amounts) ) {

$min_amount = min($min_amounts);

// Get Cart Subtotal inc. Tax excl. Shipping

$current = WC()->cart->subtotal;

// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button

if ( $current < $min_amount ) {
$added_text = esc_html__('Get free shipping if you order ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' more!', 'woocommerce' );
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
}

}

}

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice_zones' );

WooCommerce 2.6+,静态代码段:添加通知 @ cart 以显示剩余的数量以达到免费送货

/**
* @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Cart
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=442
* @author Rodolfo Melogli
* @testedwith WooCommerce 2.6.4
*/

function bbloomer_free_shipping_cart_notice_zones_static() {

// Define min_amount

$min_amount = 40;

// Get Cart Subtotal inc. Tax excl. Shipping

$current = WC()->cart->subtotal;

// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button

if ( $current < $min_amount ) {
$added_text = esc_html__('Get free shipping if you order ', 'woocommerce' ) . wc_price( $threshold - $current ) . esc_html__(' more!', 'woocommerce' );
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
}

}

}

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice_zones_static' );

WooCommerce 最高版本 2.5:添加通知 @ cart 以显示剩余的数量达到免费送货

/**
* @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Cart
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=442
* @author Rodolfo Melogli
* @testedwith WooCommerce 2.5.5
*/

function bbloomer_free_shipping_cart_notice() {

// Get Min Amount from Woo Settings
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$min_amount = $free_shipping_settings['min_amount']; 

// Get Cart Subtotal inc. Tax
$current = WC()->cart->subtotal;

// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button
if ( $current < $min_amount ) {
echo '<div class="woocommerce-message"><a href="' . get_permalink( woocommerce_get_page_id( 'shop' ) ) . '" class="button wc-forward">Continue Shopping</a>Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!</div>';
}
}

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );

PHP 代码段#2(在 WooCommerce Mini-Cart / Widget 购物车上添加通知)

/**
* @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Mini Cart
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=442
* @author Rodolfo Melogli
* @testedwith WooCommerce 2.5.5
*/

function bbloomer_mini_cart_notice() {

// Get Min Amount from Woo Settings
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$min_amount = $free_shipping_settings['min_amount']; 

// Get Cart Subtotal inc. Tax
$current = WC()->cart->subtotal;

// If Subtotal < Min Amount Echo Notice
if ( $current < $min_amount ) {
echo '<div class="woocommerce-message">Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!</div>';
}
}

add_action( 'woocommerce_before_mini_cart', 'bbloomer_mini_cart_notice' );


可以在哪里添加此代码?

您可以将 PHP 代码片段放置在主题或子主题的 functions.php 文件的底部(如果是 CSS 代码,请添加到主题的 style.css 文件底部),修改之前建议先备份原始文件,若出现错误请先删除此代码。


这段代码是否正常可用?

或者是您有更好的解决方案想要分享?请到薇晓朵 WooCommerce 中文论坛留言告知,我们希望可以帮到更多国内的 WooCommerce 用户也希望您的参与。


需要关于 WooCommerce 的帮助?

请观看我们提供的免费视频教程或到薇晓朵 WooCommerce 中文论坛提问,会有专业技术团队提供相关帮助。