在WooCommerce中检查购物车中有多个产品ID [英] Check for multiple product ID's in cart in WooCommerce

查看:73
本文介绍了在WooCommerce中检查购物车中有多个产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码检查购物车中是否包含产品ID,如果有,请添加额外的结帐字段:

I am using the following code to check if a product ID is in the cart, and if so, add extra checkout fields:

add_action('woocommerce_after_order_notes', 'conditional_checkout_field');

function conditional_checkout_field( $checkout ) {
    echo '<div id="conditional_checkout_field">';

    $product_id = 326;
    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

    // Check if the product is in the cart and show the custom field if it is

    if ($in_cart ) {
            echo '<h3>'.__('Products in your cart require the following information').'</h3>';

            woocommerce_form_field( 'custom_field_license', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('License Number'),
            'placeholder'   => __('Placeholder to help describe what you are looking for'),
            ), $checkout->get_value( 'custom_field_license' ));

    }
}

这很好用。但是,如何检查购物车中是否有多个产品ID?例如,如果购物车中有产品ID 326或245,请显示条件结帐字段?我觉得这可能很简单,但是我不确定该怎么做。

This works just fine. However, how do I check for multiple product ID's in the cart? For instance, if product ID 326 or 245 are in the cart, show the conditional checkout fields? I feel like it is probably something simple, but I'm not sure how to go about doing it.

推荐答案

您需要对功能进行一些更改以使其适用于许多产品ID。另外,我还向该字段添加了必需的选项。因此,您的代码应类似于:

I have make some changes in your function to get it work for many product IDs. Also I have added the required option to the field. So your code sould be something like:

add_action('woocommerce_after_order_notes', 'conditional_checkout_field', 10, 1);
function conditional_checkout_field( $checkout ) {

    // Set here your product IDS (in the array)
    $product_ids = array( 37, 53, 70 );
    $is_in_cart = false;

    // Iterating through cart items and check
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
        if( in_array( $cart_item['data']->get_id(), $product_ids ) ){
            $is_in_cart = true; // We set it to "true"
            break; // At east one product, we stop the loop
        }

    // If condition match we display the field
    if( $is_in_cart ){
        echo '<div id="conditional_checkout_field">
        <h3 class="field-license-heading">'.__('Products in your cart require the following information').'</h3>';

        woocommerce_form_field( 'custom_field_license', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'required'      => true, // Added required
            'label'         => __('License Number'),
            'placeholder'   => __('Placeholder to help describe what you are looking for'),
        ), $checkout->get_value( 'custom_field_license' ));

        echo '</div>';
    }
}

代码会出现在您活跃孩子的function.php文件中主题(活动主题或任何插件文件中)。

Code goes in function.php file of your active child theme (active theme or in any plugin file).

此代码已经过测试并且可以正常工作。

This code is tested and works.

这篇关于在WooCommerce中检查购物车中有多个产品ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆