简体   繁体   English

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

[英]When creating new woocommerce free shipping method, it doesn´t save the changes

I´ve created a new free shipping method in woocommerce using the following php code:我使用以下 php 代码在 woocommerce 中创建了一种新的免费送货方式:

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

In the following picture you can see the result:在下图中,您可以看到结果:

在此处输入图像描述

The problem occurs when clicking "save changes", it doesn´t save them.单击“保存更改”时会出现问题,它不会保存它们。 Is there a way to create a different free shipping method appart from the default one of woocommerce??有没有办法从默认的 woocommerce 中创建不同的免费送货方式? I mean, same fields and same option but different ID.我的意思是,相同的字段和相同的选项,但不同的 ID。

I´ve added the following code to the previous one:我在上一个代码中添加了以下代码:

´´´´ ''''

     /**
         * 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;
            }
        }

´´´´ Now it seems to be trying to save the the configuration fields (minimum amount for activating the free shipping, countries...), but the saving doesn´t finish... ´´´´ 现在似乎正在尝试保存配置字段(激活免费送货的最低金额,国家...),但保存没有完成...

尝试保存时的 Bucle

Use $this->instance_form_fields instead of $this->form_fields in the init_form_fields() method.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,
        ),
    );

}

For obtaining these values use the $this->get_option() method like you did in the init() method:要获取这些值,请使用$this->get_option()方法,就像在init()方法中所做的那样:

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

In some cases you may need the get_instance_from_fields() method but I have a working solution without it:在某些情况下,您可能需要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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 WooCommerce 提供免费送货时隐藏特定的送货方式 - Hide specific shipping method when Free Shipping is available in WooCommerce WooCommerce 中的有条件免费送货方式 - Conditional free shipping method in WooCommerce 当 Woocommerce 提供免费送货时,仅禁用统一费率送货方式 - Disable only flat rate shipping method when free shipping is available in Woocommerce 当 WooCommerce 中提供免费送货时,仅禁用特定的统一费率送货方式 - Disable only specific flat rate shipping method when free shipping is available in WooCommerce 在 Woocommerce 中启用免费送货时更改显示的免费送货标签 - Change displayed Free shipping label when Free shipping is enabled in Woocommerce 在woocommerce 3中创建新的送货方式 - Create a new shipping method in woocommerce 3 为 woocommerce 上的免费商品创建运输方式 - Create a shipping method for free items on woocommerce 在 WooCommerce 中将“免费送货”方式设置为选定的默认送货选项 - Set "free shipping" method as selected default shipping option in WooCommerce 当 Woocommerce 提供免费送货服务时,隐藏运费统一费率” - Hide shipping Flat rate" when free shipping is available in Woocommerce WooCommerce - 当免费送货可用时隐藏其他送货方式 - WooCommerce - Hide other shipping methods when FREE SHIPPING is available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM