簡體   English   中英

添加一個復選框作為隱藏相關產品的 Woocommerce 管理產品選項

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

為了使這盡可能簡單和容易,我嘗試在產品類型選擇器旁邊的頂行添加此復選框,您通常可以在其中找到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 );
    }
}

但它不起作用......有什么建議嗎?

自 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 文件中。 測試和工作。

相關: 將復選框添加到 Woocommerce 后端產品編輯頁面中的產品類型選項

為了隱藏相關產品部分,您可以顯示一個復選框以禁用相關產品。 您只需要將以下代碼段添加到您的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