簡體   English   中英

將新的運輸方式作為 Woocommerce 插件的一部分

[英]Make a new shipping method as a part of plugin in Woocommerce

我正在嘗試為 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 > 設置 > 運輸上,我看到了我的方法名稱,但是當我點擊它時,我看到一個帶有“保存更改”按鈕的空表單。 我已經多次檢查代碼,但我沒有發現問題所在。

第二個問題:正如你所看到的,這段代碼是一個完整的插件。 我想把它作為另一個插件的一部分。 什么是正確的方法?


更新:

我找到了第一個問題的答案:類 id 不能包含大寫字母。

您的代碼中存在一些錯誤和錯誤……此外,您應該盡量避免在 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 ,用真實的 php 文件名the-php-file-name.php )*

相關: 在 woocommerce 3 中創建新的運輸方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM