【问题标题】:Custom WooCommerce Shipping Method自定义 WooCommerce 运输方式
【发布时间】:2022-11-02 16:36:15
【问题描述】:

我有这个工作代码,可以在 woocommerce 设置 > 运输区域下正确生成和保存自定义运输方法。

我的问题是我无法在结帐页面上显示运输方式。

任何有关如何解决此问题并可能扩展代码的帮助将不胜感激。

add_filter('woocommerce_shipping_methods', 'add_local_shipping');
function add_local_shipping($methods) {
    $methods['local_shipping'] = 'Local_Shipping_Method';
    return $methods;
}

class Local_Shipping_Method extends WC_Shipping_Method {

            public function __construct($instance_id = 0) {
                $this->id = 'local_shipping';
                $this->instance_id = absint($instance_id);
                $this->domain = 'local_shipping';
                $this->method_title = __('Pickup', $this->domain);
                $this->method_description = __('Pickup Location for WooCommerce', $this->domain);
                $this->title = __('Pickup Location', $this->domain);
                $this->supports = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                );
            
                $this->instance_form_fields = array(
                    'enabled' => array(
                    'title'         => __( 'Enable/Disable' ),
                    'type'          => 'checkbox',
                    'label'         => __( 'Enable this shipping method' ),
                    'default'       => 'yes',
                ),
                    'title' => array(
                    'title'         => __( 'Method Title' ),
                    'type'          => 'text',
                    'description'   => __( 'This controls the title which the user sees during checkout.' ),
                    'default'       => __( 'Pickup Location' ),
                    'desc_tip'      => true
                ),
                    'tax_status' => array(
                    'title'   => __( 'Tax status', 'woocommerce' ),
                    'type'    => 'select',
                    'class'   => 'wc-enhanced-select',
                    'default' => 'taxable',
                    'options' => array(
                        'taxable' => __( 'Taxable', 'woocommerce' ),
                        'none'    => _x( 'None', 'Tax status', 'woocommerce' ),
                    ),
                ),
                    'cost'       => array(
                    'title'       => __( 'Cost', 'woocommerce' ),
                    'type'        => 'text',
                    'placeholder' => '0',
                    'description' => __( 'Optional cost for pickup.', 'woocommerce' ),
                    'default'     => '',
                    'desc_tip'    => true,
                ),
            );

                $this->enabled = $this->get_option( 'enabled' );
                $this->title   = __('Pickup Location', $this->domain);

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

    public function calculate_shipping( $package = array() ) {
        $this->add_rate( array(
            'id'    => $this->id . $this->instance_id,
            'label' => $this->title,
            'cost'  => 0,
    ) );
    } 
}

【问题讨论】:

    标签: php woocommerce hook-woocommerce


    【解决方案1】:

    您尝试的方式是错误的。并且不建议将文本域保存在变量或方法中。

    创建自定义运输方式您必须按照我在您的代码上更正的这些步骤进行操作。

    希望这可以帮助。您可以了解更多关于如何创建新的送货方式here。随意

    // To initialize your new shipping method you have to keep it in function 
    function local_shipping_init() {
        if ( ! class_exists( Local_Shipping_Method ) ) {
            class Local_Shipping_Method extends WC_Shipping_Method {
    
                public function __construct( $instance_id = 0 ) {
                    $this->id                 = 'local_shipping';
                    $this->instance_id        = absint( $instance_id );
                    $this->method_title       = __( 'Pickup', 'text-domain' );
                    $this->method_description = __( 'Pickup Location for WooCommerce', 'text-domain' );
                    $this->title              = __( 'Pickup Location', 'text-domain' );
                    $this->supports           = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );
    
                    // then you have to call this method to initiate your settings
                    $this->init();
                }
    
    
                public function init() {
                    // this method used to initiate your fields on settings 
                    $this->init_form_fields();
                    // this is settings instance where you can declar your settings field 
                    $this->init_instance_settings();
                    
                    // user defined values goes here, not in construct 
                    $this->enabled = $this->get_option( 'enabled' );
                    $this->title   = __( 'Pickup Location', 'text-domain' );
                    
                    // call this action in init() method to save your settings at the backend
                    add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
                }
    
                public function init_instance_settings() {
                    // you have to keep all the instance settings field inside the init_instance_settings method 
                    $this->instance_form_fields = array(
                        'enabled'    => array(
                            'title'   => __( 'Enable/Disable' ),
                            'type'    => 'checkbox',
                            'label'   => __( 'Enable this shipping method' ),
                            'default' => 'yes',
                        ),
                        'title'      => array(
                            'title'       => __( 'Method Title' ),
                            'type'        => 'text',
                            'description' => __( 'This controls the title which the user sees during checkout.' ),
                            'default'     => __( 'Pickup Location' ),
                            'desc_tip'    => true
                        ),
                        'tax_status' => array(
                            'title'   => __( 'Tax status', 'woocommerce' ),
                            'type'    => 'select',
                            'class'   => 'wc-enhanced-select',
                            'default' => 'taxable',
                            'options' => array(
                                'taxable' => __( 'Taxable', 'woocommerce' ),
                                'none'    => _x( 'None', 'Tax status', 'woocommerce' ),
                            ),
                        ),
                        'cost'       => array(
                            'title'       => __( 'Cost', 'woocommerce' ),
                            'type'        => 'text',
                            'placeholder' => '0',
                            'description' => __( 'Optional cost for pickup.', 'woocommerce' ),
                            'default'     => '',
                            'desc_tip'    => true,
                        ),
                    );
                }
    
                public function calculate_shipping( $package = array() ) {
                    $this->add_rate( array(
                        'id'    => $this->id, // you should define only your shipping method id here
                        'label' => $this->title,
                        'cost'  => 0,
                    ) );
                }
            }
        }
    
    }
    add_action( 'woocommerce_shipping_init', 'local_shipping_init' ); // use this hook to initialize your new custom method 
    
    function add_local_shipping( $methods ) {
        $methods['local_shipping'] = 'Local_Shipping_Method';
    
        return $methods;
    }
    add_filter( 'woocommerce_shipping_methods', 'add_local_shipping' );
    

    该代码经过测试并且工作正常。请参阅下面的屏幕截图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2021-06-07
      • 2020-08-07
      • 2018-03-17
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多