Skip to content

Commit

Permalink
Merge pull request #82 from kidunot89/bugfix/pricing-fields-format
Browse files Browse the repository at this point in the history
Resolve formatted price string for pricing fields
  • Loading branch information
kidunot89 authored Jun 3, 2019
2 parents cbe3fb2 + 83d648a commit 7b0fe1b
Show file tree
Hide file tree
Showing 17 changed files with 590 additions and 206 deletions.
78 changes: 78 additions & 0 deletions access-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,81 @@ function wc_graphql_get_order_statuses() {
}
return $order_statuses;
}

/**
* Format the price with a currency symbol.
*
* @param float $price Raw price.
* @param array $args Arguments to format a price {
* Array of arguments.
* Defaults to empty array.
*
* @type string $currency Currency code.
* Defaults to empty string (Use the result from get_woocommerce_currency()).
* @type string $decimal_separator Decimal separator.
* Defaults the result of wc_get_price_decimal_separator().
* @type string $thousand_separator Thousand separator.
* Defaults the result of wc_get_price_thousand_separator().
* @type string $decimals Number of decimals.
* Defaults the result of wc_get_price_decimals().
* @type string $price_format Price format depending on the currency position.
* Defaults the result of get_woocommerce_price_format().
* }
* @return string
*/
function wc_graphql_price( $price, $args = array() ) {
$args = apply_filters(
'wc_price_args',
wp_parse_args(
$args,
array(
'currency' => '',
'decimal_separator' => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'decimals' => wc_get_price_decimals(),
'price_format' => get_woocommerce_price_format(),
)
)
);

$unformatted_price = $price;
$negative = $price < 0;
$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
$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'] );

if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $args['decimals'] > 0 ) {
$price = wc_trim_zeros( $price );
}

$symbol = html_entity_decode( get_woocommerce_currency_symbol( $args['currency'] ) );
$return = ( $negative ? '-' : '' ) . sprintf( $args['price_format'], $symbol, $price );

/**
* Filters the string of price markup.
*
* @param string $return Price HTML markup.
* @param string $price Formatted price.
* @param array $args Pass on the args.
* @param float $unformatted_price Price as float to allow plugins custom formatting. Since 3.2.0.
*/
return apply_filters( 'wc_graphql_price', $return, $price, $args, $unformatted_price, $symbol );
}

/**
* Format a price range for display.
*
* @param string $from Price from.
* @param string $to Price to.
* @return string
*/
function wc_graphql_price_range( $from, $to ) {
$price = sprintf(
/* translators: 1: price from 2: price to */
_x( '%1$s %2$s %3$s', 'Price range: from-to', 'wp-graphql-woocommerce' ),
is_numeric( $from ) ? wc_graphql_price( $from ) : $from,
apply_filters( 'graphql_format_price_range_separator', '-', $from, $to ),
is_numeric( $to ) ? wc_graphql_price( $to ) : $to
);

return apply_filters( 'graphql_format_price_range', $price, $from, $to );
}
2 changes: 2 additions & 0 deletions includes/class-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Tax_Status;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\WC_Connection_Orderby_Enum;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Tax_Rate_Connection_Orderby_Enum;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Pricing_Field_Format;
use WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Customer_Address_Input;
use WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Product_Attribute_Input;
use WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\WC_Connection_Orderby_Input;
Expand Down Expand Up @@ -103,6 +104,7 @@ public static function graphql_register_types() {
Tax_Status::register();
WC_Connection_Orderby_Enum::register();
Tax_Rate_Connection_Orderby_Enum::register();
Pricing_Field_Format::register();

// InputObjects.
Customer_Address_Input::register();
Expand Down
88 changes: 72 additions & 16 deletions includes/model/class-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,85 @@ protected function init() {
return ! empty( $this->data->get_date_paid() ) ? $this->data->get_date_paid() : null;
},
'discountTotal' => function() {
return ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
},
$price = ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'discountTotalRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'discountTax' => function() {
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
},
$price = ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'discountTaxRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'shippingTotal' => function() {
return ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
},
$price = ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'shippingTotalRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'shippingTax' => function() {
return ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
},
$price = ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'shippingTaxRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'cartTax' => function() {
return ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
},
$price = ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'cartTaxRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'total' => function() {
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
},
$price = ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'totalRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'totalTax' => function() {
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
},
$price = ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'totalTaxRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'subtotal' => function() {
return ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : 0;
},
$price = ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : null;
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
},
'subtotalRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : 0;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'orderNumber' => function() {
return ! empty( $this->data->get_order_number() ) ? $this->data->get_order_number() : null;
},
Expand Down
36 changes: 30 additions & 6 deletions includes/model/class-product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,38 @@ protected function init() {
return ! empty( $this->data->get_sku() ) ? $this->data->get_sku() : null;
},
'price' => function() {
return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null;
},
return ! empty( $this->data->get_price() )
? \wc_graphql_price( $this->data->get_price() )
: null;
},
'priceRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'salePrice' => function() {
return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null;
},
return ! empty( $this->data->get_sale_price() )
? \wc_graphql_price( $this->data->get_sale_price() )
: null;
},
'salePriceRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'regularPrice' => function() {
return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null;
},
return ! empty( $this->data->get_regular_price() ) ?
\wc_graphql_price( $this->data->get_regular_price() )
: null;
},
'regularPriceRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'dateOnSaleFrom' => function() {
return ! empty( $this->data->get_date_on_sale_from() ) ? $this->data->get_date_on_sale_from() : null;
},
Expand Down
Loading

0 comments on commit 7b0fe1b

Please sign in to comment.