1

我正在尝试为 woocommerce 创建一个插件,我需要创建一个新的运输方式作为其中的一部分。我找到了这个网站并按照它的说明进行操作。我刚刚更改了一些名称。

这是我的代码:

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    function hGhPishtazShippingMethod(){
        if (!class_exists('hGhPishtazShippingMethodClass')){
            class hGhPishtazShippingMethodClass extends WC_Shipping_Method {
                function __construct(){
                    $this->id = 'hGhPishtazShiping';
                    $this->method_title = __( 'pishtaz post' ) ;
                    $this->method_description = __( 'sending goods with pishtaz post' );

                    // Available country
                    //$this->availability = 'including';
                    //$this->countries = array('US');

                    // Create from and settings
                    $this->init();
                    $this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'yes';
                    $this->title = isset ($this->settings['title']) ? $this->settings['title'] : __( 'pishtaz post' );
                }
                // Init your setting
                function init(){
                    $this->init_form_fields();
                    $this->init_settings();
                    add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
                }

                function init_form_fields(){
                    // Our settings
                    $this->form_fields = array (
                        'enabled' => array(
                            'title' => __( 'Enable' , 'woocommerce' ),
                            'type' => 'checkbox',
                            'description' =>__( 'enable pishtaz post' , 'woocommerce' ),
                            'default' => 'yes'
                        ),
                        'title' => array(
                            'title' => __( 'Title' , 'woocommerce' ),
                            'type' => 'text',
                            'description' => __( 'Title to be shown on the site', 'woocommerce' ),
                            'default' => __( 'pishtaz post', 'woocommerce' )
                        )
                    );
                }

                function calculate_shipping ($package){
                    // Our calculation
                }
            }
        }
    }
    add_action( 'woocommerce_shipping_init', 'hGhPishtazShippingMethod' );

    function add_hGhPishtazShippingMethod( $methods ) {
        $methods[] = 'hGhPishtazShippingMethodClass';
        return $methods;
    }
    add_filter( 'woocommerce_shipping_methods', 'add_hGhPishtazShippingMethod' );
}

现在在 WooCommerce > Settings > Shipping 我看到我的方法名称,但是当我点击它时,我看到一个带有“保存更改”按钮的空表单。我已经检查了几次代码,但我没有发现有什么问题。

第二个问题:正如你所看到的,这段代码是一个完整的插件。我想把它作为另一个插件的一部分。正确的方法是什么?


更新:

我找到了第一个问题的答案:类 id 不能包含大写字母。

4

1 回答 1

1

您的代码中存在一些错误和错误……此外,您应该尽量避免在 php 中的函数名称、变量名称和 slug 名称上使用大写字母(与 Javascript 或其他脚本语言不同)。

请尝试以下操作:

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

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

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

                public function __construct( $instance_id = 0 ){
                    $this->id = 'pishtaz';
                    $this->domain = 'pishtaz_post';
                    $this->instance_id = absint( $instance_id );
                    $this->method_title = __( 'Pishtaz post', $this->domain ) ;
                    $this->method_description = sprintf( __( 'sending goods with %s', $this->domain ), $this->method_title );

                    $this->supports = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );

                    //available country
                    //$this->availability = 'including';
                    //$this->countries = array('US');


                    //create from and settings
                    $this->init();
                }

                //init your setting
                public 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') );

                }

                public function init_form_fields(){
                    //our settings
                    $this->form_fields = array (
                        'title' => array(
                            'title'         => __( 'Title' , $this->domain ),
                            'type'          => 'text',
                            'description'   => __( 'Title to be shown on the site', $this->domain ),
                            'desc_tip'      => true,
                            'default'       => __( 'pishtaz post', $this->domain )
                        ),
                        'cost' => array(
                            'type'          => 'text',
                            'title'         => __( 'Cost', $this->domain ),
                            'description'   => __( 'Enter a cost', $this->domain ),
                            'desc_tip'      => true,
                            '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_pishtaz_shipping_method' );
    function add_pishtaz_shipping_method( $methods ) {
        $methods['pishtaz'] = 'WC_Shipping_Method_Pishtaz_Post';
        return $methods;
    }
}

代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。测试和工作。

使此代码成为另一个插件的一部分:将此代码添加到一个单独的 php 文件中,您将使用以下内容将其包含在您的主插件文件中:

include_once( 'subfolder/the-php-file-name.php' );

*(您将在其中替换subfolder为正确的子文件夹名称(如果存在)the-php-file-name.php真实的 php 文件名)*

相关:在 woocommerce 3 中创建新的运输方式

于 2019-01-05T00:14:55.887 回答