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



我在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_quantity()和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()函数,但缺点是该函数在用于其他目的时也会显示修改后的结果

最新更新