PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.25.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.25.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Controllers / Rest / ProductsController.php
ProductsController.php
62 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Controllers\Rest;
4
5 use SureCart\Models\Product;
6
7 /**
8 * Handle Product requests through the REST API
9 */
10 class ProductsController extends RestController {
11 /**
12 * Class to make the requests.
13 *
14 * @var string
15 */
16 protected $class = Product::class;
17
18 /**
19 * Run some middleware to run before request.
20 *
21 * @param \SureCart\Models\Model $class Model class instance.
22 * @param \WP_REST_Request $request Request object.
23 *
24 * @return \SureCart\Models\Model
25 */
26 protected function middleware( $class, \WP_REST_Request $request ) {
27 // if we are in edit context, we want to fetch the variants, variant options and prices.
28 if ( 'edit' === $request->get_param( 'context' ) || in_array( $request->get_method(), [ 'POST', 'PUT', 'PATCH', 'DELETE' ] ) ) {
29 $class->with( [ 'variants', 'variant_options', 'variants.image', 'prices', 'product_collections' ] );
30 }
31 return parent::middleware( $class, $request );
32 }
33
34 /**
35 * Edit model.
36 *
37 * Filter out variations which statuses are draft.
38 *
39 * @param \WP_REST_Request $request Rest Request.
40 *
41 * @return \WP_REST_Response|\WP_Error
42 */
43 public function edit( \WP_REST_Request $request ) {
44 // Stop if we are not editing a variable product.
45 if ( empty( $request['variants'] ) ) {
46 return parent::edit( $request );
47 }
48
49 // Filter draft variations.
50 $request['variants'] = array_values(
51 array_filter(
52 $request['variants'],
53 function( $variation ) {
54 return ! in_array( $variation['status'] ?? 'publish', [ 'draft', 'deleted' ] );
55 }
56 )
57 );
58
59 return parent::edit( $request );
60 }
61 }
62