Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
1.5 kB
10
Indexable
Never
public function validate_fields () {

			if ( is_checkout() && is_wc_endpoint_url( 'order-pay' ) ) {

				$PointsData = new RS_Points_Data( get_current_user_id() );
				if ( ! empty( $PointsData->total_available_points() ) ) {
					
					$user_points = $PointsData->total_available_points();
					$needed_points = 0;
					if (isset($_GET['pay_for_order'])){
						if ($_GET['pay_for_order'] === 'true'){
							$order_id = absint( get_query_var('order-pay') );
							$order = wc_get_order($order_id);
							
							if ($order){
								
								$items = $order->get_items();

								foreach ($items as $item_id => $item) {
									$product_id = $item->get_product_id();
									$quantity = $item->get_quantity();

									$enable = calculate_point_price_for_products( $product_id );
									if ( ! empty( $enable[ $product_id ] ) && null == $bundledproduct ) {
										$needed_points += $enable[ $product_id ] * $quantity;
									}
								}
							}
						}
					}

					if ($user_points < $needed_points) {
						if (function_exists('wc_add_notice')) {
							// Replace deprecated $woocommerce_add_error() function.
							wc_add_notice("You need $needed_points PSCs to complete this transaction. But you have only $user_points PSCs available.", 'error');
						} else {
							WC()->add_error("You need $needed_points PSCs to complete this transaction. But you have only $user_points PSCs available.");
						}
						return false;
					}

				}

			}
			

			return true;
		}
Leave a Comment