简体   繁体   English

基于重量的特定城市的woocommerce送货方法

[英]woocommerce shipping method for specific city based on weight

How to make shipping method based on weight for specific city? 如何根据特定城市的重量制定运输方式?

For example, I want to take the postage for city "A" for $1 per kg. 例如,我要为城市“ A”收取每公斤1美元的邮费。

Here is the code of plugin I made: 这是我制作的插件代码:

public function calculate_shipping( $package ) {
    $rate = array(
        'id' => $this->id,
        'label' => $this->title,
        'cost' => '10.99',
        'calc_tax' => 'per_item'
    );

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

Full code: 完整代码:

<?php
/*
Plugin Name: Trucking shipping
Plugin URI: http://abdhannan.com
Description: Trucking shipping dengan JNE dsb
Version: 1.0.0
Author: Abd Hannan
Author URI: http://abdhannan.com
*/

/**
 * Check if WooCommerce is active
 */
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    function trucking_shipping_init() {
        if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
            class WC_trucking_shipping_Method extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct() {
                    $this->id                 = 'trucking_shipping_method'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'JNE TRUCKING' );  // Title shown in admin
                    $this->method_description = __( 'Pengiriman dengan truck JNE' ); // Description shown in admin

                    $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                    $this->title              = "JNE TRUCKING"; // 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 ) {
                    $rate = array(
                        'id' => $this->id,
                        'label' => $this->title,
                        'cost' => '10.99',
                        'calc_tax' => 'per_item'
                    );

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

    add_action( 'woocommerce_shipping_init', 'trucking_shipping_init' );

    function add_trucking_shipping_method( $methods ) {
        $methods['trucking_shipping_method'] = 'WC_trucking_shipping_Method';
        return $methods;
    }

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

The problem is in calculate_shipping , I don't know how to do what I want. 问题是在calculate_shipping ,我不知道该怎么做。

This is not really a code question, rather a 'how do I use WooCommerce' question. 这实际上不是代码问题,而是“我如何使用WooCommerce”问题。 What you are looking for is functionality Automattic deliberately removed from WooCommerce so that they could charge for it, look here for the plugin. 您正在寻找的功能是故意从WooCommerce中删除的Automattic,以便他们可以付费, 在此处查找插件。

If you are frugal and reduce your product weights down to a specific set, then all you really need is flat rate shipping assigned to postal code defined shipping zones, mixed with shipping classes and boom no programming ever needed. 如果您比较节俭并且将产品重量减少到特定的范围,那么您真正需要的就是分配给邮政编码定义的运输区域的统一费率运输,而无需再进行编程就可以与运输类别混合使用。

Koda 幸田

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM