结帐前需要 WooCommerce 购物车中的简单产品以获得产品变体
Require simple products in WooCommerce cart for product variations before checkout
将一些产品变体添加到购物车后,需要一些简单的产品来匹配购物车中的产品变体。
如果购物车中没有正确的简单产品,将显示一条消息,用户无法继续结帐。
如果购物车中的产品变体,而所需的简单产品不在购物车中,则应显示通知。
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() 将数组与一个或多个其他数组进行比较,并 returns 数组中不存在于任何其他数组中的值。
- 要删除继续结帐按钮,您可以使用
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 );
将一些产品变体添加到购物车后,需要一些简单的产品来匹配购物车中的产品变体。
如果购物车中没有正确的简单产品,将显示一条消息,用户无法继续结帐。
如果购物车中的产品变体,而所需的简单产品不在购物车中,则应显示通知。
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() 将数组与一个或多个其他数组进行比较,并 returns 数组中不存在于任何其他数组中的值。
- 要删除继续结帐按钮,您可以使用
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 );