本文介紹了根據(jù)類別名稱woocommerce檢查購物車中的產(chǎn)品?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
如果我的購物車中有特定類別的產(chǎn)品,我正在嘗試觸發(fā) echo 語句,這是我的代碼:
I'm trying to trigger an echo statement if a certain category of product is in my cart, here's my code:
<?php
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$item_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->term_id;
}
if ( $_categoryid == 'name_of_category' ) {
//book is in cart!
$item_in_cart = true;
}
}
if ($item_in_cart === true) {echo 'YES';}
else {echo 'Nope!';}
?>
知道我做錯了什么嗎?我的購物車中確實(shí)有name_of_category"產(chǎn)品,我想要一個不錯的 Yes 回應(yīng)!
Any idea as to what i'm doing wrong? I do have 'name_of_category' products in my cart, i'd like a nice Yes echoed!
謝謝!
推薦答案
根據(jù) Barrell 的建議編輯了我的代碼并回應(yīng)了賓果游戲"!
Edited my code following Barrell's advice and echo 'Bingo'!
效果很好,代碼如下:
function check_product_in_cart() {
//Check to see if user has product in cart
global $woocommerce;
//assigns a default negative value
// categories targeted 17, 18, 19
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if (( $_categoryid === 17 ) || ( $_categoryid === 18 ) || ( $_categoryid === 19 )) {
//category is in cart!
$product_in_cart = true;
}
}
}
return $product_in_cart;
}
希望能幫到人!
這篇關(guān)于根據(jù)類別名稱woocommerce檢查購物車中的產(chǎn)品?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!