Php 如何在WooCommerce 3中添加自定义工作装运方法

Php 如何在WooCommerce 3中添加自定义工作装运方法,php,wordpress,woocommerce,shipping,extending,Php,Wordpress,Woocommerce,Shipping,Extending,我已经成功地创建了一种新的运送方法,并为运送区域提供了支持。但是,当我从下拉列表中选择方法将其添加到区域时,它不会出现在“选定方法列表”中 我录制了一段视频来演示: 我一辈子都搞不明白为什么它不起作用。如果我选择一种标准方法() 有人知道这里发生了什么,以及如何让它工作吗 以下是我的代码: 换行 public function calculate_shipping( $package ) { 这条线 公共函数calculate\u shipping($package=array()){在“w

我已经成功地创建了一种新的运送方法,并为运送区域提供了支持。但是,当我从下拉列表中选择方法将其添加到区域时,它不会出现在“选定方法列表”中

我录制了一段视频来演示:

我一辈子都搞不明白为什么它不起作用。如果我选择一种标准方法()

有人知道这里发生了什么,以及如何让它工作吗

以下是我的代码:

换行

public function calculate_shipping( $package ) {
这条线

公共函数calculate\u shipping($package=array()){

在“woocommerce\u shipping\u methods”上的方法键应该与shipping方法id匹配

就你而言: 你应该改变

function request_shipping_quote_shipping_method( $methods ) {
    $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method';

    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );
致:


WC\u Custom\u Shipping\u方法
是一个抽象类,您试图更改其继承的方法
calculate\u Shipping
哪些抽象类不允许

试着这样做

<?php

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

function request_a_shipping_quote_init() {
    class Abs_Custom_Shipping extends WC_Shipping_Method{}
    if ( ! class_exists( 'WC_Request_Shipping_Quote_Method' ) ) {
        class WC_Request_Shipping_Quote_Method extends Abs_Custom_Shipping {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct() {
                $this->id                 = 'request_a_shipping_quote'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'Request a Shipping Quote' );  // Title shown in admin
                $this->method_description = __( 'Shipping method to be used where the exact shipping amount needs to be quoted' ); // Description shown in admin

                $this->title = "Request a Shipping Quote"; // This can be added as an setting but for this example its forced.

                $this->supports = array(
                    'shipping-zones'
                );

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

            function init_form_fields() {

                $this->form_fields = array(

                    'enabled' => array(
                        'title'       => __( 'Enable', 'dc_raq' ),
                        'type'        => 'checkbox',
                        'description' => __( 'Enable this shipping method.', 'dc_raq' ),
                        'default'     => 'yes'
                    ),

                    'title' => array(
                        'title'       => __( 'Title', 'dc_raq' ),
                        'type'        => 'text',
                        'description' => __( 'Title to be displayed on site', 'dc_raq' ),
                        'default'     => __( 'Request a Quote', 'dc_raq' )
                    ),

                );

            }

            /**
             * calculate_shipping function.
             *
             * @access public
             *
             * @param mixed $package
             *
             * @return void
             */

            public function calculate_shipping( $packages = array() ) {
                $rate = array(
                    'id'       => $this->id,
                    'label'    => $this->title,
                    'cost'     => '0.00',
                    'calc_tax' => 'per_item'
                );

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

add_action( 'woocommerce_shipping_init', 'request_a_shipping_quote_init' );

function request_shipping_quote_shipping_method( $methods ) {
    $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method';

    return $methods;
}

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

在我尝试使用有问题的代码并修复我在这些帖子的评论中发现的所有错误后,我仍然有一些问题。例如,即使我成功地将其添加到shipping zone,我也无法编辑shipping方法

最后,我得到了所需的代码,为我工作后,编辑标准的免费送货Woocommerce方法。希望这将节省时间的人

function request_a_shipping_quote_init() {
    if ( ! class_exists( 'Imp_WC_Shipping_Local_Pickup' ) ) {

        class Imp_WC_Pickup_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor.
             *
             * @param int $instance_id
             */
            public function __construct( $instance_id = 0 ) {
                $this->id           = 'imp_pickup_shipping_method';
                $this->instance_id  = absint( $instance_id );
                $this->method_title = __( "Самовывоз из точки выдачи ( MO г. Дзержинский )", 'imp' );
                $this->supports     = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                );
                $this->init();
            }

            /**
             * Initialize custom shiping method.
             */
            public function init() {

                // Load the settings.
                $this->init_form_fields();
                $this->init_settings();

                // Define user set variables
                $this->title = $this->get_option( 'title' );

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

            /**
             * Calculate custom shipping method.
             *
             * @param array $package
             *
             * @return void
             */
            public function calculate_shipping( $package = array() ) {
                $this->add_rate( array(
                    'label'   => $this->title,
                    'package' => $package,
                ) );
            }

            /**
             * Init form fields.
             */
            public function init_form_fields() {
                $this->instance_form_fields = array(
                    'title' => array(
                        'title'       => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),
                        'type'        => 'text',
                        'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                        'default'     => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),
                        'desc_tip'    => true,
                    ),
                );
            }
        }
    }
}
add_action( 'woocommerce_shipping_init', 'request_a_shipping_quote_init' );

function request_shipping_quote_shipping_method( $methods ) {
    $methods['imp_pickup_shipping_method'] = 'Imp_WC_Pickup_Shipping_Method';

    return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );

我遇到了这个问题,这让我疯狂了好几天,直到在查看Woocommerce代码以了解发生了什么时,我发现在为Woocommerce_shipping_方法设置筛选方法时,我需要使添加到此数组的条目的索引与我的shippi中的ID属性相同ng方法类。一旦我这样做了,它添加了shipping方法fine,并正确地显示了区域。以前,我一直在过滤器方法中添加条目,但没有索引,这在WC看到该方法时效果很好,这就是它看起来正常的原因。但是,保存设置的代码使用ID作为I的索引确定运输方式。从其他评论中,我可以想象这个特定的索引是在WC版本3中添加的。希望这会有所帮助。

如果您的运输方式仍然不起作用,您必须确保

  • 实例id必须在构造函数中定义,如本代码段所示
  • 公共函数构造($instance\u id=0) { $this->instance\u id=absint($instance\u id); //其他线路如下 }
  • 没有过时数据:删除临时数据和客户端数据(WooCommerce设置>状态>工具)

  • 这在woocommerce 3+中不再有效,请参阅此未回答的支持线程:与WC_Shipping_方法冲突
    calculate_Shipping()
    core方法和代码插件中定义的方法…这是需要解决的问题。因为引发了此错误:严格标准:WC\u请求声明\u Shipping\u Quote\u方法::calculate\u Shipping()应与WC\u Shipping\u方法::calculate\u Shipping($package=Array)兼容在/www/wp content/plugins/request_shipping_quote_method.php在线18@LoicTheAztec成功了吗?我最终不得不放弃这条路线,因为我无法让它工作。相反,我重新设计了货到付款的运输方法,将其重新标记为“计算运输”,与其他一些定制部件一起使用。不是最整洁的solu但它在生产站点上运行良好。我确信有一种解决方案可以与定制的运送方法一起工作,只是在我拥有的时间内无法使用。谢谢。我确实实现了该代码,但它仍然不工作。该站点当前处于远程,因此我看不到抛出了什么错误,将在本地运行我看看是否能看到任何错误。所以当我添加
    $package=array()时
    现在不会抛出任何错误,但行为保持不变-装运方法不会添加到装运方法列表中。您会遇到什么错误?bcoz现在您必须检查您在函数中如何处理包,bcoz现在是包数组它是否在任何地方记录了包数组的结构,以便你知道如何正确实施吗?你能给我看看你的calculate_shipping()吗函数代码?只是刚刚发现了这个回复-修复了它!非常感谢您的回答,非常感谢这帮助我使我的传送方法正常工作。构造函数方法上缺少$instance\u id,并且在删除或处理此传送方法时没有引起意外行为。这也帮助了我。请再次使用实例\u id是必需的。在设置支撑之前,我也无法让它显示。
    <?php
    
        if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
    function request_a_shipping_quote_init() {
        class Abs_Custom_Shipping extends WC_Shipping_Method{}
        if ( ! class_exists( 'WC_Request_Shipping_Quote_Method' ) ) {
            class WC_Request_Shipping_Quote_Method extends Abs_Custom_Shipping {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct() {
                    $this->id                 = 'request_a_shipping_quote'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'Request a Shipping Quote' );  // Title shown in admin
                    $this->method_description = __( 'Shipping method to be used where the exact shipping amount needs to be quoted' ); // Description shown in admin
    
                    $this->title = "Request a Shipping Quote"; // This can be added as an setting but for this example its forced.
    
                    $this->supports = array(
                        'shipping-zones'
                    );
    
                    $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' ) );
                }
    
                function init_form_fields() {
    
                    $this->form_fields = array(
    
                        'enabled' => array(
                            'title'       => __( 'Enable', 'dc_raq' ),
                            'type'        => 'checkbox',
                            'description' => __( 'Enable this shipping method.', 'dc_raq' ),
                            'default'     => 'yes'
                        ),
    
                        'title' => array(
                            'title'       => __( 'Title', 'dc_raq' ),
                            'type'        => 'text',
                            'description' => __( 'Title to be displayed on site', 'dc_raq' ),
                            'default'     => __( 'Request a Quote', 'dc_raq' )
                        ),
    
                    );
    
                }
    
                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 *
                 * @param mixed $package
                 *
                 * @return void
                 */
    
                public function calculate_shipping( $packages = array() ) {
                    $rate = array(
                        'id'       => $this->id,
                        'label'    => $this->title,
                        'cost'     => '0.00',
                        'calc_tax' => 'per_item'
                    );
    
                    // Register the rate
                    $this->add_rate( $rate );
                }
            }
        }
    }
    
    add_action( 'woocommerce_shipping_init', 'request_a_shipping_quote_init' );
    
    function request_shipping_quote_shipping_method( $methods ) {
        $methods['request_shipping_quote_shipping_method'] = 'WC_Request_Shipping_Quote_Method';
    
        return $methods;
    }
    
    add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );
    }
    
    function request_a_shipping_quote_init() {
        if ( ! class_exists( 'Imp_WC_Shipping_Local_Pickup' ) ) {
    
            class Imp_WC_Pickup_Shipping_Method extends WC_Shipping_Method {
                /**
                 * Constructor.
                 *
                 * @param int $instance_id
                 */
                public function __construct( $instance_id = 0 ) {
                    $this->id           = 'imp_pickup_shipping_method';
                    $this->instance_id  = absint( $instance_id );
                    $this->method_title = __( "Самовывоз из точки выдачи ( MO г. Дзержинский )", 'imp' );
                    $this->supports     = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );
                    $this->init();
                }
    
                /**
                 * Initialize custom shiping method.
                 */
                public function init() {
    
                    // Load the settings.
                    $this->init_form_fields();
                    $this->init_settings();
    
                    // Define user set variables
                    $this->title = $this->get_option( 'title' );
    
                    // Actions
                    add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
                }
    
                /**
                 * Calculate custom shipping method.
                 *
                 * @param array $package
                 *
                 * @return void
                 */
                public function calculate_shipping( $package = array() ) {
                    $this->add_rate( array(
                        'label'   => $this->title,
                        'package' => $package,
                    ) );
                }
    
                /**
                 * Init form fields.
                 */
                public function init_form_fields() {
                    $this->instance_form_fields = array(
                        'title' => array(
                            'title'       => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),
                            'type'        => 'text',
                            'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                            'default'     => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),
                            'desc_tip'    => true,
                        ),
                    );
                }
            }
        }
    }
    add_action( 'woocommerce_shipping_init', 'request_a_shipping_quote_init' );
    
    function request_shipping_quote_shipping_method( $methods ) {
        $methods['imp_pickup_shipping_method'] = 'Imp_WC_Pickup_Shipping_Method';
    
        return $methods;
    }
    add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );
    
    public function __construct($instance_id = 0) { $this->instance_id = absint($instance_id); // other lines follow }