结账前,需要WooCommerce购物车中的简单产品以了解产品变化



将一些产品变体添加到购物车后,需要一些简单的产品来匹配购物车中的产品变体。

如果购物车中没有正确的简单产品,将显示一条消息,用户无法继续结账。

如果购物车中的产品变体和所需的简单产品不在购物车中,则应显示通知。

add_action('woocommerce_before_cart', 'product_cart_check');
function product_cart_check() {
foreach (WC()->cart->get_cart() as $cart_item) {
// Target product variation
$product_variation_ids = array(27741);
// Simple products should match the product variation
$simple_product_ids = array(26924, 26925);
if (in_array($cart_item['variation_id'], $product_variation_ids)) {
if (!in_array($cart_item['product_id'], $simple_product_ids)) {
// Display message
wc_print_notice('Please add required simple products to your cart', 'notice');
}
}
}
}

我有上面的代码,但它没有按预期工作。即使我将所需的简单产品添加到购物车中,通知也将始终显示。有什么建议吗?

要求购物车中的简单产品用于(a(产品变体:

  • 请改用woocommerce_check_cart_items挂钩
  • 使用array_diff((将数组与一个或多个其他数组进行比较,并返回数组中任何其他数组中都不存在的值
  • 要删除"继续结账"按钮,可以使用woocommerce_proceed_to_checkout挂钩

因此,对于特定的变体ID:

function get_cart_item_ids() {
// Initialize
$ids = array();
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Push to array
$ids[] = $cart_item['data']->get_id();
}
}

return $ids;
}
function action_woocommerce_check_cart_items() {
// Get cart item ids
$cart_item_ids = get_cart_item_ids();

// Target product variation
$product_variation_id = 27741;

// Simple products should match the product variation
$simple_product_ids = array( 26924, 26925 );

// Checks if a value exists in an array
if ( in_array( $product_variation_id, $cart_item_ids ) ) {
// Computes the difference of arrays
if ( array_diff( $simple_product_ids, $cart_item_ids ) ) {
// Notice
wc_print_notice( __( 'Please add required simple products to your cart', 'woocommerce' ), 'notice' ); 

// Remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

将相同的应用于多个产品变体ID:

function get_cart_item_ids() {
// Initialize
$ids = array();
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Push to array
$ids[] = $cart_item['data']->get_id();
}
}

return $ids;
}
function action_woocommerce_check_cart_items() {
// Get cart item ids
$cart_item_ids = get_cart_item_ids();

// Target product variations
$product_variation_ids = array( 27741, 56 );

// Simple products should match the product variation
$simple_product_ids = array( 26924, 26925 );

// Initialize
$flag = false;

// Loop through
foreach ( $product_variation_ids as $product_variation_id ) {   
// Checks if a value exists in an array
if ( in_array( $product_variation_id, $cart_item_ids ) ) {
// Computes the difference of arrays
if ( array_diff( $simple_product_ids, $cart_item_ids ) ) {
$flag = true;
break;
}
}
}

// True
if ( $flag ) {
// Notice
wc_print_notice( __( 'Please add required simple products to your cart', 'woocommerce' ), 'notice' ); 
// Remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );   
}   
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

相关内容

最新更新