PluginProbe
WooCommerce / 11.1.0-beta.2
WooCommerce v11.1.0-beta.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / StoreApi / Utilities / ProductItemTrait.php
ProductItemTrait.php
102 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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