2016-10-28 34 views
1

我需要一些幫助來完成此代碼。我一直在嘗試爲我的woocommerce網站製作插件,以根據訂購的總項目自動添加運費。即,當我嘗試下面的代碼時,無論我訂購的物品數量多少,它都會返回11的值。理想siutation低於:使用購物車總項目添加woocommerce的自定義送貨方式

1-5包5000 6-10包萬 11-15包15000 16-20包20000

這裏是我下面的代碼。

<?php 
/* 
Plugin Name: Your Shipping plugin 
Plugin URI: http://woothemes.com/woocommerce 
Description: Your shipping method plugin 
Version: 1.0.0 
Author: WooThemes 
Author URI: http://woothemes.com 
*/ 


/** 
* Check if WooCommerce is active 
*/ 

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

    function your_shipping_method_init() { 
     if (! class_exists('WC_Your_Shipping_Method')) { 
      class WC_Your_Shipping_Method extends WC_Shipping_Method { 
       /** 
       * Constructor for your shipping class 
       * 
       * @access public 
       * @return void 
       */ 
       public function __construct() { 
        $this->id     = 'your_shipping_method'; // Id for your shipping method. Should be uunique. 
        $this->method_title  = __('Your Shipping Method'); // Title shown in admin 
        $this->method_description = __('Description of your shipping method'); // Description shown in admin 

        $this->enabled   = "yes"; // This can be added as an setting but for this example its forced enabled 
        $this->title    = "My Shipping Method"; // This can be added as an setting but for this example its forced. 

        $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')); 
       } 

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

       public function calculate_shipping($package) { 
        session_start(); 
        global $woocommerce; 

        $carttotal = $woocommerce->cart->cart_contents_count; 

        /* 
         1-5 packs 5,000 
         6-10 packs 10,000 
         11-15 packs 15,000 
         16-20 packs 20,000 

        */ 
        if($carttotal < 20){ 
         $cost = 0;//Free delivery for above 20 packs 
        }else if($carttotal >= 16 && $carttotal <= 20){ 
         $cost = 20000; 
        }else if($carttotal >= 11 && $carttotal <= 15){ 
         $cost = 15000; 
        }else if($carttotal >= 6 && $carttotal <= 10){ 
         $cost = 10000; 
        }else if($carttotal >= 5 && $carttotal <= 1){ 
         $cost = 5000; 
        }else if($carttotal > 1){ 
         $cost = 0; 
        } 

        $rate = array(
         'id' => $this->id, 
         'label' => 'Shipping', 
         'cost' => $cost 
        ); 

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

       } 
      } 
     } 
    } 

    add_action('woocommerce_shipping_init', 'your_shipping_method_init'); 

    function add_your_shipping_method($methods) { 
     $methods['your_shipping_method'] = 'WC_Your_Shipping_Method'; 
     return $methods; 
    } 

    add_filter('woocommerce_shipping_methods', 'add_your_shipping_method'); 
} 

我從信用幫助:How to add custom shipping charge in woocommerce?得到了一些幫助,但現在我需要完成此操作。在此先感謝

回答

0

它看起來像你的calculate_shipping功能有一些問題

if($carttotal < 20){ 
    $cost = 0;//Free delivery for above 20 packs 

應該

if($carttotal > 20){ 

}else if($carttotal >= 5 && $carttotal <= 1){ 

是倒退,它讀取「大於或等於到5且小於或等於1「

應該

}else if($carttotal >= 1 && $carttotal <= 5){ 

也是你最後不然,如果這種說法是混亂的,的休息,如果公司應該已經覆蓋1和20 +

}else if($carttotal > 1){ 
    $cost = 0; 

之間的每一個數字,您可能只需要刪除那部分。

+0

感謝您的快速更正。非常感謝他們,但問題依然存在。對於我訂購的任意數量的物品,它在運送時返回值11 – omukiguy

0

我剛拆開的功能出了條件語句

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

的不要忘記在底部最後的右括號。我殺了幾個標準,即不能使一個插件,但只是在我的functions.php

所以我的functions.php添加

require get_template_directory() . '/inc/woo-shipping-methods.php'; 

而且/公司製造一個新的文件(避免混亂),以包含/宇航運methods.php文件

有這個代碼

<?php 

function your_shipping_method_init() { 
    if (! class_exists('WC_Your_Shipping_Method')) { 
     class WC_Your_Shipping_Method extends WC_Shipping_Method { 

      /*---------------------------------------------------------------- 
        Constructor for your shipping class 
      ----------------------------------------------------------------*/ 
      public function __construct() { 
       $this->id     = 'your_shipping_method'; // Id for your shipping method. Should be uunique. 
       $this->method_title  = __('Your Shipping Method'); // Title shown in admin 
       $this->method_description = __('Description of your shipping method'); // Description shown in admin 

       $this->enabled   = "yes"; // This can be added as an setting but for this example its forced enabled 
       $this->title    = "My Shipping Method"; // This can be added as an setting but for this example its forced. 

       $this->init(); 
      } 

      /*---------------------------------------------------------------- 
       Init your settings 
      ----------------------------------------------------------------*/ 
      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')); 
      } 

      /*---------------------------------------------------------------- 
       calculate_shipping function. 
      ----------------------------------------------------------------*/ 

      public function calculate_shipping($package) { 
       global $woocommerce; 

       $carttotal = $woocommerce->cart->cart_contents_count; 

       /* 
        1-5 packs 5,000 
        6-10 packs 10,000 
        11-15 packs 15,000 
        16-20 packs 20,000 

       */ 
       if($carttotal > 20){ 
        $cost = 0;//Free delivery for above 20 packs 
       }else if($carttotal >= 16 && $carttotal <= 20){ 
        $cost = 20000; 
       }else if($carttotal >= 11 && $carttotal <= 15){ 
        $cost = 15000; 
       }else if($carttotal >= 6 && $carttotal <= 10){ 
        $cost = 10000; 
       }else if($carttotal <= 5 && $carttotal >= 1){ 
        $cost = 5000; 
       } 

       $rate = array(
        'id' => $this->id, 
        'label' => 'Shipping', 
        'cost' => $cost, 
       ); 

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

      } 
     } 
    } 
} 

add_action('woocommerce_shipping_init', 'your_shipping_method_init'); 

function add_your_shipping_method($methods) { 
    $methods['your_shipping_method'] = 'WC_Your_Shipping_Method'; 
    return $methods; 
} 

add_filter('woocommerce_shipping_methods', 'add_your_shipping_method'); 

乾杯。

相關問題