AI
1 year ago
Agentic
4 months ago
AbstractCartRoute.php
4 months ago
AbstractRoute.php
3 months ago
AbstractTermsRoute.php
4 months ago
Batch.php
4 months ago
Cart.php
1 year ago
CartAddItem.php
4 months ago
CartApplyCoupon.php
1 year ago
CartCoupons.php
3 months ago
CartCouponsByCode.php
1 year ago
CartExtensions.php
2 years ago
CartItems.php
2 years ago
CartItemsByKey.php
2 years ago
CartRemoveCoupon.php
1 year ago
CartRemoveItem.php
4 months ago
CartSelectShippingRate.php
5 months ago
CartUpdateCustomer.php
4 months ago
CartUpdateItem.php
4 months ago
Checkout.php
1 month ago
CheckoutOrder.php
1 year ago
Order.php
7 months ago
Patterns.php
1 year ago
ProductAttributeTerms.php
1 month ago
ProductAttributes.php
1 year ago
ProductAttributesById.php
1 year ago
ProductBrands.php
1 year ago
ProductBrandsById.php
1 year ago
ProductCategories.php
1 year ago
ProductCategoriesById.php
1 year ago
ProductCollectionData.php
11 months ago
ProductReviews.php
4 months ago
ProductTags.php
1 year ago
Products.php
3 months ago
ProductsById.php
3 months ago
ProductsBySlug.php
3 months ago
ShopperListItems.php
1 month ago
ShopperListItemsByKey.php
1 month ago
ShopperLists.php
1 month ago
ShopperListsBySlug.php
1 month ago
ShopperListsNonceCheck.php
1 month ago
ProductsBySlug.php
132 lines
| 1 | <?php // phpcs:ignore Generic.PHP.RequireStrictTypes.MissingDeclaration |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\Enums\ProductStatus; |
| 5 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 6 | use Automattic\WooCommerce\StoreApi\Utilities\ProductLinksTrait; |
| 7 | |
| 8 | /** |
| 9 | * ProductsBySlug class. |
| 10 | */ |
| 11 | class ProductsBySlug extends AbstractRoute { |
| 12 | use ProductLinksTrait; |
| 13 | |
| 14 | /** |
| 15 | * The route identifier. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | const IDENTIFIER = 'products-by-slug'; |
| 20 | |
| 21 | /** |
| 22 | * The routes schema. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | const SCHEMA_TYPE = 'product'; |
| 27 | |
| 28 | /** |
| 29 | * Get the path of this REST route. |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | public function get_path() { |
| 34 | return self::get_path_regex(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get the path of this rest route. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public static function get_path_regex() { |
| 43 | return '/products/(?P<slug>[\S]+)'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get method arguments for this REST route. |
| 48 | * |
| 49 | * @return array An array of endpoints. |
| 50 | */ |
| 51 | public function get_args() { |
| 52 | return [ |
| 53 | 'args' => array( |
| 54 | 'slug' => array( |
| 55 | 'description' => __( 'Slug of the resource.', 'woocommerce' ), |
| 56 | 'type' => 'string', |
| 57 | ), |
| 58 | ), |
| 59 | [ |
| 60 | 'methods' => \WP_REST_Server::READABLE, |
| 61 | 'callback' => [ $this, 'get_response' ], |
| 62 | 'permission_callback' => '__return_true', |
| 63 | 'args' => array( |
| 64 | 'context' => $this->get_context_param( |
| 65 | array( |
| 66 | 'default' => 'view', |
| 67 | ) |
| 68 | ), |
| 69 | ), |
| 70 | 'allow_batch' => [ 'v1' => true ], |
| 71 | ], |
| 72 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get a single item. |
| 78 | * |
| 79 | * @throws RouteException On error. |
| 80 | * @param \WP_REST_Request $request Request object. |
| 81 | * @return \WP_REST_Response |
| 82 | */ |
| 83 | protected function get_route_response( \WP_REST_Request $request ) { |
| 84 | $slug = sanitize_title( $request['slug'] ); |
| 85 | |
| 86 | $object = $this->get_product_by_slug( $slug ); |
| 87 | if ( ! $object ) { |
| 88 | $object = $this->get_product_variation_by_slug( $slug ); |
| 89 | } |
| 90 | |
| 91 | if ( ! $object || 0 === $object->get_id() || ProductStatus::PUBLISH !== $object->get_status() ) { |
| 92 | throw new RouteException( 'woocommerce_rest_product_invalid_slug', __( 'Invalid product slug.', 'woocommerce' ), 404 ); |
| 93 | } |
| 94 | |
| 95 | return $this->prepare_item_for_response( $object, $request ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get a product by slug. |
| 100 | * |
| 101 | * @param string $slug The slug of the product. |
| 102 | */ |
| 103 | public function get_product_by_slug( $slug ) { |
| 104 | return wc_get_product( get_page_by_path( $slug, OBJECT, 'product' ) ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get a product variation by slug. |
| 109 | * |
| 110 | * @param string $slug The slug of the product variation. |
| 111 | */ |
| 112 | private function get_product_variation_by_slug( $slug ) { |
| 113 | global $wpdb; |
| 114 | |
| 115 | $result = $wpdb->get_results( |
| 116 | $wpdb->prepare( |
| 117 | "SELECT ID, post_name, post_parent, post_type |
| 118 | FROM $wpdb->posts |
| 119 | WHERE post_name = %s |
| 120 | AND post_type = 'product_variation'", |
| 121 | $slug |
| 122 | ) |
| 123 | ); |
| 124 | |
| 125 | if ( ! $result ) { |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | return wc_get_product( $result[0]->ID ); |
| 130 | } |
| 131 | } |
| 132 |