【问题标题】:woocommerce shipping method for specific city based on weight基于重量的特定城市的woocommerce运输方式
【发布时间】:2017-09-26 16:54:15
【问题描述】:

如何根据具体城市的重量制定运输方式?

例如,我想以每公斤 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,不知道怎么办。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

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

    如果您节俭并将您的产品重量减少到特定的一组,那么您真正需要的是分配到邮政编码定义的运输区域的统一费率运输,混合运输类别并且无需编程。

    柯达

    【讨论】:

    • 是的,没错,我有使用表格费率插件,但是,我必须为所有运输区创建,我的国家有很多 zipping 区(500 多个),所以,我想要运输方式自动计算条件,数据从 json 文件中获取。任何人都可以帮助我吗?
    • 如果您尝试实施 500 个运输区域,那么您做错了。您应该始终尽力简化每次运输。例如,对于邮政编码,您应该使用范围。
    • 嗯,你是不是不可能根据要求制作我自己的插件?如果这里有参考贴,请。
    • 您最终将重新编码表费率运输伙伴,使用可用和有效的东西会容易得多。
    • 好的,我将使用表运费,谢谢@Kodaloid
    猜你喜欢
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多