【发布时间】:2015-12-07 05:15:21
【问题描述】:
我正在尝试向我的 Woocommerce 套件添加新的运输方式。
基本上,我尝试根据用户角色对订单应用两种不同的运费。我使用统一费率作为其中之一,但需要定制一个。
我已尝试使用the 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' );
}
我使用 Composer 加载我的类。
我不应该在 Woocommerce Shipping 设置中看到新选项吗?任何指导表示赞赏。
另一种方法是截取会话数据并在那里更改运费总额和税金。然而,这似乎不起作用。我在哪里可以找到有关运输信息来源的更多数据;何时何地调用它?
【问题讨论】:
标签: php wordpress woocommerce