WooCommerce条件自定义字段,如果购物车中的产品类别

时间:2015-10-20 01:44:14

标签: php wordpress woocommerce conditional custom-fields

好的,我很难过。我搜索并阅读了几篇帖子,其中包括相关帖子Checking products in cart based on category name woocommerce?,其中我从中获取了大部分代码,Woocommerce - Add filter to display (or hide) custom checkout field if product ID == #特定于产品ID,而不是类别ID。

我想显示sdc_custom_checkout_field,当且仅当目标类别ID(在这种情况下为237)在购物车中时才会显示。

我尝试注释掉sdc_custom_checkout_field函数并使用下面显示的简单测试,但不断得到“Nope!”,所以我认为查询不正确。

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

2 个答案:

答案 0 :(得分: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'] ) );
    }
}

答案 1 :(得分: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;
    }
}