Php Woocommerce 3中按产品类别列出的自定义购物车项目计数
我正在尝试按产品类别显示Woocommerce购物车计数Php Woocommerce 3中按产品类别列出的自定义购物车项目计数,php,ajax,wordpress,woocommerce,cart,Php,Ajax,Wordpress,Woocommerce,Cart,我正在尝试按产品类别显示Woocommerce购物车计数 我有两类:“蛋白质”和“食品储藏室” 我想按项目类别显示计数。因此,如果有2蛋白质和4食品储藏室,我希望这两个项目类别计数彼此相邻显示,并使用ajax进行更新 e、 g.最后一眼会显示:购物车中的蛋白质2和购物车中的食品储藏室4(取决于购物车中的物品类型数量) 我找到了这个显示给定类别的购物车计数的代码段,但在网页上正确显示它时遇到了问题。当前,它仅显示购物车中的项目总数: function cat_cart_count( $cat_
- 我有两类:“蛋白质”和“食品储藏室”
- 我想按项目类别显示计数。因此,如果有2蛋白质和4食品储藏室,我希望这两个项目类别计数彼此相邻显示,并使用ajax进行更新
- e、 g.最后一眼会显示:购物车中的蛋白质2和购物车中的食品储藏室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>
提前谢谢
主要问题来自刷新的ajax购物车片段,这些片段默认使用“购物车内容”类和具有刷新的默认购物车计数的现有内容
因此,我们需要:
- 以不同的方式重命名该类
- 将内容(在添加到购物车时刷新)添加到特定的挂钩函数中
cat\u cart\u 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;
}
//统计特定产品类别购物车项目的实用程序函数
功能分类车计数($term){
$count=0;//正在初始化
//循环浏览购物车项目
foreach(WC()->cart->get_cart()作为$cart_项目){
如果(有术语($term,‘product_cat’,$cart_item['product_id'))
$count+=$cart_项目['quantity'];
}
//返回类别计数
返回$count==0?错误:$count;
}
//显示购物车项目计数器的实用程序功能
功能显示\购物车\计数器(){
ob_start();
$link=wc_get_cart_url();
$title=u uu('View your shopping cart','woocommerce');
$count=u uu('0项','woocommerce');
//在第一页加载时显示
回声';
返回ob_get_clean();
}
//在“添加到购物车”上显示刷新的内容
添加过滤器('woocommerce\u add\u to\u cart\u fragments','counters\u add\u to\u cart\u fragments',30,1);
函数计数器\u添加\u到\u购物车\u片段($fragments){
ob_start();
$term1=‘蛋白质’;
$count1=猫车计数($term1);
$output=$count1?sprintf(uuuuz('%d%s','woocommerce'),$count1,$term1)。'-':'';
$term2=‘食品储藏室’;
$count2=猫车计数($term2);
$output.=$count2?sprintf(uuu(“%d%s”,“woocommerce”),$count2,$term2)。“-”:”;
$count=WC()->cart->get_cart_contents_count();
$output.=$count?sprintf(_n(“(%d项)”,(%d项)”,$count),$count):_('0项','woocommerce');
$link=wc_get_cart_url();
$title=u uu('View your shopping cart','woocommerce');
//任何购物车项目事件上的替换显示
?>
工作完美-感谢您对“购物车内容”和更新的woocommerce ajax片段的解释显示我可以删除计数器链接吗?以便它仅显示为文本…例如,不显示clickable@Giuls只需删除href=“”.$link。“
和href=“”
<?php echo display_cart_counters(); ?>