Php 以编程方式向Woocommerce添加新的配送方法
我正在尝试为我的Woocommerce套件添加一种新的配送方式 基本上,我试图根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个 我尝试创建一个新类,但似乎什么都没有出现Php 以编程方式向Woocommerce添加新的配送方法,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试为我的Woocommerce套件添加一种新的配送方式 基本上,我试图根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个 我尝试创建一个新类,但似乎什么都没有出现 <?php namespace MySite; use WC_Shipping_Method; function your_shipping_method_init() { if ( ! class_exists( 'CustomShipping' ) ) {
<?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' );
}
我不确定您的代码的确切问题,但是其他的发布插件都是这样做的,所以我可能会复制它们:
首先将您的方法放入另一个文件classes/class-wc-yourshipping-method.php
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
class WC_Your_Shipping_Method extends WC_Shipping_Method {
// your class stuff truncated for brevity
}
}
然后将shipping类文件包含在插件中的某个位置:
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\u shipping\u init
的操作(因此它从未被调用),第二个是没有使用指定的名称空间名称空间MySite
如果您检查代码的缩进,第一个问题就更容易看到,它都包装在init函数中
第二个问题在日志中很明显,当WooCommerce尝试加载该类时,会出现如下错误,但由于第一个问题,它没有加载该类
PHP致命错误:在第136行的/wp-content/plugins/woocommerce/includes/Class-WC-Shipping.PHP中找不到类“WC\u-Your\u Shipping\u-Method”
PHP警告:call_user_func_array()要求参数1为有效回调,在第213行的/wp includes/plugin.PHP中未找到函数“add_your_shipping_method”或函数名无效
我删除了下面的实现代码,但在一个开发站点上测试了它,它在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您的发货方式
描述:测试装运方法
版本:1.0
*/
名称空间MySite;
使用WC_装运方法;
函数\u shipping\u method\u init(){
如果(!class_存在('WC_-Your_-Shipping_-Method')){
类WC\u Your\u Shipping\u方法扩展了WC\u Shipping\u方法{
//…用于实现的所有代码
}
}
}//函数中的类没有问题,问题是add\u action()
调用在它所附加的函数中,因此永远不会被调用。您还必须在类\函数上指定名称空间,例如$methods[]='MySite\WC\u Your\u Shipping\u Method'
。我想我不知道名称空间。无名称空间,这正是UPS和USPS功能的工作方式,因此它确实回答了“如何向WooCommerce添加新的配送方式”的问题。有点,只是它没有回答所问的问题。主要的问题是添加操作调用在钩子函数中。啊,是的,我的操作是嵌套的@doublesharp我已经实现了您的代码,而且似乎从未调用过您的\u shipping\u method\u init
。死了,倒进去也没用。我的add_action
和add_filter
调用是否应该不在此文件中?您将代码放在哪里?唯一的区别是,我在文件中添加了一个头,使其成为一个插件,然后启用它,否则它在我的测试站点上可以正常工作。您还可以将其放入mu plugins
。我用plugin头更新了示例代码。创建一个名为/wp content/plugins/my shipping/
的文件夹,将文件放入其中,然后在插件中启用“WooCommerce-Your shipping Method”,它应该显示在WooCommerce设置中。这是一个可靠的wp站点。我的所有类都存在于app/plugins/myfunctions/src/
中,其中myfunctions
是启用的插件。这个类文件是CustomShipping.php
。我使用pimple容器进行依赖项注入。当我将所有内容都放在functions.php文件中时,它会起作用,但是使用上述结构我将如何实现这一点?