[wp-trac] [WordPress Trac] #60568: wc_add_notice not working properly

WordPress Trac noreply at wordpress.org
Mon Feb 19 19:24:29 UTC 2024


#60568: wc_add_notice not working properly
--------------------------+----------------------
 Reporter:  askintopsuz   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:
Component:  General       |     Version:
 Severity:  normal        |  Resolution:  invalid
 Keywords:                |     Focuses:
--------------------------+----------------------

Comment (by askintopsuz):

 Replying to [comment:1 swissspidy]:
 > Hi there an welcome to WordPress Trac!
 >
 > Here we discuss enhancements and bugs in WordPress core itself, not
 plugins like WooCommerce.
 >
 > For help with [https://woo.com/products/woocommerce-points-and-rewards/
 WooCommerce Points and Rewards], please use [https://woo.com/contact-us/
 their support channels].

 Thank you for your reply and sorry for the wrong message then but if
 anyone else needs the solution then I write the correct coding below.
 {{{#!php
 <?php
 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 );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60568#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list