2017-05-24 4 views
0

Hallo, ich versuche, ein sehr einfaches Plugin für Woocommerce zu bauen, um einige Informationen über die Lieferung ohne Steuern zu zeigen. Der Unterschied für die anderen Methoden ist das zusätzliche HTML-Feld.woocommerce Versandmethode mit 3.0 api

Ich lese viel von der Dokumentation, aber ich denke, dass etwas an der Konfiguration fehlt. Der Administrator scheint zu funktionieren. Aber die Methode erschien nicht auf dem Kassenbildschirm. Der folgende Code ist:

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

Antwort

0

Sie benötigen die calculate_shipping Funktion Ihrer Klasse

 /** 
     * 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); 

      } 
     } 

Auf jeden Fall hinzufügen, ich glaube, Sie nicht über eine neue Versandart hinzufügen müssen nur ein paar Informationen zu zeigen, bei den Warenkorb, stattdessen können Sie diese Aktionen verwenden:

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>'; 
} 
Verwandte Themen