【问题标题】:Create a new shipping method in woocommerce 3在 woocommerce 3 中创建新的运输方式
【发布时间】:2018-07-22 08:15:33
【问题描述】:

我需要帮助以在 woocommerce 版本 3+ 中生成新的运输方式。新字段的名称是“次日交货”。与统一费率一样,它也需要存在于方法中,但它没有显示在下拉选择字段中。

以下是我尝试过的代码。但这对我不起作用。

    function request_a_shipping_quote_init() {
        if ( ! class_exists( 'WC_Request_Shipping_Quote_Method' ) ) {
            class WC_Request_Shipping_Quote_Method extends WC_Shipping_Method {

                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();
                }
function init() {
                    $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.


                    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' )
                        ),

                    );

                }

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


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

我需要它出现在下拉菜单中,例如统一免费送货等,但它没有出现在下拉菜单中。

【问题讨论】:

  • 这里有一个可能有帮助的解决方案stackoverflow.com/questions/45177226/… 互联网上有很多文章展示了如何做到这一点。
  • @ Andrew Schultz 我试过它在 3.3 版中不起作用
  • 除非您发布代码,否则没有人会提供帮助。
  • @LoicTheAztec 我已经添加了我试图在下拉列表中获取它的代码。

标签: php wordpress methods woocommerce shipping


【解决方案1】:

有些东西丢失了,有些东西是不必要的。使其工作的正确方法是:

add_action('woocommerce_shipping_init', 'request_shipping_quote_method');
function request_shipping_quote_method() {

    if ( ! class_exists( 'WC_Request_Shipping_Quote_Method' ) ) {
        class WC_Request_Shipping_Quote_Method extends WC_Shipping_Method {

            public function __construct( $instance_id = 0) {
                $this->id = 'request_shipping_quote';
                $this->instance_id = absint( $instance_id );
                $this->domain = 'rasq';
                $this->method_title = __( 'Request a Shipping Quote', $this->domain );
                $this->method_description = __( 'Shipping method to be used where the exact shipping amount needs to be quoted', $this->domain );
                $this->supports = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                );
                $this->init();
            }

            ## Load the settings API
            function init() {
                $this->init_form_fields();
                $this->init_settings();
                $this->enabled = $this->get_option( 'enabled', $this->domain );
                $this->title   = $this->get_option( 'title', $this->domain );
                $this->info    = $this->get_option( 'info', $this->domain );
                add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
             }

            function init_form_fields() {
                $this->instance_form_fields = array(
                    'title' => array(
                        'type'          => 'text',
                        'title'         => __('Title', $this->domain),
                        'description'   => __( 'Title to be displayed on site.', $this->domain ),
                        'default'       => __( 'Request a Quote ', $this->domain ),
                    ),
                    'cost' => array(
                        'type'          => 'text',
                        'title'         => __('Coast', $this->domain),
                        'description'   => __( 'Enter a cost', $this->domain ),
                        'default'       => '',
                    ),
                );
            }

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

add_filter('woocommerce_shipping_methods', 'add_request_shipping_quote');
function add_request_shipping_quote( $methods ) {
    $methods['request_shipping_quote'] = 'WC_Request_Shipping_Quote_Method';
    return $methods;
}

代码进入您的活动子主题(活动主题)的 function.php 文件中。

经过测试并且有效。


这里的运输方式选择器现在显示这个“请求运输海岸”方法:

一旦选择并添加,这次就创建了:

如果你编辑它:

【讨论】:

  • @J.Shabu 当然可以 :) ...我总是在发布任何内容之前对其进行测试...
  • 非常感谢!! 支持信息是我工作良好所缺少的!
  • 你拯救了我的一天
  • 如何在 calculate_shipping 方法中获得“成本”? ...我假设 $this->cost ...对吗?
  • @Florin 只需查看 calculate_shipping() 方法 in here 的 WooCommerce "flat_rate" 运输源代码
猜你喜欢
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 2020-08-12
  • 2016-08-12
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多