Tue*_*ave 5 php wordpress woocommerce
我正在尝试向我的 Woocommerce 套件添加新的运输方式。
基本上,我尝试根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个。
我已经尝试使用Shipping api创建一个新类,但似乎没有显示任何内容。
<?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' );
}
Run Code Online (Sandbox Code Playgroud)
我使用 Composer 来加载我的类。
我不是应该在 Woocommerce Shipping 设置中看到新选项吗?任何指导表示赞赏。
另一种方法是拦截会话数据并仅更改其中的运输总额和税费。但这似乎不起作用。我在哪里可以找到有关运输信息来源的更多数据;何时何地调用?
您的代码中有两个问题。第一个是您要在woocommerce_shipping_init附加到它的函数内部添加操作(因此它永远不会被调用),第二个是您没有使用您指定的命名空间namespace MySite.
如果您检查代码的缩进,第一个问题就更容易看出,它都包含在 init 函数内。
一旦 WooCommerce 尝试加载该类,第二个问题就会在您的日志中明显出现,并出现如下错误,但由于第一个问题而没有执行该操作。
PHP 致命错误:在第 136 行 /wp-content/plugins/woocommerce/includes/class-wc-shipping.php 中找不到类“WC_Your_Shipping_Method”
PHP 警告:call_user_func_array() 期望参数 1 为有效回调,未找到函数“add_your_shipping_method”或 /wp-includes/plugin.php 第 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' );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17509 次 |
| 最近记录: |