php - 在 WooCommerce 中检查购物车中的多个产品 ID

标签 php wordpress woocommerce checkout cart

我使用以下代码来检查产品 ID 是否在购物车中,如果是,则添加额外的结账字段:

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 是否显示条件结账字段?我觉得这可能很简单,但我不知道如何去做。

最佳答案

我对您的函数进行了一些更改,以使其适用于许多产品 ID。我还向该字段添加了所需的选项。所以你的代码应该是这样的:

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 文件中(事件主题或任何插件文件)。

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

关于php - 在 WooCommerce 中检查购物车中的多个产品 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46674347/

相关文章:

css - 如何控制正在编辑的 Wordpress 页面内的间距?

php - 仅清除 Woocommerce 中的一些结帐字段值

woocommerce - 在 Woocommerce 电子邮件通知中显示产品图片

javascript - jQuery DataTable 显示条目、搜索框和排序不起作用?

php - 如何获取要在 php curl 请求的授权 header 中解析的 FCM 服务器 key ?

php - 如何为 WordPress 插件的选项设置默认值?

javascript - 无法读取我的 WordPress 插件上 null 的属性 '1'

php - 没有 root 访问权限 - 如何启用 PHP SQlite,其中 PHP 已符合 "without-sqlite"?

javascript - 使用没有表单的Jquery ajax上传文件

php - 无限滚动启用(wordpress)