因此,我正在制作一些自定义代码,以允许根据 WooCommerce 积分和奖励插件累积的积分购买特定产品,在检查积分余额时,如果余额不足,我使用 wc_add_notice 显示错误。 第一次它有效,然后我再也无法收到这些通知,我尝试以多种不同的方式执行它,但似乎没有任何效果!
这是我的代码:
function enforce_points_based_registration( $valid, $product_id ) {
if ( $product_id == 4465 ) {
$points_required = get_post_meta( $product_id, 'points_required', true);
$user_id = get_current_user_id();
$user_points = WC_Points_Rewards_Manager::get_users_points( $user_id );
$current_user= wp_get_current_user();
$user_email = $current_user->email;
if ( wc_customer_bought_product( $user_email, $user_id, $product_id )) {
$message = "You have already bought the Vendor package";
wc_add_notice( apply_filters('wc_add_to_cart_message', $message, $product_id), "error");
return false;
}
if ( dokan_is_user_vendor( $user_id) ) {
$message = "You are already a vendor.";
wc_add_notice( apply_filters('wc_add_to_cart_message', $message, $product_id), "error");
return false;
}
if ( $user_points < $points_required ) {
// User doesn't have enough points, prevent purchase
$message = "You don't have enough points to purchase the subscription";
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ), "error" );
return false;
} else {
// User has enough points, place the order
$product = wc_get_product( $product_id );
$order = wc_create_order();
$order->add_product( $product, 1 );
$order->set_customer_id( $user_id );
$order->calculate_totals();
$order->update_status( 'completed' );
// Deduct points
WC_Points_Rewards_Manager::decrease_points( $user_id, $points_required, 'vendor_registration', $order->get_id() );
// Add custom message to the order notes
$order->add_order_note( apply_filters( 'wc_add_to_cart_message', "The order was automatically placed and paid for using points.", $product_id ) );
}
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'enforce_points_based_registration', 10, 2 );
我尝试用 apply_filters 那样调用它,这是第一次工作,我也尝试了 wc_add_notice($message,$type) 仍然没有任何效果,尽管大多数代码都按预期工作。
代码放置在functions.php(子主题)中
过滤器挂钩
wc_add_to_cart_message
在 WooCommerce 中不存在,因此您应该从代码中的所有 wc_add_notice() 函数中将其删除。
我修改了你的代码,请尝试以下操作:
function enforce_points_based_registration( $passed, $product_id ) {
global $current_user;
if ( $product_id != 4465 )
return $passed;
$points_required = get_post_meta( $product_id, 'points_required', true);
$user_points = WC_Points_Rewards_Manager::get_users_points( $current_user->ID );
if ( wc_customer_bought_product( $current_user->email, $current_user->ID, $product_id )) {
wc_add_notice( __("You have already bought the Vendor package", "woocommerce"), "error");
$passed = false;
}
elseif ( dokan_is_user_vendor( $current_user->ID) ) {
wc_add_notice( __("You are already a vendor", "woocommerce"), "error");
$passed = false;
}
elseif ( $user_points < $points_required ) {
// User doesn't have enough points, prevent purchase
wc_add_notice( __("You don't have enough points to purchase the subscription", "woocommerce"), "error" );
$passed = false;
}
else {
// User has enough points, place the order
$order = wc_create_order();
$order->add_product( wc_get_product( $product_id ), 1 );
$order->set_customer_id($current_user->ID);
$order->calculate_totals();
$order->update_status( 'completed' );
// Deduct points
WC_Points_Rewards_Manager::decrease_points( $current_user->ID, $points_required, 'vendor_registration', $order->get_id() );
// Add custom message to the order notes
$order->add_order_note( __("The order was automatically placed and paid with user's points.", "woocommerce") );
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'enforce_points_based_registration', 10, 2 );
现在可能可以工作了。