Woocommerce 结帐页面上获取购物车中特定商品的数量

我试图在结帐屏幕中显示自定义字段,这些字段以 2 件事为条件。

如果产品#1769 和/或产品#1770 在客户的购物车中。>当前购物车中的#1769 和/或#1770 的数量将决定要显示的自定义字段的数量。

现在我已经通过以下方式处理了#1:

/**
* Check to see what is in the cart
*
* @param $product_id
*
* @return bool
*/
function conditional_product_in_cart( $product_id ) {
    //Check to see if user has product in cart
    global $woocommerce;

    $check_in_cart = false;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if ( $_product->id === $product_id ) {
            $check_in_cart = true;
        }       
    }
    return $check_in_cart;
}
function checkout_register_names( $checkout ) {

$check_in_cart = conditional_product_in_cart(1769); 

    // Product id 1769 is in cart so show custom fields
    if ($check_in_cart === true ) {
    // Display custom fields for 1769...
    woocommerce_form_field( 'golf_field_one', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Golfer #1'),
    'placeholder'       => __('Name'),
    ), $checkout->get_value( 'golf_field_one' ));
    woocommerce_form_field( 'golf_field_two', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Golfer #2'),
    'placeholder'       => __('Name'),
    ), $checkout->get_value( 'golf_field_two' ));
    //etc...
    }
$check_in_cart = conditional_product_in_cart(1770); 

    // Product id 1770 is in cart so show custom fields
    if ($check_in_cart === true ) {
    // Display custom fields for 1770...
    woocommerce_form_field( 'dinner_field_one', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Dinner Name #1'),
    'placeholder'       => __('Name'),
    ), $checkout->get_value( 'dinner_field_one' ));
    woocommerce_form_field( 'dinner_field_two', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Dinner Name #2'),
    'placeholder'       => __('Name'),
    ), $checkout->get_value( 'dinner_field_two' ));
    //etc...
    }
}

以上代码将有条件地显示我在每个产品下设置的所有 woocommerce_form_field(),仅当该产品位于客户的购物车中时。现在我需要做的是根据购物车中每种产品#1769 或#1770 的数量显示一定数量的 woocommerce_form_field()。

因此如果有两(2)个产品#1769,,则应显示两个字段。如果有两(2)个产品#1769 和一个(1)产品#1770,则应显示三个总字段(两个用于产品#1769,一个用于产品#1770)。

在任何给定客户的购物车中,每个产品最多只会添加四个,因此将每个表单字段包装在 if()中并检查以下内容并不是一件大事:

if([quantity of product 1769] >= 1) {
    show first woocommerce_form_field()
}
if([quantity of product 1769 >= 2) {
    show second woocommerce_form_field()
} //etc... 
// Repeat for product 1770...

我尝试添加$qty_in_cart = $values [‘quantity’];在第一个函数 conditional_product_in_cart 中的 foreach(),但似乎不想给我任何东西。当我检查是否(isset($qty_in_cart))时,它没有设置。

我觉得我很亲密,但是我无法弄清楚我错过了什么,任何帮助将不胜感激。我正在执行同样的任务,我在 WooTickets 和 WooCommerce 上使用的代码片段可能对您有所帮助:

if (    
        in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && 
        in_array( 'wootickets/wootickets.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
    ) {
    /**
     * Add the field to the checkout
     **/
    add_action('woocommerce_after_order_notes', 'wt_attendee_details');
    function wt_attendee_details( $checkout ) {

        $attendee_count = wt_count_attendees();

        if($attendee_count > 0) {
            echo "</div></div>"; //close the Billing Address section to create new group of fields
            echo "<div id='attendee_details'><div>"; //automatically be closed from 2 Billing Address's div - </div></div>
            echo '<h3>'.__('All Event Attendees and/or clients who are ordering DVDs, please add your name and email again.').'</h3>';
            for($n = 1; $n <= $attendee_count; $n++ ) {
                woocommerce_form_field( 'attendee_name_'.$n, array(
                    'type'          => 'text',
                    'class'         => array('form-row form-row-first'),
                    'label'         => __('Name'),
                    'placeholder'   => __('name'),
                    'required'      => true,
                ), $checkout->get_value( 'attendee_name_'.$n ));
                woocommerce_form_field( 'attendee_email_'.$n, array(
                    'type'          => 'text',
                    'class'         => array('form-row validate-email'),
                    'label'         => __('Email'),
                    'placeholder'       => __('email'),
                    'required'      => true,
                ), $checkout->get_value( 'attendee_email_'.$n ));
                woocommerce_form_field( 'attendee_phone_'.$n, array(
                    'type'          => 'text',
                    'class'         => array('form-row form-row-last'),
                    'label'         => __('Phone'),
                    'placeholder'   => __(''),
                ), $checkout->get_value( 'attendee_phone_'.$n ));
            }
            echo "<style type='text/css'>
                        #attendee_details .form-row {
                            float: left;
                            margin-right: 2%;
                            width: 31%;
                        }
                        #attendee_details .form-row-last {
                            margin-right: 0;
                        }
                    </style>";
        }
        //echo "Attendees: " . $attendee_count;
    }

    /**
     * Process the checkout
     **/
    add_action('woocommerce_checkout_process', 'wt_attendee_fields_process');

    function wt_attendee_fields_process() {
        global $woocommerce;
        $attendee_count = wt_count_attendees();

        for($n = 1; $n <= $attendee_count; $n++ ) {
            if (!$_POST['attendee_email_'.$n] || !$_POST['attendee_name_'.$n])
                $error = true;
                break;
        }
        if($error) {
            $woocommerce->add_error( __('Please complete the attendee details.') );
        }

    }

    /**
     * Update the order meta with field value
     **/
    add_action('woocommerce_checkout_update_order_meta', 'wt_attendee_update_order_meta');

    function wt_attendee_update_order_meta( $order_id ) {

        $attendee_count = wt_count_attendees();
        for($n = 1; $n <= $attendee_count; $n++ ) {
            if ($_POST['attendee_name_'.$n]) update_post_meta( $order_id, $n.' Attendee Name', esc_attr($_POST['attendee_name_'.$n]));
            if ($_POST['attendee_email_'.$n]) update_post_meta( $order_id, $n.' Attendee Email', esc_attr($_POST['attendee_email_'.$n]));
            if ($_POST['attendee_phone_'.$n]) update_post_meta( $order_id, $n.' Attendee Phone', esc_attr($_POST['attendee_phone_'.$n]));
        }

    }

    function wt_count_attendees() {

        global $woocommerce;
        $attendee_count = 0;

        if (sizeof($woocommerce->cart->get_cart())>0) :
            foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
                $_product = $values['data'];
                if ($_product->exists() && $values['quantity']>0) :
                    if (get_post_meta($_product->id, '_tribe_wooticket_for_event') > 0)
                        $attendee_count += $values['quantity'];
                endif;
            endforeach;
        endif;

        return $attendee_count;

    }
}

声明:本站带有” 原 “字文章,均为本站作者原创发布。原创文章任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站作者内容侵犯了原著者的合法权益,可联系我们进行处理。

文章来源 2kss https://www.22vd.com/63363.html

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务

  • 软件商用保障
    正版采购,正规版权
  • 终身SVIP
    一次购买,终身受益
  • 海量资源
    教程、主题、插件一应俱全
  • 高效学习
    多位资深大佬,坐台解答
  • 信用保证
    数百万站长和用户的选择