Woocommerce 3中按产品类别的自定义购物车项计数 [英] Custom cart item counts by product category in Woocommerce 3

查看:80
本文介绍了Woocommerce 3中按产品类别的自定义购物车项计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按产品类别显示Woocommerce购物车计数。

I am trying to display the Woocommerce cart count by product category.


  • 我有两类: 蛋白质储藏室

  • 我想按项目类别显示计数。因此,如果有 2 蛋白 4 储藏室,我希望将这两个项目类别计数显示在旁边彼此,用ajax更新。

  • 例如最终外观应为:购物车中的蛋白质2 购物车中的PANTRY 4 (取决于购物车中的物品类型)

  • I have two categories: "protein" and "pantry"
  • I want to display the count by item category. So if there are 2 protein and 4 pantry, I want those two item category counts to be displayed next to each other, updating with ajax.
  • e.g. the final look would say: PROTEIN 2 in cart and PANTRY 4 in cart (depending on the amount of item types in the cart)

我在此处找到了此代码段会显示给定类别的购物车数量,但是我无法在网页上正确显示它。当前它仅显示购物车中的物品总数:

I have found this snippet here which displays the cart count for a given category, but I am having trouble displaying it on the web page correctly. Currently It just displays the total amount of items in the cart:

function cat_cart_count( $cat_name ) {

global $woocommerce; $cat_count = 0; 

// For each product in the cart
foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
    $_product_id = $values['product_id']; // product ID
    $_product_qty = $values['quantity']; // product quantity

    // Getting categories of the product (could be more than one)
    $terms = get_the_terms( $_product_id, 'product_cat' );

    // Checking this product has a category
    if ( $terms && ! is_wp_error( $terms ) ) { 
        $term_name = array();

        // For each category of that product
        foreach($terms as $term) {

            // Adding the category name to an array
            $term_name[] = $term->name;

            // if the product has $cat_name category
            if ( in_array( $cat_name, $term_name ) ) {

                // add 1 x product quantity to the count
                $cat_count =+ 1 * $_product_qty;
            }
        }
    }
}
// Returning category count
if ( $cat_count != 0 ) {
    return $cat_count;
} else {
    return '';
}
}

我试图将计数器显示为HTML,所以我可以将其嵌入到页面的任何位置并从那里进行自定义:

I am trying to just display the counters as HTML so that I can embed on the page anywhere and customise from there:

<a class="cart-contents" href="<?php echo WC()->cart->get_cart_contents_count(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d protein', '%d protein', cat_cart_count( "protein" ) ), cat_cart_count( "protein" ) ); ?> - <?php echo sprintf (_n( '%d pantry', '%d pantry', cat_cart_count( "pantry" ) ), cat_cart_count( "pantry" ) ); ?> - <?php echo WC()->cart->get_cart_contents_count(); ?></a>

预先感谢!

推荐答案


主要问题来自刷新的woocommerce ajax购物车片段,默认情况下使用购物车内容 类和

所以我们需要:


  • 以不同的方式重命名该类,

  • 将内容(要刷新后添加到购物车中)添加到特定的挂钩中函数。

  • rename that class differently,
  • add the content (to be refreshed on add to cart) in a specific hooked function.

我也重新研究,简化和压缩了函数 cat_cart_count()

I have also revisited, simplified and compacted your function cat_cart_count().

完整代码:

// Utility function that count specific product category cart items
function cat_cart_count( $term ) {
    $count = 0; // Initializing

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( has_term( $term, 'product_cat', $cart_item['product_id'] ) )
            $count += $cart_item['quantity'];
    }
    // Returning category count
    return $count == 0 ? false : $count;
}

// Utility function that displays the cart items counters
function display_cart_counters() {
    ob_start();

    $link  = wc_get_cart_url();
    $title = __( 'View your shopping cart', 'woocommerce' );
    $count = __( '0 item', 'woocommerce');

    // Displayed on first page load
    echo '<a class="cart-contents-cat" href="' . $link . '" title="' . $title . '">' . $count . '</a>';

    return ob_get_clean();
}

// Displaying refreshed content on add-to cart
add_filter( 'woocommerce_add_to_cart_fragments', 'counters_add_to_cart_fragment', 30, 1 );
function counters_add_to_cart_fragment( $fragments ) {
    ob_start();

    $term1   = 'protein';
    $count1  = cat_cart_count( $term1 );
    $output  = $count1 ? sprintf ( __( '%d %s', 'woocommerce'), $count1, $term1 ) . ' - ' : '';

    $term2   = 'pantry';
    $count2  = cat_cart_count( $term2 );
    $output .= $count2 ? sprintf ( __( '%d %s', 'woocommerce'), $count2, $term2 ) . ' - ' : '';

    $count   = WC()->cart->get_cart_contents_count();
    $output .= $count ? sprintf (_n( '(%d item)', '(%d items)', $count ), $count ) : __( '0 item', 'woocommerce');

    $link    = wc_get_cart_url();
    $title   = __( 'View your shopping cart', 'woocommerce' );

    // Replacement display on any cart item events
    ?>
    <a class="cart-contents-cat" href="<?php echo $link; ?>" title="<?php echo $title ?>"><?php echo $output; ?></a>
    <?php
    $fragments['a.cart-contents-cat'] = ob_get_clean();

    return $fragments;
}

代码进入活动子主题(或活动主题)的function.php文件

Code goes in function.php file of your active child theme (or active theme).Tested and works.

使用情况

1)要将计数器放在php文件的html代码中,您将使用:

1) To place the counter in the html code of a php file you will use:

<?php echo display_cart_counters(); ?>

2)要在任何php代码中使用计数器,您将调用函数 ** display_cart_counters()**

2) To use the counter inside any php code you will call the function **display_cart_counters()**.

这篇关于Woocommerce 3中按产品类别的自定义购物车项计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆