【问题标题】:WooCommerce Conditional Custom Fields if Product Category in CartWooCommerce 条件自定义字段(如果购物车中的产品类别)
【发布时间】:2016-01-18 12:35:32
【问题描述】:

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

当且仅当目标类别 ID(在本例中为 237)在购物车中时,我想显示 sdc_custom_checkout_field。

我尝试注释掉 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;
}

这里是测试sn-p:

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

我也换了

$item_in_cart

$product_in_cart

但这并没有什么不同。

********** 编辑回复@PRAFULLA **********

@Prafulla - 感谢您的意见。我很感激。我修改了我的 sn-p 如下,合并了你的,但无法让它工作。我是 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;
}

【问题讨论】:

    标签: php wordpress woocommerce conditional custom-fields


    【解决方案1】:

    经过大量工作、研究以及在另一个网站上的一些付费帮助的帮助,这是工作结果:

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

    【讨论】:

      【解决方案2】:

      这就是我完成工作的方式,请尝试一下,并注意您需要在哪里输入此代码,因为它不是为直接使用而编写的。

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

      【讨论】:

      • 比普拉富拉。不确定您是否收到我的编辑通知,所以我在这里发布。
      • 你的情况可以var_dump( $product_types_in_cart[0] )吗?
      • 假设我将该行附加到我的 functions.php 文件的末尾,它输出/返回 NULL。
      • 像这样使用add_action( 'template_redirect', 'your_template_direction_function' ); function your_template_direction_function() { global $woocommerce; if ( is_cart() ) { if ( is_subscription_in_cart() ) { wp_redirect(get_permalink(get_option( 'woocommerce_checkout_page_id' )) ); exit; } } }
      • 最好在购物车或结帐页面挂钩上使用它。
      猜你喜欢
      • 1970-01-01
      • 2019-05-11
      • 2016-10-18
      • 1970-01-01
      • 2018-08-28
      • 2018-09-30
      • 2018-07-16
      • 2019-12-13
      • 2019-01-17
      相关资源
      最近更新 更多