【问题标题】:Error in Woocommerce When Trying to Create Freight Plugin尝试创建货运插件时 Woocommerce 中的错误
【发布时间】:2019-11-21 10:15:19
【问题描述】:

我正在尝试在 Woocommerce 中开发一个自定义运费计算插件,但是我无法返回客户输入的 zip 来激活我的公式。在这个例子中,我只放了一个基本公式来测试,但它导致错误“致命错误:调用未定义的方法 Test_Shipping_Br::get_items_needing_shipping() in ...\wp-content\plugins\woocommerce\includes\class- wc-cart.php 在第 1316 行”,当您在添加产品后进入购物车时。

我需要获取客户输入的 zip 和购物车中的包裹信息(数量、重量、高度、宽度、长度),并根据这些信息制定一个特定的公式。我开始了一个基本公式只是试图获取 zip,但它已经导致了我上面报告的错误。

<?php
/*
Plugin Name: Test Shipping
Plugin URI: 
Description: 
Version: 1.0.0
Author: Wendell Christian
Author URI: 
*/

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

function Test_Shipping_Br_init() {
    if ( ! class_exists( 'Test_Shipping_Br' ) ) {
        class Test_Shipping_Br extends WC_Shipping_Method {

            public function __construct() {
                $this->id                 = 'Test_Shipping_Br'; 
                $this->method_title       = __( 'Test Shipping Brazil' );  
                $this->method_description = __( '' ); 

                $this->enabled            = "yes"; 
                $this->title              = "Test Shipping Brazil"; 

                $this->init();
            }

            function init() {
                $this->init_form_fields();
                $this->init_settings();

                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
            }


            public function calculate_shipping( $package ) {

                $array = WC_Cart::get_shipping_packages();                  
                $postcode = $array[0]['destination']['postcode'];


                if ($postcode >= 75960000 && $postcode <= 75969999)
                {
                    $cost = 17;

                }


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

                $this->add_rate( $rate );
            }
        }
    }
}

add_action( 'woocommerce_shipping_init', 'Test_Shipping_Br_init' );

function add_Test_Shipping_Br( $methods ) {
    $methods[] = 'Test_Shipping_Br';
    return $methods;
}

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

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您的插件示例中似乎缺少 get_items_needing_shipping。

    这也是错误所暗示的。

    函数存在吗?

    【讨论】:

    • 所有插件代码都是我贴的。我是开发 Wordpress 的新手,我在文档中找不到调用返回 zip、重量、高度、宽度和长度值的函数的方法。
    • 但错误清楚地表明缺少方法/功能。如果这是所有代码,您可以放心地假设您的插件中缺少一个方法。所以研究它并添加它或制作一些东西来排除它。
    • 我声明$array = WC_Cart::get_shipping_packages();继承Woocommerce方法,找不到不工作的原因。
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多