WooCommerce 教程:“您只需要花费 $ $$免费送货” 在购物页面

redmaomail 2024-07-24 17:21 阅读数 83 #WooCommerce

红帽云邮外贸主机

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

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

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

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


/**
* @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Cart

* @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

* @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

* @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

* @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' );

如何添加此代码?

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

2 、 WordPress 4.9 后改进了主题编辑器,对于 CSS 代码也可打开网站前台编辑器的【自定义】,复制代码添加到自定义 css 中。

此代码是否可用?

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

文章没看懂?代码不会用?需要帮助您可以

红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机