2017-05-24 5 views
0

こんにちは、私はwoocommerce用の非常に単純なプラグインを構築して、税金のない配送に関する情報を表示しようとしています。他の方法の違いは、余分なHTMLフィールドです。woocommerce 3.0のAPIを使った配送方法

私は多くのドキュメントを読みましたが、私は設定上何かが間違っていると思います。それは働いている管理者。しかし、この方法はチェックアウト画面には表示されませんでした。以下のコードは次のとおりです。

<?php 
if (! defined('WPINC')) { 
    die('security by preventing any direct access to your plugin file'); 
} 
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { 

function shipping_delivery_info() { 

    if (!class_exists('shipping_delivery_info')) { 

     class shipping_delivery_info extends WC_Shipping_Method { 

      public function __construct($instance_id = 0) { 

       $this->id = 'shipping_delivery_info'; 
       $this->instance_id = absint($instance_id); 
       $this->method_title = __('Shipping Delivery Info', 'shipping_delivery_info'); 
       $this->method_description = __('A Woocommerce custom shipping method plugin, that shows ' . 
        'some shipping information to costumer, like free shipping but with HTML field.', 
        'shipping_delivery_info'); 
       $this->supports = array(
        'shipping-zones', 
        'instance-settings', 
        'instance-settings-modal',      
       ); 

       $this->init(); 
      } 

      /** 
       * Load the settings API 
       */ 
      function init() { 

       // Load the settings 
       $this->init_form_fields(); 
       $this->init_settings(); 

       $this->enabled = $this->get_option('enabled'); 
       $this->title = $this->get_option('title'); 
       $this->info = $this->get_option('info'); 

       add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options')); 
      } 

      function init_form_fields() { 
       $this->instance_form_fields = array(
        'enabled' => array(
         'title'   => __('Enable/Disable', 'shipping_delivery_info'), 
         'type'   => 'checkbox', 
         'label'   => __('Enable this shipping method', 'shipping_delivery_info'), 
         'default'  => 'yes', 
        ), 
        'title' => array(
         'title' => __('Title', 'shipping_delivery_info'), 
         'type' => 'text', 
         'description' => __('The title to be displayed during checkout.', 'shipping_delivery_info'), 
         'default' => __('Shipping Information', 'shipping_delivery_info'), 
        ), 
        'info' => array(
         'title' => __('Information', 'shipping_delivery_info'), 
         'type' => 'text', 
         'description' => __('Information about delivery and its taxes.', 'shipping_delivery_info'), 
         'default' => __('Insert here some HTML.'), 
        ), 
       ); 
      } 
     } 
    } 
} 
add_action('woocommerce_shipping_init', 'shipping_delivery_info'); 

function add_shipping_delivery_info($methods) 
{ 
    $methods['shipping_delivery_info'] = 'shipping_delivery_info'; 
    return $methods; 
} 
add_filter('woocommerce_shipping_methods', 'add_shipping_delivery_info'); 

function shipping_delivery_info_message($posted) 
{ 
    $packages = WC()->shipping->get_packages(); 
    $chosen_methods = WC()->session->get('chosen_shipping_methods'); 
    if (is_array($chosen_methods) && in_array('shipping_delivery_info', $chosen_methods)) { 
     foreach ($packages as $i => $package) { 
      if ($chosen_methods[$i] != "shipping_delivery_info") { 
       continue; 
      } 
      $shipping_delivery_info = new shipping_delivery_info(); 

      $message = $shipping_delivery_info->settings['info']; 
      return $message; 
      /*$messageType = "info";     
      wc_add_notice($message, $messageType);*/ 
     } 
    } 
} 

add_action('woocommerce_review_order_before_cart_contents', 'shipping_delivery_info_message', 10); 
add_action('woocommerce_after_checkout_validation', 'shipping_delivery_info_message', 10); 
} 

答えて

0

あなたは私はあなただけでいくつかの情報を表示するために、新たな配送方法を追加する必要はありませんと思いますが、とにかく自分のクラスに

 /** 
     * function calculate_shipping. 
     * 
     * @access public 
     * @param mixed $package 
     * @return void 
     */ 
     public function calculate_shipping($package = array()) { 
       $rate = array(
        'id' => 'My Method id', 
        'label' => 'New method', 
        'cost' => 0, 
        'calc_tax' => 'per_item' 
       ); 
       $this->add_rate($rate); 

      } 
     } 

をcalculate_shipping機能を追加する必要がありますカートではなく、次の操作を使用できます。

add_action('woocommerce_review_order_before_submit', 'add_tracking_notification', 12); 
add_action('woocommerce_proceed_to_checkout', 'add_tracking_notification'); 

function add_tracking_notification() { 
     echo '<h5 style="margin-bottom:10px">This is a custom message</h5>'; 
} 
関連する問題