0
我試圖讓我的結帳只顯示幾個表單字段,只要客戶從購物車中的某個類別購買產品。到目前爲止,我能夠使用我在wordpress上找到的代碼獲得一半。我添加了一點,以檢查類別,當我運行它時,它已經工作,但在加載頁面的其餘部分之前超時。如何檢查類別產品是否在購物車中
<?php
function wordimpress_is_conditional_product_in_cart($product_id) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$book_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 === 27) {
//book is in cart!
$book_in_cart = true;
}
}
return $book_in_cart;
}
?>
有沒有更好的方法來做到這一點,或者做到這一點的方法,實際上有效嗎?