简体   繁体   English

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

[英]WooCommerce Conditional Custom Fields if Product Category in Cart

OK, I'm stumped. 好吧,我很困惑。 I've searched and read several posts including the related post Checking products in cart based on category name woocommerce? 我搜索并阅读了几篇文章,包括相关的文章根据类别名称woocommerce检查购物车中的产品? from where I derived much of this code, and Woocommerce - Add filter to display (or hide) custom checkout field if product ID == # which is specific to Product IDs, not Category IDs. 我从中派生了大部分代码,并且从Woocommerce-如果产品ID ==#特定于产品ID,而不是类别ID,则添加过滤器以显示(或隐藏)自定义结帐字段

I want to display the sdc_custom_checkout_field if, and only if, the target category ID (237 in this case) is in the cart. 我仅在购物车中有目标类别ID(在这种情况下为237)时才显示sdc_custom_checkout_field。

I tried commenting out the sdc_custom_checkout_field function and using a simple test shown below, but kept getting "Nope!", so I assume the query is incorrect. 我尝试注释掉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;
}

Here's the test snippet: 这是测试代码段:

if ($item_in_cart === true) {echo 'YES';}
else {echo 'Nope!';}

I also replaced 我也换了

$item_in_cart $ item_in_cart

with

$product_in_cart $ product_in_cart

but it made no difference. 但这没什么区别。

********** EDIT RESPONSE TO @PRAFULLA ********** **********编辑对@PRAFULLA的回复**********

@Prafulla - thanks for your input. @Prafulla-感谢您的输入。 I appreciate it. 我很感激。 I modified my snippet as follows, incorporating yours, but was unable to get it to work. 我对代码段进行了如下修改,合并了您的代码段,但无法使其正常工作。 I'm a PHP newbie, so, no surprise. 我是PHP新手,所以,不足为奇。 Do you have additional advice? 您还有其他建议吗?

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

After much work, research, and the assistance of some paid help on another website, this is the working result: 经过大量工作,研究和其他网站上有偿帮助的帮助,这是工作结果:

// 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'] ) );
    }
}

This is how I get the job done in my case , please try it and note where you need to give your input on this code as It is not written for direct use. 这是我完成工作的方式,请尝试一下,并注意您需要在此代码上输入的内容,因为它不是为直接使用而编写的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce中基于产品类别的条件自定义结帐字段 - Conditional custom checkout fields based on product category in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category WooCommerce 3中定义的产品类别的自定义添加到购物车重定向 - custom add to cart redirection for a defined product category in WooCommerce 3 如果Woocommerce上的购物车中有特定产品,则显示自定义结帐字段 - Show custom checkout fields if a specific product is in cart on Woocommerce 将产品自定义字段添加到购物车并在 WooCommerce 中随处显示 - Add product custom fields to cart and display them everywhere in WooCommerce 在 Woocommerce 的购物车中显示多个产品自定义字段值 - Show multiple product custom fields values in cart on Woocommerce 在Woocommerce中每个商品名称旁边的购物车中显示产品自定义字段 - Display product custom fields in cart next to each item name in Woocommerce 在购物车和订单项目名称下显示 woocommerce 产品自定义字段 - Show woocommerce product custom fields under the cart and order item name Woocommerce AJAX API未将自定义产品字段发布到购物车 - Woocommerce AJAX API not Posting custom Product fields to Cart 产品类别内的“高级自定义字段中继器”(woocommerce) - Advanced Custom Fields Repeater inside product category (woocommerce)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM