【问题标题】:How to add Custom Description field in Shipping methods (Backend)如何在运输方式(后端)中添加自定义描述字段
【发布时间】:2017-10-17 23:37:43
【问题描述】:

我想在 Shipping Zone 页面的 shipping method 下添加一个自定义字段,它将是一个文本输入,用户将能够添加自定义消息,我将在前端显示该消息。

我注意到它将数据保存在 wp_woocommerce_shipping_zone_methods 表中,该表没有任何额外的列来保存数据;所以我想我必须使用我的自定义逻辑,但我不知道钩子的名称。

所以我的问题是,是否有任何钩子可以帮助/允许我

  1. 添加自定义字段。
  2. 添加自定义列。

TL;DR:

【问题讨论】:

  • 您看到的表格混合了thtfoot 中的HTML,而数据是使用受Mustache 启发的Underscore.js 模板填充的。您可以查看\includes\admin\settings\views\html-admin-page-shipping-zone-methods.php了解更多详情。
  • 配送方式设置的模型也是基于Underscore.js模板。因此,要修改视图并处理输入的数据,您必须使用自定义 JS。要存储/检索数据部分,您可以使用核心设置 api 执行此操作并将它们保存在选项中。为了在前端显示相同的内容,您必须在每个 WC 模板中使用钩子。

标签: php wordpress woocommerce shipping hook-woocommerce


【解决方案1】:
add_action('woocommerce_init', 'shipping_instance_form_fields_filters');

function shipping_instance_form_fields_filters()
{
    $shipping_methods = WC()->shipping->get_shipping_methods();
    foreach($shipping_methods as $shipping_method) {
        add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_instance_form_add_extra_fields');
    }
}

function shipping_instance_form_add_extra_fields($settings)
{
    $settings['shipping_extra_field'] = [
        'title' => 'Shipping extra field',
        'type' => 'text', 
        'placeholder' => 'shipping',
        'description' => ''
    ];

    return $settings;
} 

感谢@Wprog_dy 的想法,但是您的代码仅在“flat_rate”运输方法中添加了字段,而且您的功能非常复杂。

我的示例将为所有运输方式添加一个自定义字段

【讨论】:

  • 您以后如何获取这些数据?例如,我需要在动作挂钩“woocommerce_after_shipping_rate”中将其显示在每个标签之后。
【解决方案2】:

这就是我在shipping methods 中实现custom description 字段的方式,采用统一费率和免费送货方式

我的function.php 文件:

add_filter( 'woocommerce_shipping_instance_form_fields_flat_rate', array( $this, 'add_extra_fields_in_flat_rate' ), 10, 1);
public function add_extra_fields_in_flat_rate($settings)
    {
        $counter = 0;
        $arr = array();
        foreach ($settings as $key => $value) <br>
        {
            if($key=='cost' && $counter==0)
            {
                $arr[$key] = $value; 
                $arr['shipping_extra_field'] = array(
                    'title'         => __( 'Shipping Extra Field', 'woocommerce' ), 
                    'type'             => 'text', 
                    'placeholder'    => 'shipping',
                    'description'    => ''
                ); 
                $counter++; 
            } 
            else 
            {
                $arr[$key] = $value;
            } 
        }
        return $arr; 
    } 

【讨论】:

    【解决方案3】:

    Matic Jan 做得很好并回答了这个问题,但是如果您可以将自定义字段添加到任何运输方式......那么您如何在结帐页面“使用”其内容?这是我使用的代码,连接到woocommerce_after_shipping_rate

    function shipping_instance_custom_desc( $shipping_rate, $index ) {
        
    if( is_cart() ) return; // Exit on cart page
                
        $current_instance_ids = WC()->session->get( 'chosen_shipping_methods' );
        $current_instance_id = $current_instance_ids[0];
            
        if( $shipping_rate->id == $current_instance_id ) {
            
            $option_key = 'woocommerce_'.$shipping_rate->method_id.'_'.$shipping_rate->instance_id.'_settings';
            
            $instance_settings = get_option( $option_key );
            
            if( isset( $instance_settings[ 'shipping_extra_field' ] ) ) {
                
                ?>
                <div class="shipping-method-desc">
                    <?php echo $instance_settings[ 'shipping_extra_field' ] ?>
                </div>
                <?php
                
            }
            
        }
        
    }
    add_action( 'woocommerce_after_shipping_rate', 'shipping_instance_custom_desc' , 10, 2 );
    

    我希望有一些 wc_ 函数来获取运输方式实例设置,但是哦,天哪,我错了……我发现运输方式实例设置与任何其他 WP 设置一样存储在 wp_options 表中。所以我的代码获取这个选项值并检查它是否有自定义字段(shipping_extra_field,与 Matic Jan 使用的相同)......并将其输出为当前选择的运输方式。

    【讨论】:

      【解决方案4】:

      时间较晚,但您可以使用:

      add_action('woocommerce_product_options_general_product_data', 'my_custom_fields');
      
      function my_custom_fields() {
          $field = array(
             //This ID will be use on the _postmeta table as key_name
             'id' => 'my_custom_message',
             //Text that goes inside the label tag
             'label' => 'Message:',
             //This text will appear on the description column
             'description' => 'This is a custom message not part of WooCommerce',
             //Boolean that determines the display of the description
             'desc_tip' => true,
             //Standard html input placeholder
             'placeholder' => 'Type a message',
          );
          woocommerce_wp_text_input($field);
      }
      
      add_action('woocommerce_process_product_meta', 'save_my_custom_fields');
      
      function save_my_custom_fields($post_id) {
          update_post_meta(
              $post_id,
              'my_custom_message',
              esc_attr($POST['my_custom_message'])
          );
      }
      

      $field 数组在我看来必须至少有:

      $field = array(
          'id' => 'my_custom_message',//This ID will be use on the _postmeta table as key_name
          'label' => 'Message:',//Text that goes inside the label tag
          'description' => 'This is a custom message not part of WooCommerce',//This text will appear on the description column
          'desc_tip' => true,//Boolean that determines the display of the description
          'placeholder' => 'Type a message',//Standard html input placeholder
      );
      

      您还可以指定以下内容:

          'class' => 'css-class',//Class attributte for the input tag
          'style' => 'background:red',//Style attribute for the input tag
          'wrapper_class' => 'css-class',//Class for the wrapper of the input tag, it is a paragraph
      

      【讨论】:

      • 他正在尝试在设置->shipping-zones->shipping-methods 中向运输方式添加字段。不在产品本身上。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多