product model field filters implemented

This commit is contained in:
Geoff Taylor
2020-10-22 16:01:35 -04:00
parent 75d0e89ac9
commit cee407973f
4 changed files with 207 additions and 149 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ class Product_Variation extends WC_Post {
* @param int $id - product_variation post-type ID.
*/
public function __construct( $id ) {
$data = new WC_Product_Variation( $id );
$data = \wc_get_product( $id );
parent::__construct( $data );
}
+21 -10
View File
@@ -126,6 +126,7 @@ class Product extends WC_Post {
if ( empty( $this->fields ) ) {
parent::init();
$type = $this->wc_data->get_type();
$fields = array(
'id' => function() {
return ! empty( $this->wc_data->get_id() ) ? Relay::toGlobalId( 'product', $this->wc_data->get_id() ) : null;
@@ -260,7 +261,12 @@ class Product extends WC_Post {
},
);
if ( 'grouped' !== $this->wc_data->get_type() ) {
if (
apply_filters(
"graphql_{$type}_product_model_use_pricing_and_tax_fields",
'grouped' !== $this->wc_data->get_type()
)
) {
$fields += array(
'price' => function() {
return ! empty( $this->wc_data->get_price() )
@@ -295,7 +301,12 @@ class Product extends WC_Post {
);
}
if ( 'simple' === $this->wc_data->get_type() || 'variable' === $this->wc_data->get_type() ) {
if (
apply_filters(
"graphql_{$type}_product_model_use_inventory_fields",
'simple' === $type || 'variable' === $type
)
) {
$fields += array(
'manageStock' => function() {
return ! is_null( $this->wc_data->get_manage_stock() ) ? $this->wc_data->get_manage_stock() : null;
@@ -338,11 +349,14 @@ class Product extends WC_Post {
? array_map( 'absint', $this->wc_data->get_cross_sell_ids() )
: array( '0' );
},
'stockStatus' => function() {
return ! empty( $this->wc_data->get_stock_status() ) ? $this->wc_data->get_stock_status() : null;
},
);
}
switch ( $this->wc_data->get_type() ) {
case 'simple':
switch ( true ) {
case apply_filters( "graphql_{$type}_product_model_use_virtual_data_fields", 'simple' === $type ):
$fields += array(
'virtual' => function() {
return ! is_null( $this->wc_data->is_virtual() ) ? $this->wc_data->is_virtual() : null;
@@ -359,12 +373,9 @@ class Product extends WC_Post {
'downloads' => function() {
return ! empty( $this->wc_data->get_downloads() ) ? $this->wc_data->get_downloads() : null;
},
'stockStatus' => function() {
return ! empty( $this->wc_data->get_stock_status() ) ? $this->wc_data->get_stock_status() : null;
},
);
break;
case 'variable':
case apply_filters( "graphql_{$type}_product_model_use_variation_pricing_fields", 'variable' === $type ):
$fields = array(
'price' => function() {
return $this->get_variation_price();
@@ -391,7 +402,7 @@ class Product extends WC_Post {
},
) + $fields;
break;
case 'external':
case apply_filters( "graphql_{$type}_product_model_use_external_fields", 'external' === $type ):
$fields += array(
'externalUrl' => function() {
return ! empty( $this->wc_data->get_product_url() ) ? $this->wc_data->get_product_url() : null;
@@ -401,7 +412,7 @@ class Product extends WC_Post {
},
);
break;
case 'grouped':
case apply_filters( "graphql_{$type}_product_model_use_grouped_fields", 'grouped' === $type ):
$fields += array(
'addToCartText' => function() {
return ! empty( $this->wc_data->add_to_cart_text() ) ? $this->wc_data->add_to_cart_text() : null;
+18
View File
@@ -117,4 +117,22 @@ abstract class WC_Post extends Post {
return $this->wc_data->delete( $force_delete );
}
/**
* Returns the source WP_Post instance.
*
* @return \WP_Post
*/
public function as_WP_Post() {
return $this->data;
}
/**
* Returns the source WC_Data instance
*
* @return \WC_Data
*/
public function as_WC_Data() {
return $this->wc_data;
}
}
+167 -138
View File
@@ -52,142 +52,196 @@ class Product_Types {
/**
* Defines fields related to product inventory.
*
* @param array $fields Fields array for overwriting any of the inventory fields.
*
* @return array
*/
public static function get_inventory_fields() {
return array(
'manageStock' => array(
'type' => 'Boolean',
'description' => __( 'If product manage stock', 'wp-graphql-woocommerce' ),
),
'stockQuantity' => array(
'type' => 'Int',
'description' => __( 'Number of items available for sale', 'wp-graphql-woocommerce' ),
),
'backorders' => array(
'type' => 'BackordersEnum',
'description' => __( 'Product backorders status', 'wp-graphql-woocommerce' ),
),
'soldIndividually' => array(
'type' => 'Boolean',
'description' => __( 'If should be sold individually', 'wp-graphql-woocommerce' ),
),
'backordersAllowed' => array(
'type' => 'Boolean',
'description' => __( 'Can product be backordered?', 'wp-graphql-woocommerce' ),
public static function get_inventory_fields( $fields = array() ) {
return array_merge(
array(
'manageStock' => array(
'type' => 'Boolean',
'description' => __( 'If product manage stock', 'wp-graphql-woocommerce' ),
),
'stockQuantity' => array(
'type' => 'Int',
'description' => __( 'Number of items available for sale', 'wp-graphql-woocommerce' ),
),
'backorders' => array(
'type' => 'BackordersEnum',
'description' => __( 'Product backorders status', 'wp-graphql-woocommerce' ),
),
'soldIndividually' => array(
'type' => 'Boolean',
'description' => __( 'If should be sold individually', 'wp-graphql-woocommerce' ),
),
'backordersAllowed' => array(
'type' => 'Boolean',
'description' => __( 'Can product be backordered?', 'wp-graphql-woocommerce' ),
),
'stockStatus' => array(
'type' => 'StockStatusEnum',
'description' => __( 'Product stock status', 'wp-graphql-woocommerce' ),
),
),
$fields
);
}
/**
* Defines fields related to product shipping.
*
* @param array $fields Fields array for overwriting any of shipping fields.
*
* @return array
*/
public static function get_shipping_fields() {
return array(
'weight' => array(
'type' => 'String',
'description' => __( 'Product\'s weight', 'wp-graphql-woocommerce' ),
),
'length' => array(
'type' => 'String',
'description' => __( 'Product\'s length', 'wp-graphql-woocommerce' ),
),
'width' => array(
'type' => 'String',
'description' => __( 'Product\'s width', 'wp-graphql-woocommerce' ),
),
'height' => array(
'type' => 'String',
'description' => __( 'Product\'s height', 'wp-graphql-woocommerce' ),
),
'shippingClassId' => array(
'type' => 'Int',
'description' => __( 'shipping class ID', 'wp-graphql-woocommerce' ),
),
'shippingRequired' => array(
'type' => 'Boolean',
'description' => __( 'Does product need to be shipped?', 'wp-graphql-woocommerce' ),
),
'shippingTaxable' => array(
'type' => 'Boolean',
'description' => __( 'Is product shipping taxable?', 'wp-graphql-woocommerce' ),
public static function get_shipping_fields( $fields = array() ) {
return array_merge(
array(
'weight' => array(
'type' => 'String',
'description' => __( 'Product\'s weight', 'wp-graphql-woocommerce' ),
),
'length' => array(
'type' => 'String',
'description' => __( 'Product\'s length', 'wp-graphql-woocommerce' ),
),
'width' => array(
'type' => 'String',
'description' => __( 'Product\'s width', 'wp-graphql-woocommerce' ),
),
'height' => array(
'type' => 'String',
'description' => __( 'Product\'s height', 'wp-graphql-woocommerce' ),
),
'shippingClassId' => array(
'type' => 'Int',
'description' => __( 'shipping class ID', 'wp-graphql-woocommerce' ),
),
'shippingRequired' => array(
'type' => 'Boolean',
'description' => __( 'Does product need to be shipped?', 'wp-graphql-woocommerce' ),
),
'shippingTaxable' => array(
'type' => 'Boolean',
'description' => __( 'Is product shipping taxable?', 'wp-graphql-woocommerce' ),
),
),
$fields
);
}
/**
* Defines fields not found in grouped-type products.
* Defines fields related to pricing and taxes.
*
* @param array $fields Fields array for overwriting any of the pricing and tax fields.
*
* @return array
*/
public static function get_non_grouped_fields() {
return array(
'price' => array(
'type' => 'String',
'description' => __( 'Product\'s active price', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PricingFieldFormatEnum',
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
public static function get_pricing_and_tax_fields( $fields = array() ) {
return array_merge(
array(
'price' => array(
'type' => 'String',
'description' => __( 'Product\'s active price', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PricingFieldFormatEnum',
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->priceRaw;
} else {
return $source->price;
}
},
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->priceRaw;
} else {
return $source->price;
}
},
),
'regularPrice' => array(
'type' => 'String',
'description' => __( 'Product\'s regular price', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PricingFieldFormatEnum',
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
'regularPrice' => array(
'type' => 'String',
'description' => __( 'Product\'s regular price', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PricingFieldFormatEnum',
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->regularPriceRaw;
} else {
// @codingStandardsIgnoreLine.
return $source->regularPrice;
}
},
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->regularPriceRaw;
} else {
// @codingStandardsIgnoreLine.
return $source->regularPrice;
}
},
),
'salePrice' => array(
'type' => 'String',
'description' => __( 'Product\'s sale price', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PricingFieldFormatEnum',
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
'salePrice' => array(
'type' => 'String',
'description' => __( 'Product\'s sale price', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PricingFieldFormatEnum',
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->salePriceRaw;
} else {
// @codingStandardsIgnoreLine.
return $source->salePrice;
}
},
),
'taxStatus' => array(
'type' => 'TaxStatusEnum',
'description' => __( 'Tax status', 'wp-graphql-woocommerce' ),
),
'taxClass' => array(
'type' => 'TaxClassEnum',
'description' => __( 'Tax class', 'wp-graphql-woocommerce' ),
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->salePriceRaw;
} else {
// @codingStandardsIgnoreLine.
return $source->salePrice;
}
},
),
'taxStatus' => array(
'type' => 'TaxStatusEnum',
'description' => __( 'Tax status', 'wp-graphql-woocommerce' ),
),
'taxClass' => array(
'type' => 'TaxClassEnum',
'description' => __( 'Tax class', 'wp-graphql-woocommerce' ),
$fields
);
}
/**
* Defines fields related to virtual product info.
*
* @param array $fields Fields array for overwriting any of the virtual data fields.
*
* @return array
*/
public static function get_virtual_data_fields( $fields = array() ) {
return array_merge(
array(
'virtual' => array(
'type' => 'Boolean',
'description' => __( 'Is product virtual?', 'wp-graphql-woocommerce' ),
),
'downloadExpiry' => array(
'type' => 'Int',
'description' => __( 'Download expiry', 'wp-graphql-woocommerce' ),
),
'downloadable' => array(
'type' => 'Boolean',
'description' => __( 'Is downloadable?', 'wp-graphql-woocommerce' ),
),
'downloadLimit' => array(
'type' => 'Int',
'description' => __( 'Download limit', 'wp-graphql-woocommerce' ),
),
'downloads' => array(
'type' => array( 'list_of' => 'ProductDownload' ),
'description' => __( 'Product downloads', 'wp-graphql-woocommerce' ),
),
),
$fields
);
}
@@ -202,35 +256,10 @@ class Product_Types {
'interfaces' => self::get_product_interfaces(),
'fields' => array_merge(
Product::get_fields(),
self::get_non_grouped_fields(),
self::get_pricing_and_tax_fields(),
self::get_inventory_fields(),
self::get_shipping_fields(),
array(
'virtual' => array(
'type' => 'Boolean',
'description' => __( 'Is product virtual?', 'wp-graphql-woocommerce' ),
),
'downloadExpiry' => array(
'type' => 'Int',
'description' => __( 'Download expiry', 'wp-graphql-woocommerce' ),
),
'downloadable' => array(
'type' => 'Boolean',
'description' => __( 'Is downloadable?', 'wp-graphql-woocommerce' ),
),
'downloadLimit' => array(
'type' => 'Int',
'description' => __( 'Download limit', 'wp-graphql-woocommerce' ),
),
'downloads' => array(
'type' => array( 'list_of' => 'ProductDownload' ),
'description' => __( 'Product downloads', 'wp-graphql-woocommerce' ),
),
'stockStatus' => array(
'type' => 'StockStatusEnum',
'description' => __( 'Product stock status', 'wp-graphql-woocommerce' ),
),
)
self::get_virtual_data_fields(),
),
)
);
@@ -247,7 +276,7 @@ class Product_Types {
'interfaces' => self::get_product_interfaces(),
'fields' => array_merge(
Product::get_fields(),
self::get_non_grouped_fields(),
self::get_pricing_and_tax_fields(),
self::get_inventory_fields(),
self::get_shipping_fields()
),
@@ -266,7 +295,7 @@ class Product_Types {
'interfaces' => self::get_product_interfaces(),
'fields' => array_merge(
Product::get_fields(),
self::get_non_grouped_fields(),
self::get_pricing_and_tax_fields(),
array(
'externalUrl' => array(
'type' => 'String',