【问题标题】:I can't update form fields values on my custom shipping method Woocommerce我无法在我的自定义运输方式 Woocommerce 上更新表单字段值
【发布时间】:2021-03-23 16:14:06
【问题描述】:

我真的希望有人可以帮助我,因为我的想法已经不多了。

我在 Woocommerce 上制作了这个自定义运输方法,从网站文档和其他 StakO 主题中复制和粘贴。 一切似乎都很好,除了当我尝试更新我唯一的一个名为“商店地址”的字段时,它不起作用并且值没有更新。

这是代码。谢谢大家

if ( ! defined( 'WPINC' ) ) {
   die;
}

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

   function pickupinstore_shipping_method() {
       if ( ! class_exists( 'PickupInStore_Shipping_Method' ) ) {
           class PickupInStore_Shipping_Method extends WC_Shipping_Method {
               /**
                * Constructor for your shipping class
                *
                * @access public
                * @return void
                */
               public function __construct( $instance_id = 0 ) {
                   $this->id                 = 'pickupinstore'; 
                   $this->instance_id  = absint( $instance_id );
                   $this->method_title       = __( 'Pickup in Store', 'pickupinstore' );  
                   $this->method_description = __( 'Custom Shipping Method - Pickup in Store', 'pickupinstore' ); 

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

                   
                   $this->init();

                   $this->title = isset( $this->settings['title'] ) ? "Ritiro in Negozio | ".$this->settings['title'].": GRATIS" : __( 'Pickup in Store', 'pickupinstore' );                }

               /**
                * Init your settings
                *
                * @access public
                * @return void
                */
               function init() {
                   // Load the settings API
                   $this->init_form_fields(); 
                   $this->init_settings(); 

                   // Save settings in admin if you have any defined
                   add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
               }

               /**
                * Define settings field for this shipping
                * @return void 
                */
               function init_form_fields() { 

                   $this->form_fields = array(
                                 
                       'title' => array(
                          'title' => __( 'Ritiro in Negozio', 'pickupinstore' ),
                            'type' => 'text',
                            'description' => __( 'Store Address', 'pickupinstore' ),
                            'default' => __( '197, Brooklyn Road', 'New York' ),
                            'desc_tip'    => true,
                       ),    
                   );
               }


               /**
                * This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
                *
                * @access public
                * @param mixed $package
                * @return void
                */

               public function calculate_shipping( $package = array() ) {
                   
                   $cost = 0;
                   $this->add_rate( array(
                       'id' => $this->id,
                       'label'   => $this->title,
                       'cost' => $cost
                   ) );              

               }
           }
       }
   }
   add_action( 'woocommerce_shipping_init', 'pickupinstore_shipping_method' );

   function add_pickupinstore_shipping_method( $methods ) {
       $methods['pickupinstore'] = 'PickupInStore_Shipping_Method';
       return $methods;
   }

   add_filter( 'woocommerce_shipping_methods', 'add_pickupinstore_shipping_method' );

}

编辑:

我尝试使用@docker 代码,但是当我保存更改时,标题部分(后端)中没有任何内容可比较。否则表单字段似乎已保存,但如您所见,未显示。

在前端,它显示自定义运输方式的名称,而不是我保存的标题。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    $this->title 定义移至init() 方法并使用$this->get_option() 而不是$this->settings

    function init() {
       // Load the settings API
       $this->init_form_fields();
       $this->init_settings();
    
       $this->title = null != $this->get_option('title') ? "Ritiro in Negozio | " . $this->get_option('title') . ": GRATIS" : __( 'Pickup in Store', 'pickupinstore' );
    
       // Save settings in admin if you have any defined
       add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
    
    }
    

    此外,在init_form_fields() 方法中,您应该使用$this->instance_form_fields

    function init_form_fields() {
    
       $this->instance_form_fields = array(
    
           'title' => array(
              'title' => __( 'Ritiro in Negozio', 'pickupinstore' ),
                'type' => 'text',
                'description' => __( 'Store Address', 'pickupinstore' ),
                'default' => __( '197, Brooklyn Road', 'New York' ),
                'desc_tip'    => true,
           ),
       );
    }
    

    在某些情况下,您可能需要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();
        }
    

    编辑: 这是完整的工作代码:

    <?php
    /**
    * Plugin Name: Pickup in Store
    * Plugin URI: https://www.perspectiveweb.it/
    * Description: Pickup in store - Custom Shipping Method
    * Version: 1.0.0
    * Author: Perspective Web
    * Author URI: https://www.perspectiveweb.it/
    * License: GPL-3.0+
    * License URI: http://www.gnu.org/licenses/gpl-3.0.html
    * Domain Path: /lang
    * Text Domain: perspectiveweb
    */
    
    if ( ! defined( 'WPINC' ) ) {
       die;
    }
    
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
       function pickupinstore_shipping_method() {
           if ( ! class_exists( 'PickupInStore_Shipping_Method' ) ) {
               class PickupInStore_Shipping_Method extends WC_Shipping_Method {
                   /**
                    * Constructor for your shipping class
                    *
                    * @access public
                    * @return void
                    */
                   public function __construct( $instance_id = 0 ) {
                       $this->id                 = 'pickupinstore'; 
                       $this->instance_id  = absint( $instance_id );
                       $this->method_title       = __( 'Pickup in Store', 'pickupinstore' );  
                       $this->method_description = __( 'Custom Shipping Method - Pickup in Store', 'pickupinstore' ); 
    
                       $this->supports              = array(
                           'shipping-zones',
                           'instance-settings',
                           'instance-settings-modal',
                       );
    
    
                       $this->init();
                  }
    
                   /**
                    * Init your settings
                    *
                    * @access public
                    * @return void
                    */
                   public function init() {
                     // Load the settings API
                     $this->init_form_fields();
                     $this->init_settings();
    
                     $this->title = null != $this->get_option('title') ? "Ritiro in Negozio | " . $this->get_option('title') . ": GRATIS" : __( 'Pickup in Store', 'pickupinstore' );
    
                     // Save settings in admin if you have any defined
                     add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
    
                   }
    
                   /**
                    * Define settings field for this shipping
                    * @return void
                    */
                  public function init_form_fields() {
    
                     $this->instance_form_fields = array(
    
                         'title' => array(
                            'title' => __( 'Ritiro in Negozio', 'pickupinstore' ),
                              'type' => 'text',
                              'description' => __( 'Store Address', 'pickupinstore' ),
                              'default' => __( '197, Brooklyn Road', 'New York' ),
                              'desc_tip'    => true,
                         ),
                     );
                  }
    
    
                   /**
                    * This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
                    *
                    * @access public
                    * @param mixed $package
                    * @return void
                    */
    
                   public function calculate_shipping( $package = array() ) {
                       $cost = 0;
                       $this->add_rate( array(
                           'id' => $this->id,
                           'label'   => $this->title,
                           'cost' => $cost
                       ) );
    
                   }
               }
           }
       }
       add_action( 'woocommerce_shipping_init', 'pickupinstore_shipping_method' );
    
       function add_pickupinstore_shipping_method( $methods ) {
           $methods['pickupinstore'] = 'PickupInStore_Shipping_Method';
           return $methods;
       }
    
       add_filter( 'woocommerce_shipping_methods', 'add_pickupinstore_shipping_method' );
    
    }
    
    

    【讨论】:

    • 嗨@docker,我已经更新了我的代码,但它似乎不再工作了。我进行了编辑,在其中显示了使用您的更改时会发生什么。有什么建议吗?谢谢
    • 嗨@giovinazzo17 我已经编辑了我的答案以包含整个代码。
    • 非常感谢。有用。我希望这可以节省其他人的时间!
    • 嗨@giovinazzo17,不客气。由于我的建议有效,请更新您问题的“编辑部分”。唯一的其他区别是:$this-&gt;title = null != $this-&gt;get_option('title') ? "Ritiro in Negozio | " . $this-&gt;get_option('title') . ": GRATIS" : __( 'Pickup in Store', 'pickupinstore' );
    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多