2017-07-18 87 views
6

我已经成功创建了新的送货方式,并为送货区域提供了支持。但是,当我从下拉列表中选择方法将其添加到区域时,它不会出现在“选择的方法列表”中。如何在WooCommerce中添加自定义的送货方式3

我录一个screencast gif证明:

screencast gif

我不能为我的生命弄清楚为什么它不工作。如果我选择其中一种标准方法(Screencast GIF

任何人都知道这里发生了什么以及如何使其工作?

下面的代码,我有from this official thread: Shipping Method API

if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { 

    function request_a_shipping_quote_init() { 
     if (! class_exists('WC_Request_Shipping_Quote_Method')) { 
      class WC_Request_Shipping_Quote_Method extends WC_Shipping_Method { 
       /** 
       * Constructor for your shipping class 
       * 
       * @access public 
       * @return void 
       */ 
       public function __construct() { 
        $this->id     = 'request_a_shipping_quote'; // Id for your shipping method. Should be uunique. 
        $this->method_title  = __('Request a Shipping Quote'); // Title shown in admin 
        $this->method_description = __('Shipping method to be used where the exact shipping amount needs to be quoted'); // Description shown in admin 

        $this->title = "Request a Shipping Quote"; // This can be added as an setting but for this example its forced. 

        $this->supports = array(
         'shipping-zones' 
        ); 

        $this->init(); 
       } 

       /** 
       * Init your settings 
       * 
       * @access public 
       * @return void 
       */ 
       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')); 
       } 

       function init_form_fields() { 

        $this->form_fields = array(

         'enabled' => array(
          'title'  => __('Enable', 'dc_raq'), 
          'type'  => 'checkbox', 
          'description' => __('Enable this shipping method.', 'dc_raq'), 
          'default'  => 'yes' 
         ), 

         'title' => array(
          'title'  => __('Title', 'dc_raq'), 
          'type'  => 'text', 
          'description' => __('Title to be displayed on site', 'dc_raq'), 
          'default'  => __('Request a Quote', 'dc_raq') 
         ), 

        ); 

       } 

       /** 
       * calculate_shipping function. 
       * 
       * @access public 
       * 
       * @param mixed $package 
       * 
       * @return void 
       */ 

       public function calculate_shipping($packages = array()) { 
        $rate = array(
         'id'  => $this->id, 
         'label' => $this->title, 
         'cost'  => '0.00', 
         'calc_tax' => 'per_item' 
        ); 

        // Register the rate 
        $this->add_rate($rate); 
       } 
      } 
     } 
    } 

    add_action('woocommerce_shipping_init', 'request_a_shipping_quote_init'); 

    function request_shipping_quote_shipping_method($methods) { 
     $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method'; 

     return $methods; 
    } 

    add_filter('woocommerce_shipping_methods', 'request_shipping_quote_shipping_method'); 
} 
+0

这不是在woocommerce 3+工作了,看到这个未回答支持线程:HTTPS: //wordpress.org/support/topic/official-custom-shipping-class-doesnt-work-anymore-shipping-method-api/ – LoicTheAztec

+0

**与WC_Shipping_Method'calculate_shipping()'核心方法**有冲突,并且在您的代码插件中定义的那个...这是需要解决的问题编辑。因为**抛出了这个错误**:*严格标准:WC_Request_Shipping_Quote_Method :: calculate_shipping()声明应该与/www/wp-content/plugins/request_shipping_quote_method.php中的WC_Shipping_Method :: calculate_shipping($ package = Array)兼容。 line 18 * – LoicTheAztec

+0

@LoicTheAztec有没有这方面的成功? – omukiguy

回答

2

“woocommerce_shipping_methods”上的方法键应该与Ë运输方式ID

你的情况: 你应该改变

function request_shipping_quote_shipping_method($methods) { 
    $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method'; 

    return $methods; 
} 

add_filter('woocommerce_shipping_methods', 'request_shipping_quote_shipping_method'); 

要:

function request_shipping_quote_shipping_method($methods) { 
    $methods['request_a_shipping_quote'] = 'WC_Request_Shipping_Quote_Method'; 

    return $methods; 
} 

add_filter('woocommerce_shipping_methods', 'request_shipping_quote_shipping_method'); 
+0

只是发现了这个回复 - 修正了它!非常感谢,非常感谢您的回复 – jhob101

3

改变这一行

public function calculate_shipping($package) { 

这一行

公共职能calculate_shipping($包=阵列()){

+0

谢谢你。我确实实现了该代码,但它仍然无法工作。该网站目前是远程的,所以我不能看到抛出什么错误,将它在本地工作,看看我是否可以看到任何错误。 – jhob101

+0

因此,当我添加'$ package = array()'时,现在没有错误被抛出,但行为保持不变 - 运送方法未被添加到运输方法列表中。 – jhob101

+0

你正在得到什么错误? bcoz现在你必须检查你是如何处理你的函数中的包,现在bcoz是包的数组 – Alice

相关问题