基于权重的特定城市的woocommerce运输方法

时间:2017-09-25 23:04:53

标签: php wordpress woocommerce

如何针对特定城市制定基于重量的运输方式?

例如,我想以每公斤1美元的价格购买城市“A”的邮资。

以下是我制作的插件代码:

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

完整代码:

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

问题出在calculate_shipping,我不知道如何做我想做的事。

1 个答案:

答案 0 :(得分:0)

这不是一个真正的代码问题,而是一个&#39;我如何使用WooCommerce&#39;题。您正在寻找的是Automattic故意从WooCommerce中删除的功能,以便他们可以为它收费,看看here插件。

如果您节俭并将产品重量降低到特定的设定值,那么您真正需要的是统一运费分配到邮政编码定义的运输区域,与运输类别混合,繁荣无需编程。