插件开发的WooCommerce送货方法

时间:2016-10-24 02:03:09

标签: php wordpress woocommerce

我一直在尝试为以下代码添加送货方式,但没有运气。一个好的撒玛利亚人可以提供一些帮助。

我只需要添加送货方式。

基本上代码的作用是将字段添加到woo commerce setting |中的shipping zone部分运输区。

我想将[统一费率添加到创建的区域]

我到处搜索但没有运气。 : - (

<?php


 //Creating the Zone.

  function DW_shipping_zone_init() {



   // Getting the Shipping object
  $available_zones = WC_Shipping_Zones::get_zones();
  // Get all WC Countries
  $all_countries  = WC()->countries->get_countries();
  //Array to store available names
  $available_zones_names = array();
  // Add each existing zone name into our array
  foreach ($available_zones as $zone ) {
   if( !in_array( $zone['zone_name'], $available_zones_names ) ) {
     $available_zones_names[] = $zone['zone_name'];

   }
  }

  // Check if our zone 'C' is already there
  if( ! in_array( 'C', $available_zones_names ) ){
   // Create an empty object
   $zone_za = new stdClass();
   //Add null attributes
   $zone_za->zone_id = null;
   $zone_za->zone_order =null;
   // Add shipping zone name
   $zone_za->zone_name = 'C';
   // Instantiate a new shipping zone with our object
   $new_zone_za = new WC_Shipping_Zone( $zone_za );
   // Add South Africa as location
   $new_zone_za->add_location( 'ZA', 'country' );
   // Save the zone, if non existent it will create a new zone
   $new_zone_za->save();
   // Add our shipping method to that zone
   $new_zone_za->add_shipping_method( 'some_shipping_method' );
  }

add_action( 'init', 'DW_shipping_zone_init' );

1 个答案:

答案 0 :(得分:1)

您可以为此创建插件:

在wp-content / pluigins /中创建一个文件夹,名称为“woocommerce-fast-delivery-system”

在“woocommerce-fast-delivery-system”文件夹中,创建两个文件,如下所示:

1。快速递送航运method.php

<?php 
/*
Plugin Name: WooCommerce Fast Delivery Shipping Method
*/
/**
 * Check if WooCommerce is active
 */
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
if ( in_array( 'woocommerce/woocommerce.php', $active_plugins) ) {
 add_filter( 'woocommerce_shipping_methods', 'add_fast_delivery_shipping_method' );
 function add_fast_delivery_shipping_method( $methods ) {
   $methods[] = 'WC_Fast_Delivery_Shipping_Method';
   return $methods;
 }
 add_action( 'woocommerce_shipping_init', 'fast_delivery_shipping_method_init' );
 function fast_delivery_shipping_method_init(){
   require_once 'class-fast-delivery-shipping-method.php';
 }
}

2。类快速递送航运method.php

<?php

class WC_Fast_Delivery_Shipping_Method extends WC_Shipping_Method{

          public function __construct(){
                $this->id = 'fast_delivery_shipping_method';
                  $this->method_title = __( 'Fast Delivery Shipping Method', 'woocommerce' );

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


                  // Define user set variables
                  $this->enabled        = $this->get_option( 'enabled' );
                  $this->title                 = $this->get_option( 'title' );


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

          public function init_form_fields(){
                  $this->form_fields = array(
                    'enabled' => array(
                      'title'                 => __( 'Enable/Disable', 'woocommerce' ),
                      'type'                         => 'checkbox',
                      'label'                 => __( 'Enable Fast Delivery Shipping', 'woocommerce' ),
                      'default'                 => 'yes'
                    ),
                    'title' => array(
                      'title'                 => __( 'Method Tittle', 'woocommerce' ),
                      'type'                         => 'text',
                      'description'         => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                      'default'                => __( 'Fast Delivery Shipping', 'woocommerce' ), 
                    )
                );
          }
          public function is_available( $package ){
                  foreach ( $package['contents'] as $item_id => $values ) {
              $_product = $values['data'];
              $weight =        $_product->get_weight();
              if($weight > 10){
                      return false;
              }
                  }
                  return true;
          }
          public function calculate_shipping($package){

                  //get the total weight and dimensions

            $weight = 0;
            $dimensions = 0;
            foreach ( $package['contents'] as $item_id => $values ) {
              $_product  = $values['data'];
              $weight =        $weight + $_product->get_weight() * $values['quantity'];
              $dimensions = $dimensions + (($_product->length * $values['quantity']) * $_product->width * $_product->height);

            } 
            //calculate the cost according to the table
            switch ($weight) {
                case ($weight < 1):
                  switch ($dimensions) {        
                    case ($dimensions <= 1000):
                    $cost = 3;
                    break;
                    case ($dimensions > 1000):
                    $cost = 4;
                    break;
                  }
                 break;
                case ($weight >= 1 && $weight < 3 ):
                  switch ($dimensions) {        
                    case ($dimensions <= 3000):
                    $cost = 10;
                    break;
                  }
                break;
                case ($weight >= 3 && $weight < 10):
                  switch ($dimensions) {        
                    case ($dimensions <= 5000):
                    $cost = 25;
                    break;
                    case ($dimensions > 5000):
                    $cost = 50;
                    break;
                  }
                 break;        
              }
            // send the final rate to the user. 
           $c= $this->add_rate( array(
              'id'         => $this->id,
              'label' => $this->title,
              'cost'         => $cost
            ));
          }
}