以编程方式向 Woocommerce 添加新的运输方式
Add new shipping method to Woocommerce programmatically
我正在尝试向我的 Woocommerce 套件添加新的送货方式。
基本上,我正在尝试根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个。
我已经尝试 the shipping api 创建一个新的 class 但似乎没有任何显示。
<?php namespace MySite;
use WC_Shipping_Method;
function your_shipping_method_init() {
if ( ! class_exists( 'CustomShipping' ) ) {
class CustomShipping extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Your Shipping Method' ); // Title shown in admin
$this->method_description = __( 'Description of your shipping method' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "My Shipping Method"; // This can be added as an setting but for this example its forced.
$this->init();
}
function init() {
// Load the settings API
$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
$this->init_settings(); // This is part of the settings API. Loads settings you previously init.
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package ) {
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => '10.99',
'calc_tax' => 'per_item'
);
// Register the rate
$this->add_rate( $rate );
}
}
}
add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );
function add_your_shipping_method( $methods ) {
$methods[] = 'CustomShipping';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}
我使用 Composer 加载我的 classes。
我不是应该在 Woocommerce 运输设置中看到新选项吗?任何指导表示赞赏。
另一种方法是截取会话数据,只更改运费总额和税费。但是,这似乎不起作用。我在哪里可以找到有关运输信息来源的更多数据;何时何地调用?
我不确定您的代码的确切问题,但其他运输插件如下所示,所以我可能会复制它们:
先把你的方法放到另一个文件里classes/class-wc-your-shipping-method.php
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
class WC_Your_Shipping_Method extends WC_Shipping_Method {
// your class stuff truncated for brevity
}
}
然后在您的插件中包含来自某处的运送 class 文件:
function so_32509983_init() {
include_once( 'classes/class-wc-your-shipping-method.php' );
}
add_action( 'woocommerce_shipping_init', 'so_32509983_init' );
最后告诉 WooCommerce 将您的方法初始化为其活动方法之一:
function so_32509983_add_method( $methods ) {
$methods[] = 'WC_Your_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'so_32509983_add_method' );
您的代码中有两个问题。第一个是您要在附加到它的函数中添加 woocommerce_shipping_init
的操作(因此它永远不会被调用),第二个是您没有使用指定的名称空间,namespace MySite
.
如果您检查代码的缩进,第一个问题更容易看到,它都包含在 init 函数中。
一旦 WooCommerce 尝试加载 class,第二个问题将在您的日志中出现,如下所示错误,由于第一个问题,它没有这样做。
PHP Fatal error: Class 'WC_Your_Shipping_Method' not found in /wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 136
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_your_shipping_method' not found or invalid function name in /wp-includes/plugin.php on line 213
我在下面删除了您的实施代码,但在开发网站上对其进行了测试,它在 WooCommerce 设置中显示为一种送货方式。
/*
Plugin Name: WooCommerce Your Shipping Method
Description: Test shipping method
Version: 1.0
*/
namespace MySite;
use WC_Shipping_Method;
function your_shipping_method_init() {
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
class WC_Your_Shipping_Method extends WC_Shipping_Method {
// ... all your code for the implementation
}
}
} // <-- note that the function is closed before the add_action('woocommerce_shipping_init')
// note "MySite\" prepended to the attached function
add_action( 'woocommerce_shipping_init', 'MySite\your_shipping_method_init' );
// note "MySite\" prepended to the shipping method class name
function add_your_shipping_method( $methods ) {
$methods[] = 'MySite\WC_Your_Shipping_Method';
return $methods;
}
// note "MySite\" prepended to the attached function
add_filter( 'woocommerce_shipping_methods', 'MySite\add_your_shipping_method' );
我正在尝试向我的 Woocommerce 套件添加新的送货方式。
基本上,我正在尝试根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个。
我已经尝试 the shipping api 创建一个新的 class 但似乎没有任何显示。
<?php namespace MySite;
use WC_Shipping_Method;
function your_shipping_method_init() {
if ( ! class_exists( 'CustomShipping' ) ) {
class CustomShipping extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Your Shipping Method' ); // Title shown in admin
$this->method_description = __( 'Description of your shipping method' ); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "My Shipping Method"; // This can be added as an setting but for this example its forced.
$this->init();
}
function init() {
// Load the settings API
$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
$this->init_settings(); // This is part of the settings API. Loads settings you previously init.
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package ) {
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => '10.99',
'calc_tax' => 'per_item'
);
// Register the rate
$this->add_rate( $rate );
}
}
}
add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );
function add_your_shipping_method( $methods ) {
$methods[] = 'CustomShipping';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}
我使用 Composer 加载我的 classes。
我不是应该在 Woocommerce 运输设置中看到新选项吗?任何指导表示赞赏。
另一种方法是截取会话数据,只更改运费总额和税费。但是,这似乎不起作用。我在哪里可以找到有关运输信息来源的更多数据;何时何地调用?
我不确定您的代码的确切问题,但其他运输插件如下所示,所以我可能会复制它们:
先把你的方法放到另一个文件里classes/class-wc-your-shipping-method.php
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
class WC_Your_Shipping_Method extends WC_Shipping_Method {
// your class stuff truncated for brevity
}
}
然后在您的插件中包含来自某处的运送 class 文件:
function so_32509983_init() {
include_once( 'classes/class-wc-your-shipping-method.php' );
}
add_action( 'woocommerce_shipping_init', 'so_32509983_init' );
最后告诉 WooCommerce 将您的方法初始化为其活动方法之一:
function so_32509983_add_method( $methods ) {
$methods[] = 'WC_Your_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'so_32509983_add_method' );
您的代码中有两个问题。第一个是您要在附加到它的函数中添加 woocommerce_shipping_init
的操作(因此它永远不会被调用),第二个是您没有使用指定的名称空间,namespace MySite
.
如果您检查代码的缩进,第一个问题更容易看到,它都包含在 init 函数中。
一旦 WooCommerce 尝试加载 class,第二个问题将在您的日志中出现,如下所示错误,由于第一个问题,它没有这样做。
PHP Fatal error: Class 'WC_Your_Shipping_Method' not found in /wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 136
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_your_shipping_method' not found or invalid function name in /wp-includes/plugin.php on line 213
我在下面删除了您的实施代码,但在开发网站上对其进行了测试,它在 WooCommerce 设置中显示为一种送货方式。
/*
Plugin Name: WooCommerce Your Shipping Method
Description: Test shipping method
Version: 1.0
*/
namespace MySite;
use WC_Shipping_Method;
function your_shipping_method_init() {
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
class WC_Your_Shipping_Method extends WC_Shipping_Method {
// ... all your code for the implementation
}
}
} // <-- note that the function is closed before the add_action('woocommerce_shipping_init')
// note "MySite\" prepended to the attached function
add_action( 'woocommerce_shipping_init', 'MySite\your_shipping_method_init' );
// note "MySite\" prepended to the shipping method class name
function add_your_shipping_method( $methods ) {
$methods[] = 'MySite\WC_Your_Shipping_Method';
return $methods;
}
// note "MySite\" prepended to the attached function
add_filter( 'woocommerce_shipping_methods', 'MySite\add_your_shipping_method' );