簡體   English   中英

避免在WooCommerce中添加來自不同產品類別的購物車產品

[英]Avoid add to cart products from different product categories in WooCommerce

在WooCommerce上,我嘗試了限制購物車商品來自同一產品類別 ”的回答代碼,並且它可以正常工作。 但是,如果用戶從產品頁面添加產品,則該產品將在購物車中。

有什么建議或幫助嗎?

因為woocommerce_add_to_cart_validation鈎子同時位於WC_Cart和WC_Ajax add_to_cart()方法上,所以當通過ajax或通常通過單個產品頁面將產品添加到購物車時會觸發它。因此,在添加到購物車事件中,代碼在所有情況下均適用。


也處理父產品類別

現在, 在WooCommerce中將購物車商品限制為來自同一產品類別 ”將無法處理父產品類別,因為WordPress的has_term()條件函數無法處理父條款,因此父產品類別也無法處理。

為了使其也能與父產品類別一起使用,您將需要更詳細的說明:

add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {

    // HERE your alert text message
    $message = __( 'MY ALERT MESSAGE.', 'woocommerce' );

    if( ! WC()->cart->is_empty() ) {
        $term_ids = array(); // Initializing

        // Loop through product category WP_Term objects set for the current the product
        foreach( wp_get_post_terms( $product_id, 'product_cat') as $term ) {
            $terms_ids[$term->term_id] = $term->term_id; // Add the term ID to the array

            // Add the parent term ID to the array, if it exist
            if( $term->parent > 0 )
                $terms_ids[$term->parent] = $term->parent; 
        }

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item ){
            if( ! has_product_categories( $product_id, $term_ids ) ) {
                $passed = false;
                wc_add_notice( $message, 'error' );
                break;
            }
        }
    }
    return $passed;
}

// Custom conditional function that handle parent product categories too
function has_product_categories( $product_id, $categories ) {
     // Initializing
    $parent_term_ids = $categories_ids = array();
    $taxonomy        = 'product_cat';

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
            $categories_ids[] = (int) $category;
        } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
            $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

代碼在您的活動子主題(或主題)的function.php文件中,或者在任何插件文件中。

代碼經過測試,可以正常工作


限制購物車商品僅來自不同的產品類別

在第一個函數中,替換為:

if( ! has_product_categories( $cart_item['product_id'], $term_ids ) ) {

通過

if( has_product_categories( $cart_item['product_id'], $term_ids ) ) {

相關: 在WooCommerce中將購物車項目限制為來自同一產品類別

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM