2015-10-20 66 views
2

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; 
} 

回答

5

經過大量的工作,研究,以及對其他網站的一些支付幫助援助,這是工作的結果:

// Add the field to the checkout 

add_action('woocommerce_before_order_notes', 'sdc_custom_checkout_field'); 

function sdc_custom_checkout_field($checkout) { 
    if(check_product_category()){ 
     echo '<div id="sdc_checkout_field"><h3>' . __('Duplicate Card Information' . '</h3><br>'); 

     woocommerce_form_field('dupecard_location', array(
      'type' => 'text', 
      'required' => true, 
      'class' => array('dupecard-location form-row-wide'), 
      'label' => __('Course Location'), 
     ), $checkout->get_value('dupecard_location')); 

     woocommerce_form_field('dupecard_instructor', array(
      'type' => 'text', 
      'required' => true, 
      'class' => array('dupecard-instructor form-row-wide'), 
      'label' => __('Instructor Name'), 
     ), $checkout->get_value('dupecard_instructor')); 

     woocommerce_form_field('dupecard_requestor_name', array(
      'type' => 'text', 
      'required' => true, 
      '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', 
      'required' => true, 
      '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', 
      'required' => true, 
      'class' => array('dupecard-requestor-phone form-row-wide'), 
      'label' => __('Requestor Phone'), 
     ), $checkout->get_value('dupecard_requestor_phone')); 
     echo '</div>'; 
    } 
} 

function check_product_category(){ 
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 
     $product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key); 
     if(is_category_in_cart($product_id)){ 
      return true;  
     } 
    } 
    return false; 
} 

function is_category_in_cart($product_id){ 
    return has_term(237,'product_cat', get_post($product_id)); 
} 

/*Save to DB as post meta*/ 
add_action('woocommerce_checkout_update_order_meta',     'my_custom_checkout_field_update_order_meta'); 

function my_custom_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST['dupecard_location'])) { 
    update_post_meta($order_id, 'dupecard_location', sanitize_text_field($_POST['dupecard_location'])); 
    } 
    if (! empty($_POST['dupecard_instructor'])) { 
    update_post_meta($order_id, 'dupecard_instructor', sanitize_text_field($_POST['dupecard_instructor'])); 
    } 
    if (! empty($_POST['dupecard_requestor_name'])) { 
    update_post_meta($order_id, 'dupecard_requestor_name', sanitize_text_field($_POST['dupecard_requestor_name'])); 
    } 
    if (! empty($_POST['dupecard_requestor_email'])) { 
    update_post_meta($order_id, 'dupecard_requestor_email', sanitize_text_field($_POST['dupecard_requestor_email'])); 
    } 
    if (! empty($_POST['dupecard_requestor_phone'])) { 
    update_post_meta($order_id, 'dupecard_requestor_phone', sanitize_text_field($_POST['dupecard_requestor_phone'])); 
    } 
} 
+0

非常感謝你的男人,你是一個拯救生命的人:) – Anil

0

這是我如何得到我的情況下完成任務,請嘗試一下,記下你需要給這個代碼的輸入,因爲它不是直接使用寫的。

function is_category_in_cart($your_product_category = null){ 
    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; 
    } 
} 
+0

比Prafulla。不知道你是否收到我的編輯通知,所以我在這裏發佈。 – KillerDesigner

+0

你能否請'var_dump($ product_types_in_cart [0])'在你的情況下? –

+0

假設我將該行追加到我的functions.php文件的末尾,它輸出/返回NULL。 – KillerDesigner

相關問題