自定义运输方式插件中的 WooCommerce 运输成本计算

mic*_*s90 4 php methods wordpress woocommerce shipping-method

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

\n\n
\n

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

\n
\n\n

这是我的整个插件:

\n\n
<?php \n/* *\n* Plugin Name: Min frakt modul\n  Description: Ett plugin som ber\xc3\xa4knar frakt kostnad beroende p\xc3\xa5 vikten\n*/\n\n/**\n * Check if WooCommerce is active\n */\n\nif (!defined(\'ABSPATH\')) {\n    exit;\n}\n\nif ( in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) ) {\n    function your_shipping_method_init() {\n        if ( ! class_exists( \'WC_Your_Shipping_Method\' ) ) {\n            class WC_Your_Shipping_Method extends WC_Shipping_Method {\n                /**\n                 * Constructor for your shipping class\n                 *\n                 * @access public\n                 * @return void\n                 */\n                public function __construct() {\n                    $this->id                 = \'mitt-plugin-frakt\'; // Id for your shipping method. Should be uunique.\n                    $this->method_title       = __( \'Min frakt plugin\' );  // Title shown in admin\n                    $this->method_description = __( \n                    \'Frakten skall ber\xc3\xa4knas p\xc3\xa5 totalvikten av kundvagnen.<br>\n                    Listan \xc3\xa4r f\xc3\xb6ljande:<br>\n                    < 1kg = 30kr <br>\n                    < 5kg = 60kr <br>\n                    < 10kg = 100kr <br>\n                    < 20kg = 200kr <br>\n                    > 20kg = (Vikten * 10)kr\' ); // Description shown in admin\n\n                    $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled\n                    $this->title              = "My Shipping Method"; // This can be added as an setting but for this example its forced.\n\n                    $this->init();\n                }\n\n                /**\n                 * Init your settings\n                 *\n                 * @access public\n                 * @return void\n                 */\n                function init() {\n                    // Load the settings API\n                    $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings\n                    $this->init_settings(); // This is part of the settings API. Loads settings you previously init.\n\n                    // Save settings in admin if you have any defined\n                    add_action( \'woocommerce_update_options_shipping_\' . $this->id, array( $this, \'process_admin_options\' ) );\n                }\n\n                /**\n                 * calculate_shipping function.\n                 *\n                 * @access public\n                 * @param mixed $package\n                 * @return void\n                 */\n                public function calculate_shipping( $package ) {\n\n                   $alla_items = $package[\'contents\'];\n                   $total_weight = 0;\n                    foreach ($alla_items as $orderline) {\n                        $product_weight = $orderline[\'data\']->weight;\n                        $total_weight += $product_weight;\n                    };\n\n                    if($total_weight < 1) {\n                        $cost = \'30\';\n                    } elseif($total_weight > 1 && $total_weight < 5) {\n                        $cost = \'60\';\n                    } elseif($total_weight > 5 && $total_weight < 10) {\n                        $cost = \'100\'; \n                    } elseif($total_weight > 10 && $total_weight < 20) {\n                        $cost = \'200\'; \n                    } elseif($total_weight > 20) {\n                        $cost = ($total_weight * 10);\n                    } else {\n                        echo \'ingen vikt\';\n                    }\n\n                    echo $total_weight;\n\n                    $rate = array( \n                        \'id\' => $this->id,\n                        \'label\' => $this->title,\n                        \'cost\' => $cost,\n                        \'calc_tax\' => \'per_item\'\n                    );\n\n                    // Register the rate\n                    $this->add_rate( $rate );\n                }\n            }\n        }\n    }\n\n\n\n    add_action( \'woocommerce_shipping_init\', \'your_shipping_method_init\' );\n\n    function add_your_shipping_method( $methods ) {\n        $methods[\'mitt-plugin-frakt\'] = \'WC_Your_Shipping_Method\';\n        return $methods;\n    }\n\n    add_filter( \'woocommerce_shipping_methods\', \'add_your_shipping_method\' );\n}\n\n?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

Vla*_*ert 9

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

public function calculate_shipping( $package ) 
Run Code Online (Sandbox Code Playgroud)

进入这个:

public function calculate_shipping( $package = array() )
Run Code Online (Sandbox Code Playgroud)