| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | <?php namespace TierPricingTable\Addons\Tools\API; |
| 2 | 2 | |
| 3 | 3 | use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 4 | +use TierPricingTable\Addons\Tools\PricingTest\PricingTester; | |
| 5 | +use WP_Error; | |
| 4 | 6 | use WP_REST_Request; |
| 5 | 7 | |
| 6 | 8 | class ToolsEndpoints { |
| 7 | 9 | |
| @@ -42,8 +44,23 @@ | ||
| 42 | 44 | }, |
| 43 | 45 | ), |
| 44 | 46 | ) ); |
| 45 | 47 | |
| 48 | + register_rest_route( self::NAMESPACE, '/pricing_test', array( | |
| 49 | + array( | |
| 50 | + 'methods' => 'POST', | |
| 51 | + 'callback' => array( $this, 'pricingTest' ), | |
| 52 | + 'permission_callback' => function () { | |
| 53 | + return current_user_can( 'manage_woocommerce' ); | |
| 54 | + }, | |
| 55 | + 'args' => array( | |
| 56 | + 'product_id' => array( 'type' => 'integer', 'required' => true ), | |
| 57 | + 'user_id' => array( 'type' => 'integer', 'default' => 0 ), | |
| 58 | + 'rule_id' => array( 'type' => 'integer', 'default' => 0 ), | |
| 59 | + ), | |
| 60 | + ), | |
| 61 | + ) ); | |
| 62 | + | |
| 46 | 63 | register_rest_route( self::NAMESPACE, '/roles', array( |
| 47 | 64 | array( |
| 48 | 65 | 'methods' => 'GET', |
| 49 | 66 | 'callback' => array( $this, 'getRoles' ), |
| @@ -82,8 +99,33 @@ | ||
| 82 | 99 | return current_user_can( 'manage_options' ); |
| 83 | 100 | }, |
| 84 | 101 | ), |
| 85 | 102 | ) ); |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * The Pricing test: what a customer (0 = visitor) gets for a product, and which global rules apply. | |
| 107 | + */ | |
| 108 | + public function pricingTest( WP_REST_Request $request ) { | |
| 109 | + $productId = absint( $request->get_param( 'product_id' ) ); | |
| 110 | + $userId = absint( $request->get_param( 'user_id' ) ); | |
| 111 | + $ruleId = absint( $request->get_param( 'rule_id' ) ); | |
| 112 | + | |
| 113 | + if ( ! $productId ) { | |
| 114 | + return new WP_Error( 'tpt_pricing_test_product', __( 'Pick a product first.', 'tier-pricing-table' ), array( 'status' => 400 ) ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + if ( $userId && ! get_user_by( 'id', $userId ) ) { | |
| 118 | + return new WP_Error( 'tpt_pricing_test_customer', __( 'The customer could not be loaded.', 'tier-pricing-table' ), array( 'status' => 400 ) ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $result = ( new PricingTester() )->run( $productId, $userId, $ruleId ); | |
| 122 | + | |
| 123 | + if ( isset( $result['error'] ) ) { | |
| 124 | + return new WP_Error( 'tpt_pricing_test', $result['error'], array( 'status' => 400 ) ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + return rest_ensure_response( $result ); | |
| 86 | 128 | } |
| 87 | 129 | |
| 88 | 130 | public function getRoles() { |
| 89 | 131 | global $wp_roles; |