dongsui5464 2018-02-12 07:54
浏览 134
已采纳

在woocommerce 3中创建一种新的送货方式

I need help in generating new shipping method in woocommerce version 3+. The name for new field is "Nextday delivery". Like the flat rate it also need to be there in the method but it was not displayed in the drop down select field.

enter image description here

The below is the code which I tried. But it's not working for me.

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

I need it to come in the dropdown like flatrate free shipping etc but it was not comming in the dropdown.

  • 写回答

1条回答 默认 最新

  • dongxian8048 2018-02-13 15:50
    关注

    There were some missing things and others unnecessary. The correct way to make it work is:

    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;
    }
    

    Code goes in function.php file of your active child theme (active theme).

    Tested and works.


    Here the shipping method selector now displays this "Request a shipping coast" method:

    enter image description here

    Once selected and added, it's created this time:

    enter image description here

    If you edit it:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳