【问题标题】:How do I add a custom field to the "Products Inventory" tab in the WooCommerce settings?如何将自定义字段添加到 WooCommerce 设置中的“产品库存”选项卡?
【发布时间】:2020-12-21 11:15:56
【问题描述】:

我正在寻找一种将新字段添加到 WooCommerce - 设置 - 产品 - 库存的方法

见附图:


经过一些研究,我认为我应该使用 woocommerce_inventory_settings 过滤器挂钩,但我不立即知道如何在实践中应用它?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce custom-fields


    【解决方案1】:

    woocommerce_inventory_settings 钩子可以在 class-wc-settings-products.php 在线 81 中找到(在 WooCommerce 4.4.1 中)

    所以你可以使用

    // Add custom field: WooCommerce > Settings > Products > Iventory
    function filter_woocommerce_inventory_settings( $settings ) {
        $settings[] = array(
            'title' => __( 'My title', 'woocommerce' ),
            'type'  => 'title',
            'desc'  => '',
            'id'    => 'product_inventory_custom_options',
        );
        
        $settings[] = array(
            'title'       => __( 'My message', 'woocommerce' ),
            'id'          => 'woocommerce_my_message',
            'type'        => 'text',
            'default'     => '',
            'class'       => '',
            'css'         => '',
            'placeholder' => __( 'Enter my message', 'woocommerce' ),
            'desc_tip'    => __( 'This is the message that appears when..', 'woocommerce' ),
        );
        
        $settings[] = array(
            'type' => 'sectionend',
            'id'   => 'product_inventory_custom_options',
        );
    
        return $settings;
    }
    add_filter( 'woocommerce_inventory_settings', 'filter_woocommerce_inventory_settings', 10, 1 );
    

    要在代码或您网站的其他位置获取值,请使用 get_option( string $option, mixed $default = false ) - 根据选项名称检索选项值。

    // Get message from field
    $get_option = get_option( 'woocommerce_my_message' );
    

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 1970-01-01
      • 2018-09-22
      • 2021-02-03
      • 2023-04-08
      • 2018-03-04
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多