【问题标题】:WooCommerce shipping cost calculation in custom shipping method plugin [duplicate]自定义运输方式插件中的 WooCommerce 运输成本计算 [重复]
【发布时间】:2020-08-07 21:45:11
【问题描述】:

我正在尝试为 WooCommerce 创建一个插件,该插件根据产品重量计算运费。我不断收到此错误:

警告:WC_Your_Shipping_Method::calculate_shipping($package) 的声明应与 C:\Users\Michelle\Dropbox\MEDIEINSTITUTET\Utveckling mot e-handel\WooCommerce\WC 中的 WC_Shipping_Method::calculate_shipping($package = Array) 兼容-kurs\wp-content\plugins\frakt\frakt.php 在第 18 行

这是我的整个插件:

<?php 
/* *
* Plugin Name: Min frakt modul
  Description: Ett plugin som beräknar frakt kostnad beroende på vikten
*/

/**
 * Check if WooCommerce is active
 */

if (!defined('ABSPATH')) {
    exit;
}

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    function your_shipping_method_init() {
        if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
            class WC_Your_Shipping_Method extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct() {
                    $this->id                 = 'mitt-plugin-frakt'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'Min frakt plugin' );  // Title shown in admin
                    $this->method_description = __( 
                    'Frakten skall beräknas på totalvikten av kundvagnen.<br>
                    Listan är följande:<br>
                    < 1kg = 30kr <br>
                    < 5kg = 60kr <br>
                    < 10kg = 100kr <br>
                    < 20kg = 200kr <br>
                    > 20kg = (Vikten * 10)kr' ); // Description shown in admin

                    $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                    $this->title              = "My Shipping Method"; // 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 ) {

                   $alla_items = $package['contents'];
                   $total_weight = 0;
                    foreach ($alla_items as $orderline) {
                        $product_weight = $orderline['data']->weight;
                        $total_weight += $product_weight;
                    };

                    if($total_weight < 1) {
                        $cost = '30';
                    } elseif($total_weight > 1 && $total_weight < 5) {
                        $cost = '60';
                    } elseif($total_weight > 5 && $total_weight < 10) {
                        $cost = '100'; 
                    } elseif($total_weight > 10 && $total_weight < 20) {
                        $cost = '200'; 
                    } elseif($total_weight > 20) {
                        $cost = ($total_weight * 10);
                    } else {
                        echo 'ingen vikt';
                    }

                    echo $total_weight;

                    $rate = array( 
                        'id' => $this->id,
                        'label' => $this->title,
                        'cost' => $cost,
                        'calc_tax' => 'per_item'
                    );

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



    add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

    function add_your_shipping_method( $methods ) {
        $methods['mitt-plugin-frakt'] = 'WC_Your_Shipping_Method';
        return $methods;
    }

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

?>

我真的找不到错误...当我尝试代码时,我只看到“Mici Shipping”但根本没有价格,这意味着它没有读取 if else 语句? 提前感谢您的帮助!

【问题讨论】:

    标签: php wordpress methods woocommerce shipping-method


    【解决方案1】:

    您的类 WC_Your_Shipping_Method 扩展了 WC_Shipping_Method 已经具有该方法。 转这个:

    public function calculate_shipping( $package ) 
    

    进入这个:

    public function calculate_shipping( $package = array() )
    

    【讨论】:

    • 谢谢!!现在一切正常!我还有另一个问题,但如果你超级善良 :p 该插件有效,但只有当它的产品不同时,如果我在同一个产品上有不同的数量,它就不会计算?知道为什么吗?
    • 举个例子。例如,您有一辆数量为 1 的自行车和一辆数量为 2 的自行车?比如,同一件商品两次,但数量不同?
    • 我正在用一个吉他网站测试我的插件,例如 - 1 把电吉他(重量:2 公斤) - 3 把原声吉他(每个重量:1 公斤)总重量应该在 5 公斤以上,但是是3kg,因为它不计算原声吉他kg 3次,只是一次
    • 您能否尝试看看问题是否在于它可能只计算原声吉他?尝试使用 1 个电动(2kg)和 2 个声学(1kg * 2)。你的总重量应该是 4。如果总重量是 3,这意味着它计算 1 个电动和 1 个声学。如果它只计算声学,它应该返回 2。我们可以在弄清楚这一点后解决这个问题
    • 您也可以发布“包”数组的 var_dump 吗?
    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2015-01-12
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多