将内容添加到WooCommerce管理单个产品数据选项设置中的自定义选项卡

huangapple go评论65阅读模式
英文:

Add content to a custom tab in WooCommerce admin single product data options settings

问题

我创建了一个自定义选项卡在产品数据选项面板上,但我不知道如何在上面编写内容。我有一个代码可以打印出在库存选项卡上的选项(复选框、文本等),它使用了钩子 add_action('woocommerce_product_options_sku','add_leadlovers_custom_fields' );

我使用了这段代码来生成这个选项卡

function adicionar_guia_leadlovers($tabs) {
    $tabs['guia_leadlovers'] = array(
        'label'    => __( 'LeadLovers', 'text-domain' ),
        'target'   => 'leadlovers_product_data',
        'class'    => array( 'show_if_simple', 'show_if_variable' ),
    );
    return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'adicionar_guia_leadlovers' );

但是,我应该使用什么钩子来替代 'woocommerce_product_options_sku' 并在我的自定义选项卡上编写选项?

英文:

I created a custom tab on the product data options panel, but i don't know how to write the content on it. I have yet a code that prints the options (checkboxes, texts etc) that was on the inventory tab using the hook add_action('woocommerce_product_options_sku','add_leadlovers_custom_fields' );

I used this code to generates this tab.

function adicionar_guia_leadlovers($tabs) {
    $tabs['guia_leadlovers'] = array(
        'label'    => __( 'LeadLovers', 'text-domain' ),
        'target'   => 'leadlovers_product_data',
        'class'    => array( 'show_if_simple', 'show_if_variable' ),
    );
    return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'adicionar_guia_leadlovers' );

But, what hook can I use to replace 'woocommerce_product_options_sku' and wrtite the options on my own custom tab?

答案1

得分: 1

这是缺失的挂钩函数,用于显示内容(并保存字段值)以供您的额外产品设置选项卡“LeadLovers”:

add_filter( 'woocommerce_product_data_tabs', 'add_leadlovers_guide_product_tab' );
function add_leadlovers_guide_product_tab($tabs) {
    $tabs['leadlovers_guide'] = array(
        'label'    => __( 'LeadLovers', 'text-domain' ),
        'target'   => 'leadlovers_product_data',
        'class'    => array( 'show_if_simple', 'show_if_variable' ),
    );
    return $tabs;
}

// 显示内容
add_action( 'woocommerce_product_data_panels', 'display_readlovers_guide_product_data_tab_content' );
function display_readlovers_guide_product_data_tab_content() {
    global $product_object;

    echo '<div id="leadlovers_product_data" class="panel woocommerce_options_panel">';

    ## ---- 内容开始 ---- ##

    echo '<p>This is your LeadLovers content for the product "<strong>'.$product_object->get_name().'</strong>"...</p>';

    woocommerce_wp_text_input( array(
        'id'          => '_leadlovers',
        'value'       => $product_object->get_meta('_leadlovers'),
        'label'       => __('LeadLovers field', 'woocommerce'),
        'placeholder' => '',
        'description' => __('LeadLovers description text.', 'woocommerce'),
    ));

    ## ---- 内容结束 ---- ##

    echo '</div></div>';
}

// 保存字段值
add_action( 'woocommerce_admin_process_product_object', 'save_leadlovers_guide_fields_values' );
function save_leadlovers_guide_fields_values( $product ) {
    $leadlovers = isset( $_POST['_leadlovers'] ) ? sanitize_text_field($_POST['_leadlovers']) : '';
    $product->update_meta_data( '_leadlovers', $leadlovers );
}

然后您会得到:

将内容添加到WooCommerce管理单个产品数据选项设置中的自定义选项卡

英文:

Here are the missing hooked function, to display the content (and save field values) for your additional product settings tab "LeadLovers":

add_filter( &#39;woocommerce_product_data_tabs&#39;, &#39;add_leadlovers_guide_product_tab&#39; );
function add_leadlovers_guide_product_tab($tabs) {
    $tabs[&#39;leadlovers_guide&#39;] = array(
        &#39;label&#39;    =&gt; __( &#39;LeadLovers&#39;, &#39;text-domain&#39; ),
        &#39;target&#39;   =&gt; &#39;leadlovers_product_data&#39;,
        &#39;class&#39;    =&gt; array( &#39;show_if_simple&#39;, &#39;show_if_variable&#39; ),
    );
    return $tabs;
}

// Display the content
add_action( &#39;woocommerce_product_data_panels&#39;, &#39;display_readlovers_guide_product_data_tab_content&#39; );
function display_readlovers_guide_product_data_tab_content() {
    global $product_object;

    echo &#39;&lt;div id=&quot;leadlovers_product_data&quot; class=&quot;panel woocommerce_options_panel&quot;&gt;
    &lt;div class=&quot;options_group&quot;&gt;&#39;;

    ## ---- Content Start ---- ##

    echo &#39;&lt;p&gt;This is your LeadLovers content for the product &quot;&lt;strong&gt;&#39;.$product_object-&gt;get_name().&#39;&lt;/strong&gt;&quot;…&lt;/p&gt;&#39;;

    woocommerce_wp_text_input( array(
        &#39;id&#39;          =&gt; &#39;_leadlovers&#39;,
        &#39;value&#39;       =&gt; $product_object-&gt;get_meta(&#39;_leadlovers&#39;),
        &#39;label&#39;       =&gt; __(&#39;LeadLovers field&#39;, &#39;woocommerce&#39;),
        &#39;placeholder&#39; =&gt; &#39;&#39;,
        &#39;description&#39; =&gt; __(&#39;LeadLovers description text.&#39;, &#39;woocommerce&#39;),
    ));

    ## ---- Content End  ---- ##

    echo &#39;&lt;/div&gt;&lt;/div&gt;&#39;;
}

// Save field values
add_action( &#39;woocommerce_admin_process_product_object&#39;, &#39;save_leadlovers_guide_fields_values&#39; );
function save_leadlovers_guide_fields_values( $product ) {
    $leadlovers = isset( $_POST[&#39;_leadlovers&#39;] ) ? sanitize_text_field($_POST[&#39;_leadlovers&#39;]) : &#39;&#39;;
    $product-&gt;update_meta_data( &#39;_leadlovers&#39;, $leadlovers );
}

Then you will get:

将内容添加到WooCommerce管理单个产品数据选项设置中的自定义选项卡

huangapple
  • 本文由 发表于 2023年7月18日 08:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定