避免在WooCommerce中添加来自不同产品类别的购物车产品

问题描述 投票:1回答:1

在WooCommerce上,我尝试了“Restricting cart items to be from the same product category”答案代码,它有效。但是,如果用户从产品页面添加产品,则产品将在购物车中。

有什么建议或帮助吗?

php wordpress woocommerce cart taxonomy-terms
1个回答
0
投票

因为woocommerce_add_to_cart_validation钩子位于WC_Cart和WC_Ajax add_to_cart()方法上,所以当产品通过ajax或通常通过单个产品页面添加到购物车时触发...因此代码适用于添加到购物车事件的所有情况。


Handling parent product categories too

现在“Restricting cart items to be from the same product category in 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文件中,或者放在任何插件文件中。

代码经过测试且有效


Restricting cart items to be only from different product categories

在第一个函数中,替换:

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

通过

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

相关:Restricting cart items to be from the same product category in WooCommerce

© www.soinside.com 2019 - 2024. All rights reserved.