如果您曾经为WordPress开发过某些东西,无论是主题还是简单的插件,您都已经熟悉WordPress的模块化。 WooCommerce的开发也考虑了可扩展性。
在本教程中,我们将为WooCommerce创建一个简单的运输方法,该方法将计算运输成本。 我们还将对插件添加一些限制,以便我们的送货方式仅适用于某些国家/地区。
要遵循本教程,您将需要:
- WordPress的
- WooCommerce插件已安装并激活
- 首选编辑
我们的运输方式的运输规则
在开始之前,我们需要定义我们的送货方式将如何计算成本以及运送地点。
成本将取决于我们需要运输的重量以及我们需要运输的区域。 区域是分配给国家和价格的数字。 数字越大,运输距离越长。 我们的虚拟货运公司仅向以下几个国家/地区发货:
- 美国
- 加拿大
- 德国
- 英国
- 意大利
- 西班牙
- 克罗地亚
我们的运输公司将来自克罗地亚,因此克罗地亚位于0区。让我们为其他国家/地区定义区域:
- 美国:3
- 加拿大:3
- 德国:1
- 英国:2
- 意大利:1
- 西班牙:2
现在,我们已经可以在相应的区域中运送所有国家/地区,现在该定义价格了。
- 0区:$ 10美元
- 1区:30美元
- 二区:$ 50
- 三区:$ 70
我还提到了重量,对吗? 我们的运输公司最多可以运输100公斤,价格是:
- 0-10公斤:$ 0
- 11–30公斤:$ 5
- 31–50公斤:$ 10
- 51-100公斤:$ 20美元
我们拥有创建运输方式所需的一切。 让我们学习一些有关WooCommerce Shipping API的知识。
WooCommerce运送方法API简介
创建运输方法时,我们需要从WooCommerce抽象类WC_Shipping_Method
扩展该类。 此类中定义的属性是:
-
$id
:我们运送的ID(子弹,关键字)。 需要。 -
$number
:整数ID。
-
$method_title
:在admin中显示的运送名称。
-
$method_description
:管理员(可选)中显示的简短运输说明。
-
$enabled
:字符串布尔值(“是”或“否”),用于提供是否启用我们的运输功能以及是否可以使用的信息。 -
$title
:用于在我们的网站上显示我们的运输名称。
-
$availability
:定义是否可以送货。
-
$countries
:启用此方法的$countries
/地区的数组。 默认值为空数组。
-
$tax_status
:默认值为taxable 。 如果它是应纳税的,则将征税。
-
$fee
:默认值为0 。 该方法的费用。
-
$minimum_fee
:该方法的最低费用。 默认值为空。
-
$has_settings
:定义此方法是否具有任何设置。 默认值为true。
-
$supports
:包含此方法支持的功能的数组。 默认值为空数组。
-
$rates
:$rates
数组。 必须填写此内容以注册运输成本。 默认值为空数组。
WC_Shipping_Method
中定义的方法是:
-
is_taxable()
:返回是否需要在运费基础上计算税款。 -
add_rate( $args = array() )
:将参数$ args中定义的运输价格推入属性$rates
。
-
has_settings()
:返回属性$has_settings
的值。 -
is_available()
:返回是否可以送货。 如果在属性$countries
countries中设置了$countries
,并且属性$availability
设置为包含,特定或排除的值,则该国家/地区可以发货时将返回true或false。
-
get_title()
:返回此运输的标题。
-
get_fee( $fee, $total )
:根据已解析的$fee
和$total
返回此运输的费用值。
-
supports( $feature )
:返回此传送方法是否支持某个功能。
由于类WC_Shipping_Method
扩展了类WC_Settings_API
,因此为简单起见,这里将不再解释更多的属性和方法。
还需要定义其他方法,以便货运可以获取或设置设置以及计算货运的实际成本。 这些方法是:
-
init()
:创建表单字段和设置(可以使用不同的名称,只要我们使用其中的方法并在__constructor
方法中调用它即可)。
-
calculate_shipping( $package )
:这是用于计算此运输成本的方法。 包装是要运送产品的array
。
在calculate_shipping
方法中,我们将速率与方法add_rate
。 此方法接受具有多个选项的数组:
-
id
:费率的ID。 -
label
:价格标签。
-
cost
:运费。 它可以是单个值,也可以是包含购物车中每个项目成本的array
。
-
taxes
:它接受array
税或不接受任何税,因此税由WooCommerce计算。 如果您不希望计算税收,甚至可以接受false
。 -
calc_tax
:接受per_order
或per_item
。 如果使用per_item
,则需要提供array
费用。
要注册运输方法,我们需要通过传递类名在注册方法的array
中添加运输方法。 我们可以访问该array
并使用WooCommerce插件中定义的WordPress过滤器将修改后的array
发送回去。 该过滤器称为woocommerce_shipping_methods
。
创建新的运输类别
我们将创建运输方式作为扩展WooCommerce插件的新插件。 在wp-content/plugins
下创建一个新的tutsplus-shipping文件夹。 另外,创建一个名称为tutsplus-shipping.php
的文件,并添加以下代码:
<?php
/**
* Plugin Name: TutsPlus Shipping
* Plugin URI: https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098
* Description: Custom Shipping Method for WooCommerce
* Version: 1.0.0
* Author: Igor Benić
* Author URI: http://www.ibenic.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Domain Path: /lang
* Text Domain: tutsplus
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
/*
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function tutsplus_shipping_method() {
if ( ! class_exists( 'TutsPlus_Shipping_Method' ) ) {
class TutsPlus_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'tutsplus';
$this->method_title = __( 'TutsPlus Shipping', 'tutsplus' );
$this->method_description = __( 'Custom Shipping Method for TutsPlus', 'tutsplus' );
$this->init();
$this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'yes';
$this->title = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'TutsPlus Shipping', 'tutsplus' );
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Define settings field for this shipping
* @return void
*/
function init_form_fields() {
// We will add our settings here
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package ) {
// We will add the cost, rate and logics in here
}
}
}
}
add_action( 'woocommerce_shipping_init', 'tutsplus_shipping_method' );
function add_tutsplus_shipping_method( $methods ) {
$methods[] = 'TutsPlus_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_tutsplus_shipping_method' );
}
似乎有很多需要理解的地方,但这很简单。 首先,我们检查是否定义了常量WPINC
,因为如果没有定义,则意味着有人试图直接或从非WordPress位置访问此文件。
现在我们确定可以从WordPress访问此文件,我们可以继续。 在开始为WooCommerce创建运输方式之前,我们需要确保WooCommerce处于活动状态。 我们正在检查文件woocommerce.php
是否在活动插件的数组中,该插件保存在数据库中的active_plugins
选项下。
然后,我们创建函数tutsplus_shipping_method
, tutsplus_shipping_method
其添加到woocommerce_shipping_init
动作中。 动作woocommerce_shipping_init
是WooCommerce Shippings的主要动作,它在实例化所有装运类别之前将其包括在内。 通过执行该操作,我们可以确保在初始化WooCommerce之后并将其包含在适合WooCommerce使用的正确位置的运输方法。
我们类的__construct
方法将设置一些常规属性。 在init
方法中从数据库中加载设置后,其中的某些内容很容易被覆盖。 另外两个方法留空,因为稍后我们将对其进行定义。
设置国家/地区可用性
我们的运输方式仅在先前确定的国家/地区列表中可用。 这将在__construct
方法中的方法init
之前设置。 在方法__construct
添加它:
<?php
//...
$this->method_description = __( 'Custom Shipping Method for TutsPlus', 'tutsplus' );
// Availability & Countries
$this->availability = 'including';
$this->countries = array(
'US', // Unites States of America
'CA', // Canada
'DE', // Germany
'GB', // United Kingdom
'IT', // Italy
'ES', // Spain
'HR' // Croatia
);
$this->init();
//...
属性availability
设置为'including'
因此此送货仅适用于属性countries
/地区中包含的countries
。 当WooCommerce希望向客户显示可用的货运时,它将检查该货运的属性countries
地区是否包含货运国家。
创建设置
如果您仔细查看我们的方法__construct
,您会发现我们正在检查enabled
和title
属性的设置。 我们将创建可更改属性的字段。
复制此代码并填充我们的方法init_form_fields
:
<?php
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable', 'tutsplus' ),
'type' => 'checkbox',
'description' => __( 'Enable this shipping.', 'tutsplus' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'tutsplus' ),
'type' => 'text',
'description' => __( 'Title to be display on site', 'tutsplus' ),
'default' => __( 'TutsPlus Shipping', 'tutsplus' )
),
);
}
现在,您可以转到WordPress管理并在WooCommerce>设置>运送> TutsPlus运送中更改这些设置。
尝试更改设置“ 启用”,然后在“ 运输选项 ”选项卡上查看是否启用了我们的运输方式。
我们还定义了我们的运输方式最多可以运输100公斤。 如果该规则在不久的将来发生变化怎么办? 我们可以轻松地在设置中对其进行编辑。 我们将添加该设置,以便我们的方法init_form_fields
现在看起来像这样:
<?php
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable', 'tutsplus' ),
'type' => 'checkbox',
'description' => __( 'Enable this shipping.', 'tutsplus' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'tutsplus' ),
'type' => 'text',
'description' => __( 'Title to be display on site', 'tutsplus' ),
'default' => __( 'TutsPlus Shipping', 'tutsplus' )
),
'weight' => array(
'title' => __( 'Weight (kg)', 'tutsplus' ),
'type' => 'number',
'description' => __( 'Maximum allowed weight', 'tutsplus' ),
'default' => 100
),
);
}
现在,我们甚至可以在设置下设置最大重量。 我们可以使我们的国家,地区和其他所有东西也可以自定义,但是为了简单起见,我们将跳过这一部分。
计算运输成本
现在,所有设置都已完成,在销售产品时,我们还需要采取进一步的措施。 我们需要根据访客购物车中的产品计算运输费用。
我们将一次更新一次calculate_shipping
方法,以便您了解每一步。 第一步是按重量计算成本。 将此代码添加到该方法:
<?php
//...
public function calculate_shipping( $package ) {
$weight = 0;
$cost = 0;
$country = $package["destination"]["country"];
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$weight = $weight + $_product->get_weight() * $values['quantity'];
}
$weight = wc_get_weight( $weight, 'kg' );
if( $weight <= 10 ) {
$cost = 0;
} elseif( $weight <= 30 ) {
$cost = 5;
} elseif( $weight <= 50 ) {
$cost = 10;
} else {
$cost = 20;
}
}
//...
我们定义了一些初始变量: $weight
, $cost
和$country
。 变量$weight
将保存所有产品的总重量,变量$cost
将保存此运输方式的费用,变量$country
将保存所选运输国家/地区的ISO代码。
我们通过迭代购物车并将购物车中每种产品的重量加到变量$weight
来获得总$weight
。 有了总重量后,我们将使用wc_get_weight
函数将重量转换为千克,因为这是我们的送货方式设置限制的单位。
这里的最后一件事是获得所计算重量的成本。 如果您仔细观察最后一部分,我们没有像我们所说的那样限制100公斤的重量。 即使总重为101公斤或以上的购物车,我们的运输成本也将显示成本。 我们的运输限制的这一部分将在本文后面进行,作为处理结帐或更新订单时的限制。
计算选定运输国家的成本
现在我们有了基于购物车重量的成本,我们必须计算选定运输国家的成本。 接下来添加此代码:
<?php
//...
public function calculate_shipping( $package ) {
//...
$countryZones = array(
'HR' => 0,
'US' => 3,
'GB' => 2,
'CA' => 3,
'ES' => 2,
'DE' => 1,
'IT' => 1
);
$zonePrices = array(
0 => 10,
1 => 30,
2 => 50,
3 => 70
);
$zoneFromCountry = $countryZones[ $country ];
$priceFromZone = $zonePrices[ $zoneFromCountry ];
$cost += $priceFromZone;
}
//...
$countryZones
数组保存每个国家的区域。 第二个数组$zonePrices
保存每个区域的价格。 设置完这两个数组后,我们将按区域获得成本,如下所示:
- 我们将ISO国家/地区代码传递给数组
$countryZones
以获取区域。 - 我们将返回的区域传递给数组
$zonePrices
以获取成本。 - 我们将返回的成本添加到变量
$cost
。
注册费率
我们通过总重量计算了成本,并且还按运输国家/地区添加了成本。 这里的最后一步是注册费率,因此请添加最后一部分:
<?php
//...
public function calculate_shipping( $package ) {
//...
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
);
$this->add_rate( $rate );
}
//...
如果您在执行该方法时遇到困难,则这里是此方法的完整代码:
<?php
//...
public function calculate_shipping( $package ) {
$weight = 0;
$cost = 0;
$country = $package["destination"]["country"];
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$weight = $weight + $_product->get_weight() * $values['quantity'];
}
$weight = wc_get_weight( $weight, 'kg' );
if( $weight <= 10 ) {
$cost = 0;
} elseif( $weight <= 30 ) {
$cost = 5;
} elseif( $weight <= 50 ) {
$cost = 10;
} else {
$cost = 20;
}
$countryZones = array(
'HR' => 0,
'US' => 3,
'GB' => 2,
'CA' => 3,
'ES' => 2,
'DE' => 1,
'IT' => 1
);
$zonePrices = array(
0 => 10,
1 => 30,
2 => 50,
3 => 70
);
$zoneFromCountry = $countryZones[ $country ];
$priceFromZone = $zonePrices[ $zoneFromCountry ];
$cost += $priceFromZone;
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
);
$this->add_rate( $rate );
}
//...
如果您现在尝试查看购物车或转到结帐页面并选择可用于此运送的国家/地区,则将使用此自定义运送方式显示运费。 这是一张来自3区的国家的照片:
添加限制
由于即使购物车的总重量超过了我们的限制,我们也允许运输方式注册其费率,所以我们必须添加一些限制。 我们的限制条件将通知客户该订单因重量原因而无法发货。

限制功能
在我们的功能中,我们将寻找已选择的运输方式。 如果方法是我们的TutsPlus_Shipping_Method
那么我们将检查其重量限制和购物车中的总重量。 如果购物车中的重量超过了重量限制,我们将通知我们的客户。
在过滤器woocommerce_shipping_methods
添加以下代码:
<?php
function tutsplus_validate_order( $posted ) {
$packages = WC()->shipping->get_packages();
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if( is_array( $chosen_methods ) && in_array( 'tutsplus', $chosen_methods ) ) {
foreach ( $packages as $i => $package ) {
if ( $chosen_methods[ $i ] != "tutsplus" ) {
continue;
}
$TutsPlus_Shipping_Method = new TutsPlus_Shipping_Method();
$weightLimit = (int) $TutsPlus_Shipping_Method->settings['weight'];
$weight = 0;
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$weight = $weight + $_product->get_weight() * $values['quantity'];
}
$weight = wc_get_weight( $weight, 'kg' );
if( $weight > $weightLimit ) {
$message = sprintf( __( 'Sorry, %d kg exceeds the maximum weight of %d kg for %s', 'tutsplus' ), $weight, $weightLimit, $TutsPlus_Shipping_Method->title );
$messageType = "error";
if( ! wc_has_notice( $message, $messageType ) ) {
wc_add_notice( $message, $messageType );
}
}
}
}
}
我们正在获得按运输方式分开的包裹。 如果所有产品都使用相同的运送方式,则所有产品将只返回一个包裹。 在那之后,我们得到了选择的方法,然后对于每个包裹,我们正在检查该包裹是否由我们的运送方法运送。
如果要使用我们的运输方式运输包裹,则需要从设置中设置重量限制,然后再计算包裹的总重量。 如果总重量超过我们的重量限制,它将通知客户。
通知订单更新
每当客户在结帐页面上进行任何更改时,都会进行订单更新。 我们将功能添加tutsplus_validate_order
的动作woocommerce_review_order_before_cart_contents
。 设置完所有内容后,将调用此操作,以便我们可以从会话中获取所选的运输方式,并从运输中获取包裹。 一旦客户更改了某些内容,此操作将触发我们的功能并在必要时添加通知。
在我们的函数tutsplus_validate_order
之后添加以下代码:
<?php
add_action( 'woocommerce_review_order_before_cart_contents', 'tutsplus_validate_order' , 10 );
结帐时通知
当客户单击按钮下订单或购买时,WooCommerce将处理所有帐单和运输数据以及购物车内容和选定的运输。
如果有任何WooCommerce错误通知,它将停止结帐过程并将所有这些错误通知显示给客户。 由于我们已经创建了一个使用会话获取所选运输方式的函数,因此我们必须将此函数添加到将在设置会话之后触发的操作中。 即将在WooCommerce检查是否有任何错误通知之前触发的操作是woocommerce_after_checkout_validation
。
让我们将函数添加到该操作中:
<?php
add_action( 'woocommerce_after_checkout_validation', 'tutsplus_validate_order' , 10 );
完整代码
这是此自定义送货方式的完整代码,因此,如果您在阅读本教程时遇到任何麻烦,可以在这里查看:
<?php
/**
* Plugin Name: TutsPlus Shipping
* Plugin URI: http://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098
* Description: Custom Shipping Method for WooCommerce
* Version: 1.0.0
* Author: Igor Benić
* Author URI: http://www.ibenic.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Domain Path: /lang
* Text Domain: tutsplus
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
/*
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function tutsplus_shipping_method() {
if ( ! class_exists( 'TutsPlus_Shipping_Method' ) ) {
class TutsPlus_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'tutsplus';
$this->method_title = __( 'TutsPlus Shipping', 'tutsplus' );
$this->method_description = __( 'Custom Shipping Method for TutsPlus', 'tutsplus' );
// Availability & Countries
$this->availability = 'including';
$this->countries = array(
'US', // Unites States of America
'CA', // Canada
'DE', // Germany
'GB', // United Kingdom
'IT', // Italy
'ES', // Spain
'HR' // Croatia
);
$this->init();
$this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'yes';
$this->title = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'TutsPlus Shipping', 'tutsplus' );
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Define settings field for this shipping
* @return void
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable', 'tutsplus' ),
'type' => 'checkbox',
'description' => __( 'Enable this shipping.', 'tutsplus' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'tutsplus' ),
'type' => 'text',
'description' => __( 'Title to be display on site', 'tutsplus' ),
'default' => __( 'TutsPlus Shipping', 'tutsplus' )
),
'weight' => array(
'title' => __( 'Weight (kg)', 'tutsplus' ),
'type' => 'number',
'description' => __( 'Maximum allowed weight', 'tutsplus' ),
'default' => 100
),
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package ) {
$weight = 0;
$cost = 0;
$country = $package["destination"]["country"];
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$weight = $weight + $_product->get_weight() * $values['quantity'];
}
$weight = wc_get_weight( $weight, 'kg' );
if( $weight <= 10 ) {
$cost = 0;
} elseif( $weight <= 30 ) {
$cost = 5;
} elseif( $weight <= 50 ) {
$cost = 10;
} else {
$cost = 20;
}
$countryZones = array(
'HR' => 0,
'US' => 3,
'GB' => 2,
'CA' => 3,
'ES' => 2,
'DE' => 1,
'IT' => 1
);
$zonePrices = array(
0 => 10,
1 => 30,
2 => 50,
3 => 70
);
$zoneFromCountry = $countryZones[ $country ];
$priceFromZone = $zonePrices[ $zoneFromCountry ];
$cost += $priceFromZone;
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
);
$this->add_rate( $rate );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'tutsplus_shipping_method' );
function add_tutsplus_shipping_method( $methods ) {
$methods[] = 'TutsPlus_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_tutsplus_shipping_method' );
function tutsplus_validate_order( $posted ) {
$packages = WC()->shipping->get_packages();
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if( is_array( $chosen_methods ) && in_array( 'tutsplus', $chosen_methods ) ) {
foreach ( $packages as $i => $package ) {
if ( $chosen_methods[ $i ] != "tutsplus" ) {
continue;
}
$TutsPlus_Shipping_Method = new TutsPlus_Shipping_Method();
$weightLimit = (int) $TutsPlus_Shipping_Method->settings['weight'];
$weight = 0;
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$weight = $weight + $_product->get_weight() * $values['quantity'];
}
$weight = wc_get_weight( $weight, 'kg' );
if( $weight > $weightLimit ) {
$message = sprintf( __( 'Sorry, %d kg exceeds the maximum weight of %d kg for %s', 'tutsplus' ), $weight, $weightLimit, $TutsPlus_Shipping_Method->title );
$messageType = "error";
if( ! wc_has_notice( $message, $messageType ) ) {
wc_add_notice( $message, $messageType );
}
}
}
}
}
add_action( 'woocommerce_review_order_before_cart_contents', 'tutsplus_validate_order' , 10 );
add_action( 'woocommerce_after_checkout_validation', 'tutsplus_validate_order' , 10 );
}
结论
WooCommerce运送API使您可以通过简单的步骤创建自己的运送方法。 在计算运输方式的可用性或成本时,可以在运输方式内部设置限制和可用性,但是也可以使用WooCommerce操作在运输方式外部设置限制和可用性。
如果您正在寻找其他实用程序来帮助您构建不断增长的WordPress工具集或用于研究代码并变得更加精通WordPress的代码,请不要忘记查看Envato Market中提供的功能 。
如果您对此或任何其他运输方式有疑问,可以在下面的评论中发布。 您也可以在我的博客或Twitter @igorbenic上关注我。
翻译自: https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098