使用WooCommerce检查购物车中的产品类别

时间:2019-01-21 20:38:38

标签: php wordpress woocommerce cart custom-taxonomy

我正在尝试创建一个条件,即如果购物车中存在类别ilutulestik中的任何产品,则仅在WooCommerce结帐页面上显示一个函数。

但是,当前,它似乎无法获得购物车信息。我认为这是因为,即使我在购物车中有if ( $cat_in_cart )类别的产品,如果我在代码上使用ilutulestik条件,使用它的功能也不会显示。

我尝试了许多不同的方法来获取购物车信息,但似乎没有一个可行。我将在下面介绍两种尝试方法:

尝试1

add_action('woocommerce_before_cart', 'kontrollime_ilutulestiku_olemasolu');

    function kontrollime_ilutulestiku_olemasolu()
    {

        global $woocommerce;
        $cat_in_cart = false;

            foreach ( WC()->cart->get_cart() as $cart_item_key => $values )
            {
            $item = $values['data'];
            $item_id = $item->id;

                if ( has_term( 'ilutulestik-2', 'product_cat', $item_id ) )
                {
                    $cat_in_cart = true;
                    break;
                }
            }
    }

尝试2

add_action('woocommerce_before_cart', 'kontrollime_ilutulestiku_olemasolu');

     function kontrollime_ilutulestiku_olemasolu($package)
     {

        global $woocommerce;
        $cat_in_cart = false;

            foreach ($package['contents'] as $product)
            {
                // get product categories
                $product_cats = wp_get_post_terms( 
$product['product_id'], 'product_cat', array('fields' => 'names') );
                // if it has category_name unset flat rate
                    if( in_array('ilutulestik-2', $product_cats) )
                    {
                    $cat_in_cart = true;
                    break;
                    }  
            }         
    }

我希望这段代码可以检查购物车中是否有属于类别Ilutulestik(或子弹ilutulestik-2)的产品,如果包含,将$cat_in_cart的值更改为{ {1}},因此以后我可以使用true激活任何代码。

1 个答案:

答案 0 :(得分:0)

将购物车商品与产品类别一起使用的正确方法是:

add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
    $categories   = array('ilutulestik-2');
    $has_category = false;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Check for product categories
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $has_category = true;
            break;
        }
    }

    // Testing output (display a notice)
    if ( $has_category ) { 
        wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
    }
}

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


如果您还需要检查带有产品类别的父条款,则可以改用:

// Custom conditional function that checks for parent product categories
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;
}

add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
    $categories   = array('ilutulestik-2');
    $has_category = false;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Check for product categories
        if ( has_product_categories( $cart_item['product_id'], $categories ) ) {
            $has_category = true;
            break;
        }
    }

    // Testing output (display a notice)
    if ( $has_category ) {
        wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
    }
}

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