Woocommerce 为区域添加自定义装运选项
我正在尝试添加一个自定义的运送选项来支持区域。 我查看了所有的示例(例如),但当我将其添加为区域上的装运方式时,我无法使其显示出来Woocommerce 为区域添加自定义装运选项,woocommerce,hook-woocommerce,Woocommerce,Hook Woocommerce,我正在尝试添加一个自定义的运送选项来支持区域。 我查看了所有的示例(例如),但当我将其添加为区域上的装运方式时,我无法使其显示出来 <?php function hotcookie_USPS_method() { // if ( ! class_exists( 'hotcookie_USPS_Method' ) ) { class hotcookie_USPS_Method extends WC_Shipping_Method { var $id;
<?php
function hotcookie_USPS_method() {
// if ( ! class_exists( 'hotcookie_USPS_Method' ) ) {
class hotcookie_USPS_Method extends WC_Shipping_Method {
var $id; // Method ID - should be unique to the shipping method
var $instance_id; // Instance ID number
/**
* Constructor. The instance ID is passed to this.
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'hotcookie_USPS_delivery';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'hotcookie USPS Delivery', 'hotcookie');
$this->method_description = __( 'Custom USPS Shipping Method for Hot Cookie', 'hotcookie');
$this->title = $this->method_title;
$this->supports = array(
'shipping-zones',
'instance-settings',
);
$this->instance_form_fields = array(
'title' => array(
'title' => __( 'Method Title' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.' ),
'default' => __( 'Test Method' ),
'desc_tip' => true
),
);
$this->enabled = 'yes';
$this->title = $this->get_option( 'title' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* calculate_shipping function.
* @param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate( array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => 100,
) );
}
}
// }
}
add_action( 'woocommerce_shipping_init', 'hotcookie_USPS_method' );
function add_hotcookie_usps_method( $methods ) {
$methods['hotcookie_usps'] = 'hotcookie_USPS_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_hotcookie_usps_method' );
?>
显示为可选择的选项
但在我单击“添加配送方式”后,它就不会显示了。我终于让它工作起来了。我唯一的改变就是改变
$this->id = 'hotcookie_USPS_delivery';
到
相配
$methods['hotcookie_usps'] = 'hotcookie_USPS_Method';
方法中的字符串是否需要与“id”相同
$methods['hotcookie_usps'] = 'hotcookie_USPS_Method';