Skip to content

Commit 7b0fe1b

Browse files
authored
Merge pull request #82 from kidunot89/bugfix/pricing-fields-format
Resolve formatted price string for pricing fields
2 parents cbe3fb2 + 83d648a commit 7b0fe1b

17 files changed

+590
-206
lines changed

access-functions.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,81 @@ function wc_graphql_get_order_statuses() {
8989
}
9090
return $order_statuses;
9191
}
92+
93+
/**
94+
* Format the price with a currency symbol.
95+
*
96+
* @param float $price Raw price.
97+
* @param array $args Arguments to format a price {
98+
* Array of arguments.
99+
* Defaults to empty array.
100+
*
101+
* @type string $currency Currency code.
102+
* Defaults to empty string (Use the result from get_woocommerce_currency()).
103+
* @type string $decimal_separator Decimal separator.
104+
* Defaults the result of wc_get_price_decimal_separator().
105+
* @type string $thousand_separator Thousand separator.
106+
* Defaults the result of wc_get_price_thousand_separator().
107+
* @type string $decimals Number of decimals.
108+
* Defaults the result of wc_get_price_decimals().
109+
* @type string $price_format Price format depending on the currency position.
110+
* Defaults the result of get_woocommerce_price_format().
111+
* }
112+
* @return string
113+
*/
114+
function wc_graphql_price( $price, $args = array() ) {
115+
$args = apply_filters(
116+
'wc_price_args',
117+
wp_parse_args(
118+
$args,
119+
array(
120+
'currency' => '',
121+
'decimal_separator' => wc_get_price_decimal_separator(),
122+
'thousand_separator' => wc_get_price_thousand_separator(),
123+
'decimals' => wc_get_price_decimals(),
124+
'price_format' => get_woocommerce_price_format(),
125+
)
126+
)
127+
);
128+
129+
$unformatted_price = $price;
130+
$negative = $price < 0;
131+
$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
132+
$price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] ), $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] );
133+
134+
if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $args['decimals'] > 0 ) {
135+
$price = wc_trim_zeros( $price );
136+
}
137+
138+
$symbol = html_entity_decode( get_woocommerce_currency_symbol( $args['currency'] ) );
139+
$return = ( $negative ? '-' : '' ) . sprintf( $args['price_format'], $symbol, $price );
140+
141+
/**
142+
* Filters the string of price markup.
143+
*
144+
* @param string $return Price HTML markup.
145+
* @param string $price Formatted price.
146+
* @param array $args Pass on the args.
147+
* @param float $unformatted_price Price as float to allow plugins custom formatting. Since 3.2.0.
148+
*/
149+
return apply_filters( 'wc_graphql_price', $return, $price, $args, $unformatted_price, $symbol );
150+
}
151+
152+
/**
153+
* Format a price range for display.
154+
*
155+
* @param string $from Price from.
156+
* @param string $to Price to.
157+
* @return string
158+
*/
159+
function wc_graphql_price_range( $from, $to ) {
160+
$price = sprintf(
161+
/* translators: 1: price from 2: price to */
162+
_x( '%1$s %2$s %3$s', 'Price range: from-to', 'wp-graphql-woocommerce' ),
163+
is_numeric( $from ) ? wc_graphql_price( $from ) : $from,
164+
apply_filters( 'graphql_format_price_range_separator', '-', $from, $to ),
165+
is_numeric( $to ) ? wc_graphql_price( $to ) : $to
166+
);
167+
168+
return apply_filters( 'graphql_format_price_range', $price, $from, $to );
169+
}

includes/class-actions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Tax_Status;
2424
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\WC_Connection_Orderby_Enum;
2525
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Tax_Rate_Connection_Orderby_Enum;
26+
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Pricing_Field_Format;
2627
use WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Customer_Address_Input;
2728
use WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Product_Attribute_Input;
2829
use WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\WC_Connection_Orderby_Input;
@@ -103,6 +104,7 @@ public static function graphql_register_types() {
103104
Tax_Status::register();
104105
WC_Connection_Orderby_Enum::register();
105106
Tax_Rate_Connection_Orderby_Enum::register();
107+
Pricing_Field_Format::register();
106108

107109
// InputObjects.
108110
Customer_Address_Input::register();

includes/model/class-order.php

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,85 @@ protected function init() {
9696
return ! empty( $this->data->get_date_paid() ) ? $this->data->get_date_paid() : null;
9797
},
9898
'discountTotal' => function() {
99-
return ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
100-
},
99+
$price = ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
100+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
101+
},
102+
'discountTotalRaw' => array(
103+
'callback' => function() {
104+
return ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
105+
},
106+
'capability' => $this->post_type_object->cap->edit_posts,
107+
),
101108
'discountTax' => function() {
102-
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
103-
},
109+
$price = ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
110+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
111+
},
112+
'discountTaxRaw' => array(
113+
'callback' => function() {
114+
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
115+
},
116+
'capability' => $this->post_type_object->cap->edit_posts,
117+
),
104118
'shippingTotal' => function() {
105-
return ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
106-
},
119+
$price = ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
120+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
121+
},
122+
'shippingTotalRaw' => array(
123+
'callback' => function() {
124+
return ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
125+
},
126+
'capability' => $this->post_type_object->cap->edit_posts,
127+
),
107128
'shippingTax' => function() {
108-
return ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
109-
},
129+
$price = ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
130+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
131+
},
132+
'shippingTaxRaw' => array(
133+
'callback' => function() {
134+
return ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
135+
},
136+
'capability' => $this->post_type_object->cap->edit_posts,
137+
),
110138
'cartTax' => function() {
111-
return ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
112-
},
139+
$price = ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
140+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
141+
},
142+
'cartTaxRaw' => array(
143+
'callback' => function() {
144+
return ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
145+
},
146+
'capability' => $this->post_type_object->cap->edit_posts,
147+
),
113148
'total' => function() {
114-
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
115-
},
149+
$price = ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
150+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
151+
},
152+
'totalRaw' => array(
153+
'callback' => function() {
154+
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
155+
},
156+
'capability' => $this->post_type_object->cap->edit_posts,
157+
),
116158
'totalTax' => function() {
117-
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
118-
},
159+
$price = ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
160+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
161+
},
162+
'totalTaxRaw' => array(
163+
'callback' => function() {
164+
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
165+
},
166+
'capability' => $this->post_type_object->cap->edit_posts,
167+
),
119168
'subtotal' => function() {
120-
return ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : 0;
121-
},
169+
$price = ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : null;
170+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
171+
},
172+
'subtotalRaw' => array(
173+
'callback' => function() {
174+
return ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : 0;
175+
},
176+
'capability' => $this->post_type_object->cap->edit_posts,
177+
),
122178
'orderNumber' => function() {
123179
return ! empty( $this->data->get_order_number() ) ? $this->data->get_order_number() : null;
124180
},

includes/model/class-product-variation.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,38 @@ protected function init() {
9595
return ! empty( $this->data->get_sku() ) ? $this->data->get_sku() : null;
9696
},
9797
'price' => function() {
98-
return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null;
99-
},
98+
return ! empty( $this->data->get_price() )
99+
? \wc_graphql_price( $this->data->get_price() )
100+
: null;
101+
},
102+
'priceRaw' => array(
103+
'callback' => function() {
104+
return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null;
105+
},
106+
'capability' => $this->post_type_object->cap->edit_posts,
107+
),
100108
'salePrice' => function() {
101-
return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null;
102-
},
109+
return ! empty( $this->data->get_sale_price() )
110+
? \wc_graphql_price( $this->data->get_sale_price() )
111+
: null;
112+
},
113+
'salePriceRaw' => array(
114+
'callback' => function() {
115+
return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null;
116+
},
117+
'capability' => $this->post_type_object->cap->edit_posts,
118+
),
103119
'regularPrice' => function() {
104-
return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null;
105-
},
120+
return ! empty( $this->data->get_regular_price() ) ?
121+
\wc_graphql_price( $this->data->get_regular_price() )
122+
: null;
123+
},
124+
'regularPriceRaw' => array(
125+
'callback' => function() {
126+
return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null;
127+
},
128+
'capability' => $this->post_type_object->cap->edit_posts,
129+
),
106130
'dateOnSaleFrom' => function() {
107131
return ! empty( $this->data->get_date_on_sale_from() ) ? $this->data->get_date_on_sale_from() : null;
108132
},

0 commit comments

Comments
 (0)