Php 针对Woocommerce中的多个产品类别计数的ajax添加到购物车的JS警报
在Woocommerce中,当达到特定产品类别购物车中特定数量的产品时,我试图显示一个JavaScript“甜蜜警报”,当达到次要类别中特定数量的产品时,显示另一个警报 项目通过AJAX添加到购物车中,这就是为什么我想使用JavaScript警报(Sweet警报) e、 gPhp 针对Woocommerce中的多个产品类别计数的ajax添加到购物车的JS警报,php,jquery,ajax,wordpress,woocommerce,Php,Jquery,Ajax,Wordpress,Woocommerce,在Woocommerce中,当达到特定产品类别购物车中特定数量的产品时,我试图显示一个JavaScript“甜蜜警报”,当达到次要类别中特定数量的产品时,显示另一个警报 项目通过AJAX添加到购物车中,这就是为什么我想使用JavaScript警报(Sweet警报) e、 g 如果购物车包含5个“行李”类别的产品-显示警报 袋子 如果购物车包含“鞋”类别的5种产品-显示警告鞋 如果篮子已经有5个袋子,然后又添加了5只鞋-显示警惕鞋,但不要显示袋子,因为它们已经在篮子里了 下面的代码可以用于场景1和
// Wordpress Ajax: Get different cart items count
add_action( 'wp_ajax_nopriv_checking_items', 'checking_cart_items' );
add_action( 'wp_ajax_checking_items', 'checking_cart_items' );
function checking_cart_items() {
if( isset($_POST['id']) && $_POST['id'] > 0 ){
// Initialising variables
$count = 0;
$product_id = $_POST['id'];
$category = 'bags';
$category = 't-shirts';
// Loop through cart for product category
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
$count += $cart_item['quantity'];
//ELSE IF STATEMENT HERE?
}
}
// Only if the added item belongs to the defined product category
if( has_term( $category, 'product_cat', $_POST['id'] ) )
echo $count; // Returned value to jQuery
}
die(); // To avoid server error 500
}
// The Jquery script
add_action( 'wp_footer', 'items_check' );
function items_check() {
if(is_checkout()) return; // Except on checkout page
?>
<script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
<script type="text/javascript">
jQuery( function($){
// wc_add_to_cart_params is required to continue
if ( typeof wc_add_to_cart_params === 'undefined' )
return false;
$(document.body).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
// The Ajax request
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.ajax_url,
data: {
'action': 'checking_items',
'id' : $button.data( 'product_id' ) // Send the product ID
},
//ONLY DISPLAY ALERT IF TOTAL ITEMS IS FROM CATEGORY BAGS
success: function (response) {
console.log('response: '+response); // Testing: to be removed
if(response == 5 ){
//DISPLAY JAVASCRIPT ALERT
const toast = swal.mixin({
toast: true,
showConfirmButton: false,
timer: 3000
});
toast({
type: 'success',
title: '5 Items Added!'
})
}
}
});
});
});
</script>
<?php
}
//Wordpress Ajax:获取不同的购物车项目计数
添加操作(“wp\u ajax\u nopriv\u checking\u items”、“checking\u cart\u items”);
添加操作(“wp\u ajax\u检查项目”、“检查购物车项目”);
功能检查\u购物车\u项目(){
如果(isset($\u POST['id'])&&$\u POST['id']>0){
//初始化变量
$count=0;
$product_id=$_POST['id'];
$category='行李';
$category='t恤';
//在购物车中循环查找产品类别
foreach(WC()->cart->get_cart()作为$cart_项目){
if(具有术语($category,$product\u cat',$cart\u item['product\u id')){
$count+=$cart_项目['quantity'];
//这里还有其他的IF语句吗?
}
}
//仅当添加的项目属于定义的产品类别时
如果(有术语($category,$product\u cat',$u POST['id']))
echo$count;//返回给jQuery的值
}
die();//避免服务器错误500
}
//Jquery脚本
添加操作('wp_footer','items_check');
功能项检查(){
if(is_checkout())返回;//在签出页面上除外
?>
jQuery(函数($){
//wc_添加_至_购物车参数需要继续
如果(wc类型添加到购物车参数==='undefined')
返回false;
$(document.body).on('added_to_cart',函数(事件,片段,cart_hash,$按钮){
//Ajax请求
$.ajax({
键入:“POST”,
url:wc_add_to_cart_params.ajax_url,
数据:{
“操作”:“检查项目”,
'id':$button.data('product_id')//发送产品id
},
//仅当物品总数来自分类行李时才显示警报
成功:功能(响应){
console.log('response:'+response);//测试:要删除
如果(响应==5){
//显示JAVASCRIPT警报
const toast=swal.mixin({
吐司:是的,
showconfixton:false,
计时器:3000
});
吐司({
键入:“成功”,
标题:“添加了5项!”
})
}
}
});
});
});
要使其适用于两种不同的产品类别,当其中一种产品类别的5个项目已添加到购物车时,会显示甜蜜警报:
// Wordpress Ajax: Get different cart items count
add_action( 'wp_ajax_nopriv_checking_items', 'checking_cart_items' );
add_action( 'wp_ajax_checking_items', 'checking_cart_items' );
function checking_cart_items() {
if( isset($_POST['id']) && $_POST['id'] > 0 ){
// Initialising variables
$counts = array();
$product_id = $_POST['id'];
$categories = array('bags', 'shoes');
// Loop through cart for product categories count
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $categories[0], 'product_cat', $cart_item['product_id'] ) )
$counts[0] += $cart_item['quantity'];
if ( has_term( $categories[1], 'product_cat', $cart_item['product_id'] ) )
$counts[1] += $cart_item['quantity'];
}
// Return the product category count that belongs to the added item
if( has_term( $categories[0], 'product_cat', $product_id ) )
echo json_encode(array( strtoupper($categories[0]) => $counts[0])); // Returned value to jQuery
if( has_term( $categories[1], 'product_cat', $product_id ) )
echo json_encode(array( strtoupper($categories[1]) => $counts[1])); // Returned value to jQuery
}
die(); // To avoid server error 500
}
// The Jquery script
add_action( 'wp_footer', 'items_check' );
function items_check() {
if(is_checkout()) return; // Except on checkout page
?>
<script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
<script type="text/javascript">
jQuery( function($){
// wc_add_to_cart_params is required to continue
if ( typeof wc_add_to_cart_params === 'undefined' )
return false;
$(document.body).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
// The Ajax request
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.ajax_url,
data: {
'action': 'checking_items',
'id' : $button.data('product_id') // Send the product ID
},
//ONLY DISPLAY ALERT IF TOTAL ITEMS IS FROM CATEGORY BAGS
success: function (response) {
$.each( JSON.parse(response), function( category, count ) {
if( count == 5 ){
// Display javascript alert
const toast = swal.mixin({
toast: true,
showConfirmButton: false,
timer: 3000
});
toast({
type: 'success',
title: "You've added 5 "+category+"!"
});
}
// The below line is just for testing: to be removed
console.log('category: '+category+' | count: '+count);
});
}
});
});
});
</script>
<?php
}
//Wordpress Ajax:获取不同的购物车项目计数
添加操作(“wp\u ajax\u nopriv\u checking\u items”、“checking\u cart\u items”);
添加操作(“wp\u ajax\u检查项目”、“检查购物车项目”);
功能检查\u购物车\u项目(){
如果(isset($\u POST['id'])&&$\u POST['id']>0){
//初始化变量
$counts=array();
$product_id=$_POST['id'];
$categories=数组('bags','shoes');
//循环购物车以进行产品类别计数
foreach(WC()->cart->get_cart()作为$cart_项目){
如果(有术语($categories[0],'product\u cat',$cart\u item['product\u id']))
$counts[0]+=$cart_项目['quantity'];
如果(有术语($categories[1],'product\u cat',$cart\u item['product\u id'))
$counts[1]+=$cart_项目['quantity'];
}
//返回属于添加项目的产品类别计数
if(有_术语($categories[0],'product_cat',$product_id))
echo json_encode(数组(strtoupper($categories[0])=>counts[0]);//向jQuery返回值
if(有术语($categories[1],'product\u cat',$product\u id))
echo json_encode(数组(strtoupper($categories[1])=>counts[1]);//返回给jQuery的值
}
die();//避免服务器错误500
}
//Jquery脚本
添加操作('wp_footer','items_check');
功能项检查(){
if(is_checkout())返回;//在签出页面上除外
?>
jQuery(函数($){
//wc_添加_至_购物车参数需要继续
如果(wc类型添加到购物车参数==='undefined')
返回false;
$(document.body).on('added_to_cart',函数(事件,片段,cart_hash,$按钮){
//Ajax请求
$.ajax({
键入:“POST”,
url:wc_add_to_cart_params.ajax_url,
数据:{
“操作”:“检查项目”,
'id':$button.data('product_id')//发送产品id
},
//仅当物品总数来自分类行李时才显示警报
成功:功能(响应){
$.each(JSON.parse(响应)、函数(类别、计数){
如果(计数=5){
//显示javascript警报
const toast=swal.mixin({
吐司:是的,