为区域添加自定义运输选项

时间:2019-09-17 19:10:28

标签: woocommerce hook-woocommerce

我正在尝试添加自定义运送选项以支持区域。 我查看了所有示例(例如https://www.tychesoftwares.com/creating-a-new-shipping-method-and-exploring-the-shipping-method-api-of-woocommerce/),但是当我将其添加为区域中的送货方式时,却无法显示它。

<?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' );
?>

显示为选择

但是在我单击“添加运输方式”后没有显示。

1 个答案:

答案 0 :(得分:0)

我终于开始工作了。我所做的唯一更改就是更改

        $this->id                 = 'hotcookie_USPS_delivery';

        $this->id                 = 'hotcookie_usps';

使其匹配

  $methods['hotcookie_usps'] = 'hotcookie_USPS_Method';

方法中的字符串是否需要与“ id”相同?