简体   繁体   English

添加一个复选框作为隐藏相关产品的 Woocommerce 管理产品选项

[英]Add a checkbox as Woocommerce admin product option that hides related products

To make this as simple and as easy as possible, I'm trying to add this checkbox on the top row next to the product type selector where you would normally find Virtual and Download .为了使这尽可能简单和容易,我尝试在产品类型选择器旁边的顶行添加此复选框,您通常可以在其中找到VirtualDownload

The idea is to have the checkbox there so that no matter the product type, it is always available.我们的想法是在那里设置复选框,以便无论产品类型如何,它始终可用。

This is what I've tried:这是我尝试过的:

add_action( 'woocommerce_product_type_options', 'remove_related_products_checkbox' );        
function remove_related_products_checkbox() {           
    woocommerce_wp_checkbox( array( 
        'id' => '_remove_related_products', 
        'class' => '', 
        'label' => 'Remove Related Products?'
    ) );      
}

add_action( 'save_post_product', 'related_products_checkbox_save' );
function remove_related_products_checkbox_save( $product_id ) {
    global $pagenow, $typenow;

    if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if ( isset( $_POST['_remove_related_products'] ) ) {
        update_post_meta( $product_id, '_remove_related_products', $_POST['_remove_related_products'] );
    } else 
        delete_post_meta( $product_id, '_remove_related_products' );
}

add_action( 'woocommerce_after_single_product_summary', 'remove_related_products_checkbox_display', 1 );
function remove_related_products_checkbox_display() {
    global $product;

    if ( ! empty ( get_post_meta( $product->get_id(), '_remove_related_products', true ) ) ) {
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    }
}

But it doesn't work… Any advice please?但它不起作用......有什么建议吗?

Your code is a bit outdated since WooCommerce 3 and there are some mistakes.自 WooCommerce 3 以来,您的代码有点过时,并且存在一些错误。

Try the following instead:请尝试以下操作:

add_filter( 'product_type_options', 'hide_related_products_option' );
function hide_related_products_option( $fields ) {
    $fields['hide_related'] = array(
        'id'                => '_hide_related',
        'wrapper_class'     => '',
        'label'             => __('Remove Related Products'),
        'description'   => __( 'Remove/Hide related products.', 'woocommerce' ),
        'default'           => 'no'
    );
    return $fields;
}

add_action( 'woocommerce_admin_process_product_object', 'hide_related_products_option_save' );
function hide_related_products_option_save( $product ) {
    $product->update_meta_data( '_hide_related', isset( $_POST['_hide_related'] ) ? 'yes' : 'no' );
}

add_action( 'woocommerce_after_single_product_summary', 'remove_related_products_checkbox_display', 1 );
function remove_related_products_checkbox_display() {
    global $product;

    if ( $product->get_meta('_hide_related') === 'yes' ) {
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    }
}

Code goes in function.php file of your active child theme (active theme).代码位于活动子主题(活动主题)的 function.php 文件中。 Tested and works.测试和工作。

Related: Add checkbox to product type option in Woocommerce backend product edit pages相关: 将复选框添加到 Woocommerce 后端产品编辑页面中的产品类型选项

In order to hide the Related Products section you can display a checkbox to disable related products.为了隐藏相关产品部分,您可以显示一个复选框以禁用相关产品。 You just need to add the following snippet to your functions.php.您只需要将以下代码段添加到您的functions.php 中。

add_action( 'woocommerce_product_options_general_product_data', 'codeithub_add_related_checkbox_products' );        
  
function codeithub_add_related_checkbox_products() {           
woocommerce_wp_checkbox( array( 
   'id' => 'hide_related', 
   'class' => '', 
   'label' => 'Hide Related Products'
   ) 
);      
}
  
add_action( 'save_post_product', 'codeithub_save_related_checkbox_products' );
  
function codeithub_save_related_checkbox_products( $product_id ) {
   global $pagenow, $typenow;
   if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset( $_POST['hide_related'] ) ) {
      update_post_meta( $product_id, 'hide_related', $_POST['hide_related'] );
    } else delete_post_meta( $product_id, 'hide_related' );
}
  

add_action( 'woocommerce_after_single_product_summary', 'codeithub_hide_related_checkbox_products', 1 );
  
function codeithub_hide_related_checkbox_products() {
    global $product;
    if ( ! empty ( get_post_meta( $product->get_id(), 'hide_related', true ) ) ) {
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM