Php 用于插件开发的方法

Php 用于插件开发的方法,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我一直在无休止地尝试在下面的代码中添加一个shipping方法,但没有成功。请一位好心的撒玛利亚人提供一些帮助 我只需要添加一个运输方式 基本上,代码所做的是将字段添加到woo commerce设置| shipping zones中的shipping zones部分 我想添加一个[创建区域的统一费率] 我到处都找过了,但运气不好-( 您可以为此创建插件: 在wp content/pluigins/内创建一个名为“woocommerce fast delivery system”的文件夹 在“wo

我一直在无休止地尝试在下面的代码中添加一个shipping方法,但没有成功。请一位好心的撒玛利亚人提供一些帮助

我只需要添加一个运输方式

基本上,代码所做的是将字段添加到woo commerce设置| shipping zones中的shipping zones部分

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

我到处都找过了,但运气不好-(


您可以为此创建插件:

在wp content/pluigins/内创建一个名为“woocommerce fast delivery system”的文件夹

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


1.fast-delivery-shipping-method.php
我想使用您的方法,但是我可以根据邮政编码作为统一费率添加费用吗?我还想知道是否可以预定义方法。:-)谢谢您的时间。
<?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' );
<?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';
 }
}
<?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
            ));
          }
}