AgenticCheckoutUtils.php
4 months ago
ArrayUtils.php
2 years ago
CartController.php
1 month ago
CartTokenUtils.php
1 year ago
CheckoutTrait.php
1 month ago
DraftOrderTrait.php
1 year ago
JsonWebToken.php
11 months ago
LocalPickupUtils.php
5 months ago
NoticeHandler.php
1 year ago
OrderAuthorizationTrait.php
6 months ago
OrderController.php
1 month ago
Pagination.php
2 years ago
PaymentUtils.php
1 year ago
ProductItemTrait.php
1 month ago
ProductLinksTrait.php
3 months ago
ProductQuery.php
2 months ago
ProductQueryFilters.php
11 months ago
QuantityLimits.php
11 months ago
RateLimits.php
1 year ago
SanitizationUtils.php
2 years ago
ValidationUtils.php
2 years ago
ProductLinksTrait.php
73 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 5 | |
| 6 | /** |
| 7 | * ProductLinksTrait |
| 8 | * |
| 9 | * Shared functionality for preparing product links including embeddable links for upsells, cross-sells, and related products. |
| 10 | */ |
| 11 | trait ProductLinksTrait { |
| 12 | /** |
| 13 | * Prepare links for the request. |
| 14 | * |
| 15 | * @param \WC_Product $item Product object. |
| 16 | * @param \WP_REST_Request $request Request object. |
| 17 | * @return array |
| 18 | * |
| 19 | * @since 10.6.0 |
| 20 | */ |
| 21 | protected function prepare_links( $item, $request ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 22 | $links = array( |
| 23 | 'self' => array( |
| 24 | 'href' => rest_url( $this->get_namespace() . '/products/' . $item->get_id() ), |
| 25 | ), |
| 26 | 'collection' => array( |
| 27 | 'href' => rest_url( $this->get_namespace() . '/products' ), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | if ( $item->get_parent_id() ) { |
| 32 | $links['up'] = array( |
| 33 | 'href' => rest_url( $this->get_namespace() . '/products/' . $item->get_parent_id() ), |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | $upsell_ids = $item->get_upsell_ids(); |
| 38 | if ( ! empty( $upsell_ids ) ) { |
| 39 | $links['upsells'] = array( |
| 40 | 'href' => add_query_arg( |
| 41 | array( 'include' => implode( ',', $upsell_ids ) ), |
| 42 | rest_url( $this->get_namespace() . '/products' ) |
| 43 | ), |
| 44 | 'embeddable' => true, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | $cross_sell_ids = $item->get_cross_sell_ids(); |
| 49 | if ( ! empty( $cross_sell_ids ) ) { |
| 50 | $links['cross_sells'] = array( |
| 51 | 'href' => add_query_arg( |
| 52 | array( 'include' => implode( ',', $cross_sell_ids ) ), |
| 53 | rest_url( $this->get_namespace() . '/products' ) |
| 54 | ), |
| 55 | 'embeddable' => true, |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | $links['related'] = array( |
| 60 | 'href' => add_query_arg( |
| 61 | array( |
| 62 | 'related' => $item->get_id(), |
| 63 | 'per_page' => 10, |
| 64 | ), |
| 65 | rest_url( $this->get_namespace() . '/products' ) |
| 66 | ), |
| 67 | 'embeddable' => true, |
| 68 | ); |
| 69 | |
| 70 | return $links; |
| 71 | } |
| 72 | } |
| 73 |