programing

WooCommerce 3에서 커스텀 워킹 배송 방법을 추가하는 방법

goodjava 2023. 2. 11. 09:24

WooCommerce 3에서 커스텀 워킹 배송 방법을 추가하는 방법

저는 새로운 배송 방법을 성공적으로 만들어 배송 구역을 지원했습니다.그러나 드롭다운에서 메서드를 선택하여 존에 추가하면 'selected methods list'에 표시되지 않습니다.

나는 시연하기 위해 스크린캐스트 gif를 녹화했다.

스크린캐스트 GIF

나는 아무리 해도 그것이 왜 작동하지 않는지 이해할 수 없다.표준 방식(스크린캐스트 GIF) 중 하나를 선택하면 정상적으로 동작합니다.

여기서 무슨 일이 일어나고 있고 어떻게 작동시키는지 아는 사람?

이 공식 스레드에서 얻은 코드는 다음과 같습니다. 배송 방법 API:

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

    function request_a_shipping_quote_init() {
        if ( ! class_exists( 'WC_Request_Shipping_Quote_Method' ) ) {
            class WC_Request_Shipping_Quote_Method extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                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();
                }

                /**
                 * Init your settings
                 *
                 * @access public
                 * @return void
                 */
                function init() {
                    // Load the settings API
                    $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.

                    // Save settings in admin if you have any defined
                    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' )
                        ),

                    );

                }

                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 *
                 * @param mixed $package
                 *
                 * @return void
                 */

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

                    // Register the rate
                    $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' );
}

"woocommerce_shipping_methods"의 메서드 키는 배송 방법 ID와 일치해야 합니다.

고객님의 경우:옷을 갈아입어야 한다

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

수신인:

function request_shipping_quote_shipping_method( $methods ) {
    $methods['request_a_shipping_quote'] = 'WC_Request_Shipping_Quote_Method';

    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );

이 행을 변경하다

public function calculate_shipping( $package ) {

이 선까지

public function calculate_shipping( $package = array() ) {

문제의 코드를 사용하고, 이 투고에 코멘트에서 발견된 에러를 모두 수정하려고 해도, 아직 문제가 있었습니다.예를 들어 배송지구에 추가해도 배송방법을 편집할 수 없습니다.

여기에 이미지 설명 입력

드디어 표준 무료배송 Wooomerce 메서드를 편집하여 원하는 코드를 얻었습니다.누군가에게 시간을 절약해 주길 바란다.

function request_a_shipping_quote_init() {
    if ( ! class_exists( 'Imp_WC_Shipping_Local_Pickup' ) ) {

        class Imp_WC_Pickup_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor.
             *
             * @param int $instance_id
             */
            public function __construct( $instance_id = 0 ) {
                $this->id           = 'imp_pickup_shipping_method';
                $this->instance_id  = absint( $instance_id );
                $this->method_title = __( "Самовывоз из точки выдачи ( MO г. Дзержинский )", 'imp' );
                $this->supports     = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                );
                $this->init();
            }

            /**
             * Initialize custom shiping method.
             */
            public function init() {

                // Load the settings.
                $this->init_form_fields();
                $this->init_settings();

                // Define user set variables
                $this->title = $this->get_option( 'title' );

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

            /**
             * Calculate custom shipping method.
             *
             * @param array $package
             *
             * @return void
             */
            public function calculate_shipping( $package = array() ) {
                $this->add_rate( array(
                    'label'   => $this->title,
                    'package' => $package,
                ) );
            }

            /**
             * Init form fields.
             */
            public function init_form_fields() {
                $this->instance_form_fields = array(
                    'title' => array(
                        'title'       => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),
                        'type'        => 'text',
                        'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                        'default'     => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),
                        'desc_tip'    => true,
                    ),
                );
            }
        }
    }
}
add_action( 'woocommerce_shipping_init', 'request_a_shipping_quote_init' );

function request_shipping_quote_shipping_method( $methods ) {
    $methods['imp_pickup_shipping_method'] = 'Imp_WC_Pickup_Shipping_Method';

    return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );

WC_Custom_Shipping_Method추상 클래스이며 상속된 메서드를 변경하려고 합니다.calculate_shipping추상적인 수업들은 허락하지 않습니다.

이렇게 해봐.

<?php

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

function request_a_shipping_quote_init() {
    class Abs_Custom_Shipping extends WC_Shipping_Method{}
    if ( ! class_exists( 'WC_Request_Shipping_Quote_Method' ) ) {
        class WC_Request_Shipping_Quote_Method extends Abs_Custom_Shipping {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            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();
            }

            /**
             * Init your settings
             *
             * @access public
             * @return void
             */
            function init() {
                // Load the settings API
                $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.

                // Save settings in admin if you have any defined
                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' )
                    ),

                );

            }

            /**
             * calculate_shipping function.
             *
             * @access public
             *
             * @param mixed $package
             *
             * @return void
             */

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

                // Register the rate
                $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' );
}

(배송 방식을 자녀 클래스로 확장한 후 자녀 클래스를 손자 클래스로 확장하여 calculate_method를 변경할 수 있습니다).

말이 됐으면 좋겠다.

안부 전해요

이 문제가 있어서 며칠 동안 정신이 없었어요.Woocommerce 코드를 보고 무슨 일이 일어나고 있는지 이해하던 중 woocommerce_shipping_methods의 필터 방식을 설정할 때 이 배열에 추가한 엔트리의 인덱스를 배송 중인 ID 속성과 동일하게 만들어야 한다는 것을 알게 되었습니다.ethod 클래스이렇게 하면 배송방법이 추가되어 존에 맞게 표시됩니다.이전부터 인덱스 없이 필터 방식으로 배열에 엔트리를 추가했는데 WC에서 보기만 하면 문제없을 것 같습니다.그러나 설정을 저장하는 코드는 ID를 인덱스로 사용하여 배송 방법을 식별합니다.다른 코멘트로 미루어 볼 때 이 특정 인덱스는 WC 버전 3에서 추가된 것으로 추측됩니다.이게 도움이 됐으면 좋겠다.

배송방법이 여전히 효과가 없는 것 같으면 다음 사항을 확인해야 합니다.

  1. instance_id는 이 스니펫과 같이 생성자에서 정의되어야 합니다.


퍼블릭 함수 __public function_id =0){$this - > parames _ id = absint parames _ absint param_id
// 다른 행이 이어집니다.}
  1. 오래된 데이터는 없습니다.과도한 데이터와 클라이언트의 데이터를 삭제합니다(WooCommerce Settings > Status > Tools).

언급URL : https://stackoverflow.com/questions/45177226/how-to-add-a-custom-working-shipping-method-in-woocommerce-3