外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>商城相关>在 WooCommerce 中提供免费送货时隐藏运费

在 WooCommerce 中提供免费送货时隐藏运费

为您的商店提供免费送货服务是减少摩擦和最小化结账放弃率的好方法。当免费选项可用时,删除其他运费(例如付费选项)是有意义的。在其他情况下,提供额外费率(例如本地取货和溢价/加急费率)对客户有利。

在这篇文章中,有不同的方法可以在免费提供时隐藏其他运费。例如隐藏所有其他选项、仅隐藏付费选项或某些特定费率。

当免费送货可用时隐藏所有运费选项

第一个片段是最简单的。隐藏所有其他运输选项,使免费送货选项成为唯一可用的选项。这会检查 WooCommerce 运输区中设置的“免费送货”方法,当可用时,它会隐藏除免费送货费以外的所有运费选项。

<?php

/**
 * Hide all shipping options when free shipping is available.
 *
 * @author Jeroen Sormani
 * @link   https://aceplugins.com/hide-shipping-rates-when-free-shipping-is-available-in-woocommerce/
 *
 * @param $available_methods
 * @return array
 */
function js_hide_all_shipping_when_free_is_available( $shipping_rates ) {
	foreach ( $shipping_rates as $key => $rate ) {

		// Check if current rate is 'free_shipping'
		if ( $rate->get_method_id() == 'free_shipping' ) {
			$shipping_rates = array( $key => $rate );
		}
	}

	return $shipping_rates;
}
add_filter( 'woocommerce_package_rates', 'js_hide_all_shipping_when_free_is_available' );

只有一种运输选项可用具有额外的好处,即客户不必为运输做出任何额外的选择。默认情况下,WooCommerce 会显示不同可用选项的单选按钮,但只有一种运费,它不会显示为选项。

当免费送货可用时隐藏所有运费选项

保留本地取件可用

在上面的代码中,本地取货选项被隐藏了。即使提供免费送货,客户也可能更愿意取货。这就是为什么这个代码片段将是对上面的一个小的修改;通过此更改,它也将保留任何本地取件选项。

<?php	

/**
 * Hide all other shipping options when free shipping is available.
 *
 * @author Jeroen Sormani
 * @link   https://aceplugins.com/hide-shipping-rates-when-free-shipping-is-available-in-woocommerce/
 *
 * @param $available_methods
 * @return array
 */
function js_hide_shipping_when_free_is_available( $shipping_rates ) {

	$free_rates     = array();
	$free_available = false;

	foreach ( $shipping_rates as $key => $rate ) {

		// Check for free rates / don't take in account local pickup
		if ( $rate->get_method_id() == 'free_shipping' && $rate->get_method_id() != 'local_pickup' ) {
			$free_available = true;
		}

		if ( 0 == $rate->cost ) {
			$free_rates[ $key ] = $rate;
		}
	}

	// Show all free rates
	if ( $free_available ) {
		return $free_rates;
	}
}
add_filter( 'woocommerce_package_rates', 'js_hide_shipping_when_free_is_available' );

在免费可用时隐藏所有支付运费选项(含其他运输类型)

第一个和第二个代码片段仅寻找“免费送货”方法。使用不同的解决方案(例如Advanced Free Shipping 或Advanced Shipping 插件)来创建免费送货选项,之前的代码段无法识别它。通过进行一些更改,可以保留所有可用的免费选项,这些选项可以识别任何方法类型的费率,包括免费送货、本地取货和任何其他自定义送货方式。

<?php	

/**
 * Hide all other shipping options when free shipping is available.
 *
 * @author Jeroen Sormani
 * @link   https://aceplugins.com/hide-shipping-rates-when-free-shipping-is-available-in-woocommerce/
 *
 * @param $available_methods
 * @return array
 */
function js_hide_shipping_when_free_is_available( $shipping_rates ) {

	$free_rates     = array();
	$free_available = false;

	foreach ( $shipping_rates as $key => $rate ) {

		// Check for free rates / don't take in account local pickup
		if ( 0 == $rate->cost && $rate->get_method_id() != 'local_pickup' ) {
			$free_available = true;
		}

		if ( 0 == $rate->cost ) {
			$free_rates[ $key ] = $rate;
		}
	}

	// Show all free rates
	if ( $free_available ) {
		return $free_rates;
	}
}
add_filter( 'woocommerce_package_rates', 'js_hide_shipping_when_free_is_available' );

保留支付运费可用

到目前为止,我们一直隐藏所有支付运费。如果您为“常规”送货选项提供免费送货服务,提供 Premium 或 Expedited 选项可能有利于客户更快地收到他们的产品。

在下面的代码片段中,它将像前面的代码片段一样查找并保留所有免费送货选项,但也可以保留某些(付费)运费。

<?php	

/**
 * Hide shipping options when free shipping is available.
 *
 * @author Jeroen Sormani
 * @link   https://aceplugins.com/hide-shipping-rates-when-free-shipping-is-available-in-woocommerce/
 *
 * @param $available_methods
 * @return array
 */
function js_hide_shipping_when_free_is_available( $shipping_rates ) {

	$show_rates     = array();
	$free_available = false;
	$allowed_paid   = array( 'flat_rate:62' ); // Shipping rate ID or instance ID

	foreach ( $shipping_rates as $key => $rate ) {

		// Check for free rates / don't take in account local pickup
		if ( 0 == $rate->cost && $rate->get_method_id() != 'local_pickup' ) {
			$free_available = true;
		}

		if ( 0 == $rate->cost || in_array( $rate->get_id(), $allowed_paid ) || in_array( $rate->get_instance_id(), $allowed_paid ) ) {
			$show_rates[ $key ] = $rate;
		}
	}

	// Show all free rates
	if ( $free_available ) {
		return $show_rates;
	}
}
add_filter( 'woocommerce_package_rates', 'js_hide_shipping_when_free_is_available' );

注:文本内容出自 aceplugins.com,由 WordPress大学 翻译整理。

拓展阅读:

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
商城相关

了解 WooCommerce 订单失败的原因和解决方案

2021-9-11 8:14:55

WordPress开发商城相关

在 WooCommerce 上添加和显示自定义字段

2021-10-14 9:19:18

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索