问题描述
我已经在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 );
解决方法
您似乎不需要执行某些步骤,这就足够了
- get_post()-检索具有帖子ID或帖子对象的帖子数据
- 通过代码中添加的注释标签进行解释
function filter_woocommerce_add_to_cart_validation( $passed,$product_id,$quantity,$variation_id = null,$variations = null ) {
// Cart NOT empty
if ( ! WC()->cart->is_empty() ) {
// Retrieves post data given a post ID or post object.
$product = get_post( $product_id );
// Post author
$product_author = $product->post_author;
// Flag,by default false
$flag = false;
// Loop trough cart
foreach( WC()->cart->get_cart() as $cart_item ) {
// Get product ID
$cart_product_id = $cart_item['data']->get_id();
// Get post
$cart_product = get_post( $cart_product_id );
// Post author
$cart_product_author = $cart_product->post_author;
// Condition NOT equal
if( $cart_product_author != $product_author ) {
$flag = true;
// Break loop
break;
}
}
// True
if ( $flag ) {
// Add notice
wc_add_notice( __( 'Products in the cart have to come from only 1 vendor','woocommerce' ),'error' );
// NOT passed
$passed = false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation','filter_woocommerce_add_to_cart_validation',10,5 );