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
ProductItemTrait.php
102 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 3 | |
| 4 | /** |
| 5 | * ProductItemTrait |
| 6 | * |
| 7 | * Shared functionality for formatting product item data. |
| 8 | */ |
| 9 | trait ProductItemTrait { |
| 10 | /** |
| 11 | * Get an array of pricing data. |
| 12 | * |
| 13 | * @param \WC_Product $product Product instance. |
| 14 | * @param string $tax_display_mode If returned prices are incl or excl of tax. |
| 15 | * @return array |
| 16 | */ |
| 17 | protected function prepare_product_price_response( \WC_Product $product, $tax_display_mode = '' ) { |
| 18 | $tax_display_mode = $this->get_tax_display_mode( $tax_display_mode ); |
| 19 | $price_function = $this->get_price_function_from_tax_display_mode( $tax_display_mode ); |
| 20 | $prices = parent::prepare_product_price_response( $product, $tax_display_mode ); |
| 21 | $rounding_precision = wc_get_rounding_precision(); |
| 22 | |
| 23 | // Add raw prices (prices with greater precision). |
| 24 | $prices['raw_prices'] = array( |
| 25 | 'precision' => $rounding_precision, |
| 26 | 'price' => $this->prepare_money_response( $price_function( $product ), $rounding_precision ), |
| 27 | 'regular_price' => $this->prepare_money_response( $price_function( $product, array( 'price' => $product->get_regular_price() ) ), $rounding_precision ), |
| 28 | 'sale_price' => $this->prepare_money_response( $price_function( $product, array( 'price' => $product->get_sale_price() ) ), $rounding_precision ), |
| 29 | ); |
| 30 | |
| 31 | return $prices; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Format variation data, for example convert slugs such as attribute_pa_size to Size. |
| 36 | * |
| 37 | * @param array $variation_data Array of data from the cart. |
| 38 | * @param \WC_Product $product Product data. |
| 39 | * @return array |
| 40 | */ |
| 41 | protected function format_variation_data( $variation_data, $product ) { |
| 42 | $return = array(); |
| 43 | |
| 44 | if ( ! is_iterable( $variation_data ) ) { |
| 45 | return $return; |
| 46 | } |
| 47 | |
| 48 | foreach ( $variation_data as $key => $value ) { |
| 49 | $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $key ) ) ); |
| 50 | if ( taxonomy_exists( $taxonomy ) ) { |
| 51 | // If this is a term slug, get the term's nice name. |
| 52 | $term = get_term_by( 'slug', $value, $taxonomy ); |
| 53 | if ( ! is_wp_error( $term ) && $term && $term->name ) { |
| 54 | |
| 55 | /** |
| 56 | * Filters the variation option name for taxonomy attributes. |
| 57 | * |
| 58 | * @since 10.7.0 |
| 59 | * |
| 60 | * @internal Matches filter name in WooCommerce core. |
| 61 | * |
| 62 | * @param string $name The term name to display. |
| 63 | * @param \WP_Term $term Term object. |
| 64 | * @param string $taxonomy Taxonomy name. |
| 65 | * @param \WC_Product $product Product data. |
| 66 | * @return string |
| 67 | */ |
| 68 | $value = apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $taxonomy, $product ); |
| 69 | } |
| 70 | $label = wc_attribute_label( $taxonomy ); |
| 71 | } else { |
| 72 | |
| 73 | /** |
| 74 | * Filters the variation option name. |
| 75 | * |
| 76 | * Filters the variation option name for custom option slugs. |
| 77 | * |
| 78 | * @since 2.5.0 |
| 79 | * |
| 80 | * @internal Matches filter name in WooCommerce core. |
| 81 | * |
| 82 | * @param string $value The name to display. |
| 83 | * @param null $unused Unused because this is not a variation taxonomy. |
| 84 | * @param string $taxonomy Taxonomy or product attribute name. |
| 85 | * @param \WC_Product $product Product data. |
| 86 | * @return string |
| 87 | */ |
| 88 | $value = apply_filters( 'woocommerce_variation_option_name', $value, null, $taxonomy, $product ); |
| 89 | $label = wc_attribute_label( str_replace( 'attribute_', '', $key ), $product ); |
| 90 | } |
| 91 | |
| 92 | $return[] = array( |
| 93 | 'raw_attribute' => $this->prepare_html_response( $key ), |
| 94 | 'attribute' => $this->prepare_html_response( $label ), |
| 95 | 'value' => $this->prepare_html_response( $value ), |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | return $return; |
| 100 | } |
| 101 | } |
| 102 |