OK,我很爲難。我已經搜索並閱讀了幾篇文章,其中包括相關帖子Checking products in cart based on category name woocommerce?,其中我從中得到了大部分代碼,而Woocommerce - Add filter to display (or hide) custom checkout field if product ID == #對於產品ID而非類別ID是特定的。WooCommerce有條件的自定義字段如果車產品類別
我想顯示如果sdc_custom_checkout_field,且僅當目標類別ID(237在這種情況下)在購物車中。
我嘗試註釋掉sdc_custom_checkout_field功能和使用如下一個簡單的測試,但一直得到「不!」,所以我想查詢不正確。
add_action('woocommerce_before_order_notes', 'sdc_custom_checkout_field');
function sdc_custom_checkout_field($checkout) {
//Check if Product in Cart
//$product_in_cart = check_product_in_cart();
//Product is in cart so show additional fields
if ($product_in_cart === true) {
echo '<div id="my_custom_checkout_field"><h3>' . __('Duplicate Card Information' . '</h3><br>');
woocommerce_form_field('dupecard_location', array(
'type' => 'text',
'class' => array('dupecard-location form-row-wide'),
'label' => __('Course Location'),
), $checkout->get_value('dupecard_location'));
woocommerce_form_field('dupecard_instructor', array(
'type' => 'text',
'class' => array('dupecard-instructor form-row-wide'),
'label' => __('Instructor Name'),
), $checkout->get_value('dupecard_instructor'));
woocommerce_form_field('dupecard_requestor_name', array(
'type' => 'text',
'class' => array('dupecard-requestor-name form-row-wide'),
'label' => __('Requestor Name'),
), $checkout->get_value('dupecard_requestor_name'));
woocommerce_form_field('dupecard_requestor_email', array(
'type' => 'text',
'class' => array('dupecard-requestor-email form-row-wide'),
'label' => __('Requestor Email'),
), $checkout->get_value('dupecard_requestor_email'));
woocommerce_form_field('dupecard_requestor_phone', array(
'type' => 'text',
'class' => array('dupecard-requestor-phone form-row-wide'),
'label' => __('Requestor Phone'),
), $checkout->get_value('dupecard_requestor_phone'));
echo '</div>';
}
}
function check_product_in_cart() {
//Check to see if user has product in cart
global $woocommerce;
//assign default negative value
$product_in_cart = false;
// start cart items fetch loop
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
$cat_ids = array();
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
if(in_array(237, (array)$cat_ids)) {
//category is in cart!
$product_in_cart = true;
}
}
return $product_in_cart;
}
這裏的測試片段:
if ($item_in_cart === true) {echo 'YES';}
else {echo 'Nope!';}
我也取代
$ item_in_cart
與
$ product_in_cart
但它沒有區別。
**********編輯響應@PRAFULLA **********
@Prafulla - 感謝您的輸入。我很感激。我修改了我的代碼片段,併入了您的代碼,但無法使其工作。我是一個PHP新手,所以,毫不奇怪。你有額外的建議嗎?
add_action('woocommerce_before_order_notes', 'sdc_custom_checkout_field');
function sdc_custom_checkout_field($checkout) {
//Check if Product in Cart
$your_product_category = is_category_in_cart();
//Product is in cart so show additional fields
if ($your_product_category === true) {
echo '<div id="my_custom_checkout_field"><h3>' . __('Duplicate Card Information' . '</h3><br>');
woocommerce_form_field('dupecard_location', array(
'type' => 'text',
'class' => array('dupecard-location form-row-wide'),
'label' => __('Course Location'),
), $checkout->get_value('dupecard_location'));
woocommerce_form_field('dupecard_instructor', array(
'type' => 'text',
'class' => array('dupecard-instructor form-row-wide'),
'label' => __('Instructor Name'),
), $checkout->get_value('dupecard_instructor'));
woocommerce_form_field('dupecard_requestor_name', array(
'type' => 'text',
'class' => array('dupecard-requestor-name form-row-wide'),
'label' => __('Requestor Name'),
), $checkout->get_value('dupecard_requestor_name'));
woocommerce_form_field('dupecard_requestor_email', array(
'type' => 'text',
'class' => array('dupecard-requestor-email form-row-wide'),
'label' => __('Requestor Email'),
), $checkout->get_value('dupecard_requestor_email'));
woocommerce_form_field('dupecard_requestor_phone', array(
'type' => 'text',
'class' => array('dupecard-requestor-phone form-row-wide'),
'label' => __('Requestor Phone'),
), $checkout->get_value('dupecard_requestor_phone'));
echo '</div>';
}
}
function is_category_in_cart($your_product_category = 237){
global $woocommerce;
$products_in_cart = $woocommerce->cart->get_cart();
$product_types_in_cart = array_column($products_in_cart, 'data');
//if ( $product_types_in_cart[0]->product_type == 'subscription') { this is what I have tested
if ( $product_types_in_cart[0]->product_cat == $your_product_category) {
return true;
}
return $your_product_category;
}
非常感謝你的男人,你是一個拯救生命的人:) – Anil