【发布时间】:2021-01-17 17:56:08
【问题描述】:
我在我的 WoocCmmerce 商店的子主题的 functions.php 文件中编写了一个函数。
add to cart 按钮上的验证检查购物车中的所有产品是否仅来自 1 个供应商。
- 如果产品来自一个供应商,它将添加该产品。
- 如果产品来自不同的供应商,则通知应显示警告。
此功能无法正常工作,因为该通知仅在我手动刷新页面时显示。它应该立即显示通知。谁能帮帮我?
下面是我的代码:
add_action( 'wp_enqueue_scripts', 'martfury_child_enqueue_scripts', 20 );
function martfury_child_enqueue_scripts() {
wp_enqueue_style( 'martfury-child-style', get_stylesheet_uri() );
if ( is_rtl() ) {
wp_enqueue_style( 'martfury-rtl', get_template_directory_uri() . '/rtl.css', array(), '20180105' );
}
}
add_action( 'woocommerce_add_to_cart_validation', function( $is_allow, $product_id, $quantity ) {
$product = get_post( $product_id );
$product_author = $product->post_author;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_product_id = $cart_item['product_id'];
$cart_product = get_post( $cart_product_id );
$cart_product_author = $cart_product->post_author;
if( $cart_product_author != $product_author ) {
$is_allow = false;
break;
}
}
if( !$is_allow ){
// We display an error message
wc_add_notice( __( "Well, you already have some item in your cart. First checkout with those and then purchase other items!", "wcfm-ecogear" ), 'error' );
return wp_redirect($woocommerce->cart->get_cart_url());
}
return $is_allow;
}, 50, 3 );
【问题讨论】:
标签: wordpress validation woocommerce product vendor