Woocommerce 3 中按产品类别自定义购物车项目计数
Custom cart item counts by product category in Woocommerce 3
我正在尝试按产品类别显示 Woocommerce 购物车数量。
- 我有两个类别:“蛋白质”和“pantry”
- 我想按项目类别显示计数。所以如果有 2 protein 和 4 pantry,我希望这两个项目类别计数彼此相邻显示,更新为 ajax.
- 例如最后的外观会说:购物车中的蛋白质 2 和 购物车中的 PANTRY 4(取决于购物车中商品类型的数量)
我找到了这个片段 ,它显示了给定类别的购物车数量,但我无法在网页上正确显示它。目前它只显示购物车中的商品总数:
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,这样我就可以嵌入页面的任何位置并从那里进行自定义:
<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>
提前致谢!
The main problem comes from the refreshed woocommerce ajax cart fragments that use by default "cart-contents"
class and the existing content with the refreshed default cart count.
所以我们需要:
- 以不同的方式重命名 class,
- 在特定的挂钩函数中添加内容(添加到购物车时刷新)。
我还重新访问、简化和压缩了您的功能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 文件。经过测试并有效。
用法
1) 要将计数器放在 php 文件的 html 代码中,您将使用:
<?php echo display_cart_counters(); ?>
2) 要在任何 php 代码中使用计数器,您将调用函数 **display_cart_counters()**
.
我正在尝试按产品类别显示 Woocommerce 购物车数量。
- 我有两个类别:“蛋白质”和“pantry”
- 我想按项目类别显示计数。所以如果有 2 protein 和 4 pantry,我希望这两个项目类别计数彼此相邻显示,更新为 ajax.
- 例如最后的外观会说:购物车中的蛋白质 2 和 购物车中的 PANTRY 4(取决于购物车中商品类型的数量)
我找到了这个片段
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,这样我就可以嵌入页面的任何位置并从那里进行自定义:
<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>
提前致谢!
The main problem comes from the refreshed woocommerce ajax cart fragments that use by default
"cart-contents"
class and the existing content with the refreshed default cart count.
所以我们需要:
- 以不同的方式重命名 class,
- 在特定的挂钩函数中添加内容(添加到购物车时刷新)。
我还重新访问、简化和压缩了您的功能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 文件。经过测试并有效。
用法
1) 要将计数器放在 php 文件的 html 代码中,您将使用:
<?php echo display_cart_counters(); ?>
2) 要在任何 php 代码中使用计数器,您将调用函数 **display_cart_counters()**
.