修改 "get_cart_contents_count()" 以仅计算 WooCommerce 购物车中的特定产品 ID

Modify "get_cart_contents_count()" to count only specific product IDs in WooCommerce cart

我在 WooCommerce 中有 3 个门票产品,我想为每个购买的门票产品添加一个名称字段 - 因此,如果客户购买了三张门票,则会在结账时显示 3 个名称字段以供填写。

下面的代码有效:

// Custom WooCommerce Checkout Fields based on Quantity
add_action( 'woocommerce_before_order_notes', 'person_details' );

function person_details($checkout) {

    global $woocommerce;

    $count = WC()->cart->get_cart_contents_count();

    $i = 1;

    for($k=2; $k<= $count; $k++) {
        $i++;

        print ('<h3>Please enter details of attendee '.$i.'</h3>');

        woocommerce_form_field( 'cstm_full_name'.$i, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Full name'),
            'placeholder'   => __('Enter full name'),
            ),
        $checkout->get_value( 'cstm_full_name'.$i ));

        echo '<div class="clear"></div>';
    }
}

但这包括添加到购物车的所有产品 - 我如何定位特定产品 ID 使其仅成为购物车数量的一部分?

$count = WC()->cart->get_cart_contents_count();

作为替代方法和最推荐的方法,您可以在回调函数中使用 WC_Cart::get_cart_item_quantities() and in_array() 从购物车中的特定产品 ID 获取购物车商品数量

所以你得到:

function action_woocommerce_before_order_notes( $checkout ) {
    // True
    if ( WC()->cart ) {
        // Specific product IDs
        $product_ids = array( 30, 823, 53, 57 );

        // Initialize
        $count = 0;

        // Loop trough cart items quantities
        foreach ( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
            // Checks if a value exists in an array
            if ( in_array( $product_id, $product_ids ) ) {
                $count += $cart_item_quantity;
            }
        }

        // Result
        echo '<p style="color: red; font-size: 25px;">Count = ' . $count . '</p>';
    }
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );

您可以使用 woocommerce_cart_contents_count 过滤器挂钩, 这允许您根据需要自定义 get_cart_contents_count() 函数:

function filter_woocommerce_cart_contents_count( $array_sum ) {
    // True
    if ( WC()->cart ) {
        // Specific product IDs
        $product_ids = array( 30, 823, 53, 57 );

        // Loop trough cart items quantities
        foreach ( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
            // Checks if a value NOT exists in an array
            if ( ! in_array( $product_id, $product_ids ) ) {
                $array_sum -= $cart_item_quantity;
            }
        }
    }

    return $array_sum;
}
add_filter( 'woocommerce_cart_contents_count', 'filter_woocommerce_cart_contents_count', 10, 1 );

function action_woocommerce_before_order_notes( $checkout ) {
    // True
    if ( WC()->cart ) {
        // Get number of items in the cart.
        $count = WC()->cart->get_cart_contents_count();

        // Result
        echo '<p style="color: red; font-size: 25px;">Count = ' . $count . '</p>';
    }
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );

注:虽然这里的好处是这样可以继续使用get_cart_contents_count()功能,缺点是这个功能也会显示修改后的内容用于其他目的时的结果