php 当创建新的woocommerce免费送货方法时,它不会保存更改

pxq42qpu  于 2023-04-04  发布在  PHP
关注(0)|答案(3)|浏览(113)

我在woocommerce中使用以下php代码创建了一个新的免费送货方法:

if ( ! defined( 'WPINC' ) ) {
 
    die;
 
}
 
/*
 * Check if WooCommerce is active
 */
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

function enviogratuitopeninsula_shipping_init() {
    if ( ! class_exists( 'Imp_WC_Shipping_Free_Local_Pickup' ) ) {

        class Imp_WC_Pickup_Free_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor.
             *
             * @param int $instance_id
             */
            public function __construct( $instance_id = 0 ) {
                $this->id           = 'imp_enviopeninsulagratuito_method';
                $this->instance_id  = absint( $instance_id );
                $this->method_title = __( "Envio gratuito para la peninsula", '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' ) );
            }

            function init_form_fields() {

                 $this->init_settings();
                $this->form_fields = array(

            'enabled'      => array(
                'title'   => __( 'Activar/Desactivar', 'woocommerce' ),
                'type'    => 'checkbox',
                'label'   => __( 'Una vez desactivado, este método heredado dejará de estar disponible.', 'woocommerce' ),
                'default' => 'no',
            ),
            'title'        => array(
                'title'       => __( 'Titulo', 'woocommerce' ),
                'type'        => 'text',
                'description' => __( 'Esto controla el título que el usuario ve durante el pago.', 'woocommerce' ),
                'default'     => __( 'Envío gratuito', 'woocommerce' ),
                'desc_tip'    => true,
            ),
            'availability' => array(
                'title'   => __( 'Disponibilidad de los métodos', 'woocommerce' ),
                'type'    => 'select',
                'default' => 'all',
                'class'   => 'availability wc-enhanced-select',
                'options' => array(
                    'all'      => __( 'All allowed countries', 'woocommerce' ),
                    'specific' => __( 'Specific Countries', 'woocommerce' ),
                ),
            ),
            'countries'    => array(
                'title'             => __( 'Países específicos', 'woocommerce' ),
                'type'              => 'multiselect',
                'class'             => 'wc-enhanced-select',
                'css'               => 'width: 400px;',
                'default'           => '',
                'options'           => WC()->countries->get_shipping_countries(),
                'custom_attributes' => array(
                    'data-placeholder' => __( 'Seleccione algunos países', 'woocommerce' ),
                ),
            ),
            'requires'     => array(
                'title'   => __( 'El envío gratuito requiere...', 'woocommerce' ),
                'type'    => 'select',
                'class'   => 'wc-enhanced-select',
                'default' => '',
                'options' => array(
                    ''           => __( 'N/D', 'woocommerce' ),
                    'coupon'     => __( 'Un cupón de envío gratuito válido', 'woocommerce' ),
                    'min_amount' => __( 'Un importe mínimo de pedido', 'woocommerce' ),
                    'either'     => __( 'Un importe mínimo de pedido O un cupón', 'woocommerce' ),
                    'both'       => __( 'Un importe mínimo de pedido Y un cupón', 'woocommerce' ),
                ),
            ),
            'min_amount'   => array(
                'title'       => __( 'Importe mínimo del pedido', 'woocommerce' ),
                'type'        => 'price',
                'placeholder' => wc_format_localized_price( 0 ),
                'description' => __( 'Los usuarios tendrán que gastar esta cantidad para obtener el envío gratuito (si está activado arriba).', 'woocommerce' ),
                'default'     => '0',
                'desc_tip'    => true,
            ),
         );

            }

            /**
             * 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'     => '',
                    'calc_tax' => ''
                );

                // Register the rate
                $this->add_rate( $rate );
            }
        }
    }
}
add_action( 'woocommerce_shipping_init', 'enviogratuitopeninsula_shipping_init' );

function envioapeninsulagratuito_shipping_method( $methods ) {
    $methods['imp_enviopeninsulagratuito_method'] = 'Imp_WC_Pickup_Free_Shipping_Method';

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

在下面的图片中,您可以看到结果:

当点击“保存更改”时出现问题,它不保存它们。有没有一种方法可以创建一个不同的免费送货方法,从woocommerce的默认方法中分离出来??我的意思是,相同的字段和相同的选项,但不同的ID。

kxxlusnw

kxxlusnw1#

我将以下代码添加到前一个:
´´´´

/**
         * Process and redirect if disabled.
         */
        public function process_admin_options() {
            parent::process_admin_options();

            if ( 'no' === $this->settings['enabled'] ) {
                wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options' ) );
                exit;
            }
        }

´´ ´´现在它似乎正在尝试保存配置字段(激活免费送货的最低金额,国家...),但保存没有完成...

umuewwlo

umuewwlo2#

init_form_fields()方法中使用$this->instance_form_fields而不是$this->form_fields

function init_form_fields() {

    $this->init_settings();
    $this->instance_form_fields = array(

        'enabled'      => array(
            'title'   => __( 'Activar/Desactivar', 'woocommerce' ),
            'type'    => 'checkbox',
            'label'   => __( 'Una vez desactivado, este método heredado dejará de estar disponible.', 'woocommerce' ),
            'default' => 'no',
        ),
        'title'        => array(
            'title'       => __( 'Titulo', 'woocommerce' ),
            'type'        => 'text',
            'description' => __( 'Esto controla el título que el usuario ve durante el pago.', 'woocommerce' ),
            'default'     => __( 'Envío gratuito', 'woocommerce' ),
            'desc_tip'    => true,
        ),
        'availability' => array(
            'title'   => __( 'Disponibilidad de los métodos', 'woocommerce' ),
            'type'    => 'select',
            'default' => 'all',
            'class'   => 'availability wc-enhanced-select',
            'options' => array(
                'all'      => __( 'All allowed countries', 'woocommerce' ),
                'specific' => __( 'Specific Countries', 'woocommerce' ),
            ),
        ),
        'countries'    => array(
            'title'             => __( 'Países específicos', 'woocommerce' ),
            'type'              => 'multiselect',
            'class'             => 'wc-enhanced-select',
            'css'               => 'width: 400px;',
            'default'           => '',
            'options'           => WC()->countries->get_shipping_countries(),
            'custom_attributes' => array(
                'data-placeholder' => __( 'Seleccione algunos países', 'woocommerce' ),
            ),
        ),
        'requires'     => array(
            'title'   => __( 'El envío gratuito requiere...', 'woocommerce' ),
            'type'    => 'select',
            'class'   => 'wc-enhanced-select',
            'default' => '',
            'options' => array(
                ''           => __( 'N/D', 'woocommerce' ),
                'coupon'     => __( 'Un cupón de envío gratuito válido', 'woocommerce' ),
                'min_amount' => __( 'Un importe mínimo de pedido', 'woocommerce' ),
                'either'     => __( 'Un importe mínimo de pedido O un cupón', 'woocommerce' ),
                'both'       => __( 'Un importe mínimo de pedido Y un cupón', 'woocommerce' ),
            ),
        ),
        'min_amount'   => array(
            'title'       => __( 'Importe mínimo del pedido', 'woocommerce' ),
            'type'        => 'price',
            'placeholder' => wc_format_localized_price( 0 ),
            'description' => __( 'Los usuarios tendrán que gastar esta cantidad para obtener el envío gratuito (si está activado arriba).', 'woocommerce' ),
            'default'     => '0',
            'desc_tip'    => true,
        ),
    );

}

要获得这些值,请使用$this->get_option()方法,就像您在init()方法中所做的那样:

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

在某些情况下,你可能需要get_instance_from_fields()方法,但我有一个没有它的工作解决方案:

/**
     * Get setting form fields for instances of this shipping method within zones.
     *
     * @return array
     */
    public function get_instance_form_fields() {
        return parent::get_instance_form_fields();
    }
zynd9foi

zynd9foi3#

使用$this->instance_form_fields解决了我的问题。

相关问题