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

时间:2019-04-24 11:55:47

标签: php wordpress woocommerce cart taxonomy-terms

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

有什么建议或帮助吗?

1 个答案:

答案 0 :(得分:0)

由于woocommerce_add_to_cart_validation钩子同时位于WC_Cart和WC_Ajax add_to_cart()方法上,因此当通过ajax或通常通过单个产品页面将产品添加到购物车时会触发该钩子。在添加到购物车事件中的所有情况下均可使用。


也处理父产品类别

现在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文件中,或者出现在任何插件文件中。

代码已经过测试,可以正常工作


限制购物车商品只能来自不同的产品类别

在第一个功能中,替换:

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