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

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

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

问题

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

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

  1. function adicionar_guia_leadlovers($tabs) {
  2. $tabs['guia_leadlovers'] = array(
  3. 'label' => __( 'LeadLovers', 'text-domain' ),
  4. 'target' => 'leadlovers_product_data',
  5. 'class' => array( 'show_if_simple', 'show_if_variable' ),
  6. );
  7. return $tabs;
  8. }
  9. 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.

  1. function adicionar_guia_leadlovers($tabs) {
  2. $tabs['guia_leadlovers'] = array(
  3. 'label' => __( 'LeadLovers', 'text-domain' ),
  4. 'target' => 'leadlovers_product_data',
  5. 'class' => array( 'show_if_simple', 'show_if_variable' ),
  6. );
  7. return $tabs;
  8. }
  9. 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”:

  1. add_filter( 'woocommerce_product_data_tabs', 'add_leadlovers_guide_product_tab' );
  2. function add_leadlovers_guide_product_tab($tabs) {
  3. $tabs['leadlovers_guide'] = array(
  4. 'label' => __( 'LeadLovers', 'text-domain' ),
  5. 'target' => 'leadlovers_product_data',
  6. 'class' => array( 'show_if_simple', 'show_if_variable' ),
  7. );
  8. return $tabs;
  9. }
  10. // 显示内容
  11. add_action( 'woocommerce_product_data_panels', 'display_readlovers_guide_product_data_tab_content' );
  12. function display_readlovers_guide_product_data_tab_content() {
  13. global $product_object;
  14. echo '<div id="leadlovers_product_data" class="panel woocommerce_options_panel">';
  15. ## ---- 内容开始 ---- ##
  16. echo '<p>This is your LeadLovers content for the product "<strong>'.$product_object->get_name().'</strong>"...</p>';
  17. woocommerce_wp_text_input( array(
  18. 'id' => '_leadlovers',
  19. 'value' => $product_object->get_meta('_leadlovers'),
  20. 'label' => __('LeadLovers field', 'woocommerce'),
  21. 'placeholder' => '',
  22. 'description' => __('LeadLovers description text.', 'woocommerce'),
  23. ));
  24. ## ---- 内容结束 ---- ##
  25. echo '</div></div>';
  26. }
  27. // 保存字段值
  28. add_action( 'woocommerce_admin_process_product_object', 'save_leadlovers_guide_fields_values' );
  29. function save_leadlovers_guide_fields_values( $product ) {
  30. $leadlovers = isset( $_POST['_leadlovers'] ) ? sanitize_text_field($_POST['_leadlovers']) : '';
  31. $product->update_meta_data( '_leadlovers', $leadlovers );
  32. }

然后您会得到:

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

英文:

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

  1. add_filter( &#39;woocommerce_product_data_tabs&#39;, &#39;add_leadlovers_guide_product_tab&#39; );
  2. function add_leadlovers_guide_product_tab($tabs) {
  3. $tabs[&#39;leadlovers_guide&#39;] = array(
  4. &#39;label&#39; =&gt; __( &#39;LeadLovers&#39;, &#39;text-domain&#39; ),
  5. &#39;target&#39; =&gt; &#39;leadlovers_product_data&#39;,
  6. &#39;class&#39; =&gt; array( &#39;show_if_simple&#39;, &#39;show_if_variable&#39; ),
  7. );
  8. return $tabs;
  9. }
  10. // Display the content
  11. add_action( &#39;woocommerce_product_data_panels&#39;, &#39;display_readlovers_guide_product_data_tab_content&#39; );
  12. function display_readlovers_guide_product_data_tab_content() {
  13. global $product_object;
  14. echo &#39;&lt;div id=&quot;leadlovers_product_data&quot; class=&quot;panel woocommerce_options_panel&quot;&gt;
  15. &lt;div class=&quot;options_group&quot;&gt;&#39;;
  16. ## ---- Content Start ---- ##
  17. 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;;
  18. woocommerce_wp_text_input( array(
  19. &#39;id&#39; =&gt; &#39;_leadlovers&#39;,
  20. &#39;value&#39; =&gt; $product_object-&gt;get_meta(&#39;_leadlovers&#39;),
  21. &#39;label&#39; =&gt; __(&#39;LeadLovers field&#39;, &#39;woocommerce&#39;),
  22. &#39;placeholder&#39; =&gt; &#39;&#39;,
  23. &#39;description&#39; =&gt; __(&#39;LeadLovers description text.&#39;, &#39;woocommerce&#39;),
  24. ));
  25. ## ---- Content End ---- ##
  26. echo &#39;&lt;/div&gt;&lt;/div&gt;&#39;;
  27. }
  28. // Save field values
  29. add_action( &#39;woocommerce_admin_process_product_object&#39;, &#39;save_leadlovers_guide_fields_values&#39; );
  30. function save_leadlovers_guide_fields_values( $product ) {
  31. $leadlovers = isset( $_POST[&#39;_leadlovers&#39;] ) ? sanitize_text_field($_POST[&#39;_leadlovers&#39;]) : &#39;&#39;;
  32. $product-&gt;update_meta_data( &#39;_leadlovers&#39;, $leadlovers );
  33. }

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:

确定