【问题标题】:Add a checkbox as Woocommerce admin product option that hides related products添加一个复选框作为隐藏相关产品的 Woocommerce 管理产品选项
【发布时间】:2019-05-02 10:45:40
【问题描述】:

为了使此操作尽可能简单和容易,我尝试将此复选框添加到产品类型选择器旁边的顶行,您通常会在其中找到VirtualDownload

我们的想法是在此处设置复选框,以便无论产品类型如何,它始终可用。

这是我尝试过的:

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 );
    }
}

但它不起作用……请给点建议?

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    自 WooCommerce 3 以来,您的代码有点过时,并且存在一些错误。

    请尝试以下方法:

    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 );
        }
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    相关:Add checkbox to product type option in Woocommerce backend product edit pages

    【讨论】:

    • 谢谢,非常感谢。我知道这可能是一个新问题,但我现在才意识到文本后面有一个逗号,我该如何删除它?现在是删除相关产品?:我希望它是删除相关产品
    • @KevinAronsson 更新,删除“?”…
    【解决方案2】:

    为了隐藏相关产品部分,您可以显示一个复选框来禁用相关产品。您只需将以下 sn-p 添加到您的 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 );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2021-02-19
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多