Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions includes/model/class-product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,16 @@ protected function init() {
? \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() )
? \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() ) ?
\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,
),
'salePrice' => function() {
return ! empty( $this->data->get_sale_price() )
? \wc_graphql_price( $this->data->get_sale_price() )
: null;
},
'dateOnSaleFrom' => function() {
return ! empty( $this->data->get_date_on_sale_from() ) ? $this->data->get_date_on_sale_from() : null;
},
Expand Down Expand Up @@ -204,10 +186,33 @@ protected function init() {
'type' => function() {
return ! empty( $this->data->get_type() ) ? $this->data->get_type() : null;
},

/**
* Editor/Shop Manager only fields
*/
'priceRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'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,
),
'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,
),

/**
* Connection resolvers fields
*
* These field resolvers are used in connection resolvers to define WP_Query argument
* These field resolvers are used in connection resolvers to define WP_Query argument.
* Note: underscore naming style is used as a quick identifier
*/
'parent_id' => function() {
Expand Down
111 changes: 65 additions & 46 deletions includes/model/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,11 @@ protected function init() {
'featured' => function() {
return ! is_null( $this->data->get_featured() ) ? $this->data->get_featured() : null;
},
'catalogVisibility' => function() {
return ! empty( $this->data->get_catalog_visibility() ) ? $this->data->get_catalog_visibility() : null;
},
'description' => function() {
return ! empty( $this->data->get_description() )
? apply_filters( 'the_content', $this->data->get_description() )
: null;
},
'descriptionRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_description() ) ? $this->data->get_description() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'shortDescription' => function() {
$short_description = ! empty( $this->data->get_short_description() )
? apply_filters(
Expand All @@ -205,12 +196,6 @@ protected function init() {
: null;
return apply_filters( 'the_excerpt', $short_description );
},
'shortDescriptionRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_short_description() ) ? $this->data->get_short_description() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'sku' => function() {
return ! empty( $this->data->get_sku() ) ? $this->data->get_sku() : null;
},
Expand All @@ -220,9 +205,6 @@ protected function init() {
'dateOnSaleTo' => function() {
return ! empty( $this->data->get_date_on_sale_to() ) ? $this->data->get_date_on_sale_to() : null;
},
'totalSales' => function() {
return ! is_null( $this->data->get_total_sales() ) ? $this->data->get_total_sales() : null;
},
'reviewsAllowed' => function() {
return ! empty( $this->data->get_reviews_allowed() ) ? $this->data->get_reviews_allowed() : null;
},
Expand All @@ -244,6 +226,35 @@ protected function init() {
'purchasable' => function () {
return ! is_null( $this->data->is_purchasable() ) ? $this->data->is_purchasable() : null;
},

/**
* Editor/Shop Manager only fields
*/
'descriptionRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_description() ) ? $this->data->get_description() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'shortDescriptionRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_short_description() ) ? $this->data->get_short_description() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'catalogVisibility' => array(
'callback' => function() {
return ! empty( $this->data->get_catalog_visibility() ) ? $this->data->get_catalog_visibility() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'totalSales' => array(
'callback' => function() {
return ! is_null( $this->data->get_total_sales() ) ? $this->data->get_total_sales() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),

/**
* Connection resolvers fields
*
Expand Down Expand Up @@ -286,45 +297,49 @@ protected function init() {
);

if ( 'grouped' !== $this->data->get_type() ) {
$shared_fields['price'] = function() {
$shared_fields['price'] = function() {
return ! empty( $this->data->get_price() )
? \wc_graphql_price( $this->data->get_price() )
: null;
};
$shared_fields['regularPrice'] = function() {
return ! empty( $this->data->get_regular_price() )
? \wc_graphql_price( $this->data->get_regular_price() )
: null;
};
$shared_fields['salePrice'] = function() {
return ! empty( $this->data->get_sale_price() )
? \wc_graphql_price( $this->data->get_sale_price() )
: null;
};
$shared_fields['taxStatus'] = function() {
return ! empty( $this->data->get_tax_status() ) ? $this->data->get_tax_status() : null;
};
$shared_fields['taxClass'] = function() {
return ! is_null( $this->data->get_tax_class() ) ? $this->data->get_tax_class() : '';
};

/**
* Editor/Shop Manager only fields
*/
$shared_fields['priceRaw'] = array(
'callback' => function() {
return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
);
$shared_fields['regularPrice'] = function() {
return ! empty( $this->data->get_regular_price() )
? \wc_graphql_price( $this->data->get_regular_price() )
: null;
};
$shared_fields['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,
);
$shared_fields['salePrice'] = function() {
return ! empty( $this->data->get_sale_price() )
? \wc_graphql_price( $this->data->get_sale_price() )
: null;
};
$shared_fields['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,
);
$shared_fields['taxStatus'] = function() {
return ! empty( $this->data->get_tax_status() ) ? $this->data->get_tax_status() : null;
};
$shared_fields['taxClass'] = function() {
return ! is_null( $this->data->get_tax_class() ) ? $this->data->get_tax_class() : '';
};
}

if ( 'simple' === $this->data->get_type() || 'variable' === $this->data->get_type() ) {
Expand Down Expand Up @@ -400,35 +415,39 @@ protected function init() {
'price' => function() {
return $this->get_variation_price();
},
'regularPrice' => function() {
return $this->get_variation_price( 'regular' );
},
'salePrice' => function() {
return $this->get_variation_price( 'sale' );
},
'variation_ids' => function() {
return ! empty( $this->data->get_children() )
? array_map( 'absint', $this->data->get_children() )
: array( '0' );
},

/**
* Editor/Shop Manager only fields
*/
'priceRaw' => array(
'callback' => function() {
return $this->get_variation_price( '', true );
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'regularPrice' => function() {
return $this->get_variation_price( 'regular' );
},
'regularPriceRaw' => array(
'callback' => function() {
return $this->get_variation_price( 'regular', true );
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'salePrice' => function() {
return $this->get_variation_price( 'sale' );
},
'salePriceRaw' => array(
'callback' => function() {
return $this->get_variation_price( 'sale', true );
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'variation_ids' => function() {
return ! empty( $this->data->get_children() )
? array_map( 'absint', $this->data->get_children() )
: array( '0' );
},
);
break;
case 'external':
Expand Down
9 changes: 7 additions & 2 deletions tests/_support/Helper/crud-helpers/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ public function create_download( $id = 0 ) {

public function print_query( $id, $raw = false ) {
$data = wc_get_product( $id );
$is_shop_manager = false;
$user = wp_get_current_user();
if ( $user && in_array( 'shop_manager', (array) $user->roles ) ) {
$is_shop_manager = true;
}

return array(
'id' => $this->to_relay_id( $id ),
Expand All @@ -310,7 +315,6 @@ public function print_query( $id, $raw = false ) {
'modified' => $data->get_date_modified()->__toString(),
'status' => $data->get_status(),
'featured' => $data->get_featured(),
'catalogVisibility' => strtoupper( $data->get_catalog_visibility() ),
'description' => ! empty( $data->get_description() )
? $raw
? $data->get_description()
Expand All @@ -336,7 +340,6 @@ public function print_query( $id, $raw = false ) {
: null,
'dateOnSaleFrom' => $data->get_date_on_sale_from(),
'dateOnSaleTo' => $data->get_date_on_sale_to(),
'totalSales' => $data->get_total_sales(),
'taxStatus' => strtoupper( $data->get_tax_status() ),
'taxClass' => ! empty( $data->get_tax_class() )
? $data->get_tax_class()
Expand Down Expand Up @@ -367,6 +370,8 @@ public function print_query( $id, $raw = false ) {
'shippingRequired' => $data->needs_shipping(),
'shippingTaxable' => $data->is_shipping_taxable(),
'link' => get_post_permalink( $id ),
'totalSales' => $is_shop_manager ? $data->get_total_sales() : null,
'catalogVisibility' => $is_shop_manager ? strtoupper( $data->get_catalog_visibility() ) :null,
);
}

Expand Down