Php 实时更新产品类别购物车项目计数,无需在Woocommerce中重新加载
我创建了一个快捷码,用于统计添加到购物车中的属于“72”产品类别的产品数量 我正在使用自定义条件函数has_product_category(),它来自以下回答线程:Php 实时更新产品类别购物车项目计数,无需在Woocommerce中重新加载,php,ajax,wordpress,woocommerce,cart,Php,Ajax,Wordpress,Woocommerce,Cart,我创建了一个快捷码,用于统计添加到购物车中的属于“72”产品类别的产品数量 我正在使用自定义条件函数has_product_category(),它来自以下回答线程: 这是我的密码: function cat_cart_count_bottiglie() { $bottiglie = 0; // Iterating through each cart item foreach ( WC()->cart->get_cart() as $cart_item
这是我的密码:
function cat_cart_count_bottiglie() {
$bottiglie = 0;
// Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], $category_ids = array( 72 ) ) ) {
$bottiglie += $cart_item['quantity'];
}
}
return $bottiglie;
}
add_shortcode('bottiglie', 'cat_cart_count_bottiglie');
代码工作正常
但是,只有在添加到购物车后刷新页面时,此快捷码计数才会更新,对于Ajax添加到购物车或在购物车页面中删除项目或更改项目数量时,此快捷码计数不起作用
有什么方法可以立即更新吗?您需要一个javascript来运行对DOM的实时更改,但是wordpress短代码不能与ajax一起使用(我想,如果我错了,请纠正我)。但是,基本上,您需要ajax再次获取短代码,或者在单击“添加到购物车”后手动更改DOM。以下代码将ajax刷新您的短代码自定义产品类别“72”项目计数:
// Custom conditional function that checks also for parent product category terms
function has_product_category( $product_id, $category_ids, $taxonomy = 'product_cat' ) {
$term_ids = array(); // Initializing
// 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 ){
$term_ids[] = $term->parent; // Set the parent product category
$term_ids[] = $term->term_id;
} else {
$term_ids[] = $term->term_id;
}
}
return array_intersect( $category_ids, array_unique($term_ids) );
}
// Custom function that count cart items remaining to a product_category
function get_bottiglie_count( $term_ids ){
$count = 0; // Initializing
// Loop through each cart item
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], $term_ids ) ) {
$count += $cart_item['quantity'];
}
}
return $count;
}
// Ajax refressing count
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_bottiglie_count', 50, 1 );
function refresh_bottiglie_count( $fragments ){
$fragments['#bottiglie-count'] = do_shortcode( "[bottiglie]" );
return $fragments;
}
// Shortcode that display the count cart items remaining to a product_category
add_shortcode('bottiglie', 'shortcode_bottiglie_count');
function shortcode_bottiglie_count( $atts ) {
return '<span id="bottiglie-count">' . get_bottiglie_count( array(72) ) . '</span>';
}
//还检查父产品类别术语的自定义条件函数
函数具有产品类别($product\u id、$category\u id、$taxonomy='product\u cat')){
$term_ids=array();//正在初始化
//循环查看当前产品类别术语,以仅获取父主类别术语
foreach(获取\u术语($product\u id,$TAXINOLY)作为$term){
如果($term->parent>0){
$term\u id[]=$term->parent;//设置父产品类别
$term\u id[]=$term->term\u id;
}否则{
$term\u id[]=$term->term\u id;
}
}
返回数组_intersect($category_id,数组_unique($term_id));
}
//自定义函数,用于统计产品类别中剩余的购物车项目
函数get\u bottiglie\u count($term\u id){
$count=0;//正在初始化
//循环浏览每个购物车项目
foreach(WC()->cart->get_cart()作为$cart_项目){
if(具有商品类别($cart\u item['product\u id',$term\u id)){
$count+=$cart_项目['quantity'];
}
}
返回$count;
}
//重压缩计数
添加过滤器(“woocommerce\u添加到购物车\u片段”,“刷新\u bottiglie\u计数”,50,1);
函数刷新\u bottiglie\u计数($fragments){
$fragments['#bottiglie count']=do_短码(“[bottiglie]”);
返回$fragments;
}
//显示产品类别剩余购物车项目计数的快捷码
添加_shortcode('bottiglie','shortcode_bottiglie_count');
函数短码\u bottiglie\u计数($atts){
返回''获取计数(数组(72))。';
}
代码进入活动子主题(活动主题)的function.php文件。已测试并正常工作。我在移动菜单中也输入了相同的快捷码。只是在我更改购物车中的数量或添加新产品时不要更新。有办法吗?@Vdgaetano我认为你的手机菜单在某种程度上停止了这个过程,没有人能真正回答这种问题,因为这是你的主题所特有的(因为这种行为在不同的主题中是不同的)。一些插件也会产生一些问题,比如。