2019-03-31 00:21:31 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Model - Product
|
|
|
|
|
*
|
|
|
|
|
* Resolves product crud object model
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Model
|
2019-03-31 00:21:31 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Model;
|
2019-03-31 00:21:31 -04:00
|
|
|
|
|
|
|
|
use GraphQLRelay\Relay;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Product
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @property \WC_Product $wc_data
|
|
|
|
|
* @property \WP_Post_Type $post_type_object
|
|
|
|
|
*
|
|
|
|
|
* @property int $ID
|
|
|
|
|
* @property string $id
|
|
|
|
|
* @property string $type
|
|
|
|
|
* @property string $slug
|
|
|
|
|
* @property string $name
|
|
|
|
|
* @property string $date
|
|
|
|
|
* @property string $modified
|
|
|
|
|
* @property string $status
|
|
|
|
|
* @property bool $featured
|
|
|
|
|
* @property string $description
|
|
|
|
|
* @property string $descriptionRaw
|
|
|
|
|
* @property string $shortDescription
|
|
|
|
|
* @property string $shortDescriptionRaw
|
|
|
|
|
* @property string $sku
|
|
|
|
|
* @property string $dateOnSaleFrom
|
|
|
|
|
* @property string $dateOnSaleTo
|
|
|
|
|
* @property bool $reviewsAllowed
|
|
|
|
|
* @property string $purchaseNote
|
|
|
|
|
* @property int $menuOrder
|
|
|
|
|
* @property float $averageRating
|
|
|
|
|
* @property int $reviewCount
|
|
|
|
|
* @property bool $onSale
|
|
|
|
|
* @property bool $purchasable
|
|
|
|
|
* @property string $catalogVisibility
|
|
|
|
|
* @property int $totalSales
|
|
|
|
|
*
|
|
|
|
|
* @property array $upsell_ids
|
|
|
|
|
* @property array $attributes
|
|
|
|
|
* @property array $default_attributes
|
|
|
|
|
* @property int $image_id
|
|
|
|
|
* @property int[] $gallery_image_ids
|
|
|
|
|
* @property int[] $category_ids
|
|
|
|
|
* @property int[] $tag_ids
|
|
|
|
|
* @property int $parent_id
|
|
|
|
|
*
|
|
|
|
|
* @property bool $manageStock
|
|
|
|
|
* @property int $stockQuantity
|
|
|
|
|
* @property string $backorders
|
|
|
|
|
* @property bool $backordersAllowed
|
|
|
|
|
* @property bool $soldIndividually
|
|
|
|
|
* @property float $weight
|
|
|
|
|
* @property float $length
|
|
|
|
|
* @property float $width
|
|
|
|
|
* @property float $height
|
|
|
|
|
* @property int $shippingClassId
|
|
|
|
|
* @property bool $shippingRequired
|
|
|
|
|
* @property bool $shippingTaxable
|
|
|
|
|
* @property array $cross_sell_ids
|
|
|
|
|
* @property string $stockStatus
|
|
|
|
|
*
|
|
|
|
|
* @property bool $virtual
|
|
|
|
|
* @property int $downloadExpiry
|
|
|
|
|
* @property bool $downloadable
|
|
|
|
|
* @property int $downloadLimit
|
|
|
|
|
* @property array $downloads
|
|
|
|
|
*
|
|
|
|
|
* @property string $price
|
|
|
|
|
* @property float|float[] $priceRaw
|
|
|
|
|
* @property string $regularPrice
|
|
|
|
|
* @property float|float[] $regularPriceRaw
|
|
|
|
|
* @property string $salePrice
|
|
|
|
|
* @property float|float[] $salePriceRaw
|
|
|
|
|
* @property string $taxStatus
|
|
|
|
|
* @property string $taxClass
|
|
|
|
|
*
|
|
|
|
|
* @property string $externalUrl
|
|
|
|
|
* @property string $buttonText
|
|
|
|
|
*
|
|
|
|
|
* @property string $addToCartText
|
|
|
|
|
* @property string $addToCartDescription
|
|
|
|
|
* @property int[] $grouped_ids
|
|
|
|
|
*
|
|
|
|
|
* @property int[] $variation_ids
|
|
|
|
|
*
|
2026-03-20 13:31:13 -04:00
|
|
|
* @mixin \WC_Product
|
2023-06-13 23:17:02 +03:00
|
|
|
* @package WPGraphQL\WooCommerce\Model
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
2020-04-30 17:12:56 -04:00
|
|
|
class Product extends WC_Post {
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
2020-02-13 23:00:55 -06:00
|
|
|
* Stores the product type: external, grouped, simple, variable.
|
2019-03-31 00:21:31 -04:00
|
|
|
*
|
2020-02-13 23:00:55 -06:00
|
|
|
* @var string
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
|
|
|
|
protected $product_type;
|
|
|
|
|
|
2026-03-31 09:17:44 -04:00
|
|
|
/**
|
|
|
|
|
* Cached variation prices to avoid redundant lookups across
|
|
|
|
|
* multiple pricing fields (price, regularPrice, salePrice, etc.).
|
|
|
|
|
*
|
|
|
|
|
* @var array|null
|
|
|
|
|
*/
|
|
|
|
|
private $variation_prices = null;
|
|
|
|
|
|
2019-12-03 16:26:10 -05:00
|
|
|
/**
|
2020-02-13 23:00:55 -06:00
|
|
|
* Stores product factory.
|
2019-12-03 16:26:10 -05:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @var \WC_Product_Factory|null
|
2019-12-03 16:26:10 -05:00
|
|
|
*/
|
|
|
|
|
protected static $product_factory = null;
|
|
|
|
|
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
2020-02-13 23:00:55 -06:00
|
|
|
* Product constructor.
|
2019-03-31 00:21:31 -04:00
|
|
|
*
|
2021-07-05 13:51:52 -04:00
|
|
|
* @param int|\WC_Data $id - product post-type ID.
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @throws \Exception Failed to retrieve product data source.
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
|
|
|
|
public function __construct( $id ) {
|
2020-04-30 17:12:56 -04:00
|
|
|
// Get WC_Product object.
|
2021-07-05 13:51:52 -04:00
|
|
|
$data = \wc_get_product( $id );
|
2020-04-30 17:12:56 -04:00
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
// Check if product is valid.
|
|
|
|
|
if ( ! is_object( $data ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new \Exception( __( 'Failed to retrieve product data source', 'graphql-for-ecommerce' ) );
|
2023-06-13 23:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-22 08:17:01 -04:00
|
|
|
parent::__construct( $data );
|
2019-03-31 22:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-27 10:22:36 -05:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
|
|
|
|
protected static function get_allowed_restricted_fields( $allowed_restricted_fields = [] ) {
|
|
|
|
|
return array_merge(
|
|
|
|
|
parent::get_allowed_restricted_fields(),
|
|
|
|
|
[ 'type' ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* Returns the product type.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get_type() {
|
|
|
|
|
return $this->wc_data->get_type();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-02 00:06:32 -04:00
|
|
|
/**
|
|
|
|
|
* Returns string of variation price range.
|
|
|
|
|
*
|
2019-06-03 13:48:29 -04:00
|
|
|
* @param string $pricing_type - Range selected pricing type.
|
|
|
|
|
* @param boolean $raw - Whether to return raw value.
|
2019-06-02 00:06:32 -04:00
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2019-06-03 13:48:29 -04:00
|
|
|
private function get_variation_price( $pricing_type = '', $raw = false ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* Variable product data source.
|
|
|
|
|
*
|
|
|
|
|
* @var \WC_Product_Variable $data
|
|
|
|
|
*/
|
|
|
|
|
$data = $this->wc_data;
|
|
|
|
|
|
2026-03-31 09:17:44 -04:00
|
|
|
if ( is_null( $this->variation_prices ) ) {
|
|
|
|
|
$this->variation_prices = $data->get_variation_prices( true );
|
|
|
|
|
}
|
|
|
|
|
$prices = $this->variation_prices;
|
2019-06-02 00:06:32 -04:00
|
|
|
|
2020-09-22 08:17:01 -04:00
|
|
|
if ( empty( $prices['price'] ) || ( 'sale' === $pricing_type && ! $this->wc_data->is_on_sale() ) ) {
|
2019-06-02 00:06:32 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 18:14:33 -05:00
|
|
|
switch ( $pricing_type ) {
|
|
|
|
|
case 'sale':
|
|
|
|
|
$prices = array_values( array_diff( $prices['sale_price'], $prices['regular_price'] ) );
|
|
|
|
|
break;
|
|
|
|
|
case 'regular':
|
|
|
|
|
$prices = $prices['regular_price'];
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$prices = $prices['price'];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort( $prices, SORT_NUMERIC );
|
2024-08-06 18:07:37 -04:00
|
|
|
graphql_debug( implode( ', ', $prices ) );
|
2021-01-26 18:14:33 -05:00
|
|
|
|
|
|
|
|
if ( $raw ) {
|
2021-09-08 11:32:25 +05:30
|
|
|
return implode( ', ', $prices );
|
2021-01-26 18:14:33 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-21 13:21:29 -04:00
|
|
|
return wc_graphql_price_range( current( $prices ), end( $prices ) );
|
2019-06-02 00:06:32 -04:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
|
|
|
|
* Initializes the Product field resolvers
|
|
|
|
|
*
|
2019-04-03 20:04:32 -04:00
|
|
|
* @access protected
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
2019-04-03 20:04:32 -04:00
|
|
|
protected function init() {
|
2019-03-31 00:21:31 -04:00
|
|
|
if ( empty( $this->fields ) ) {
|
2020-09-22 08:17:01 -04:00
|
|
|
parent::init();
|
|
|
|
|
|
2020-10-22 16:01:35 -04:00
|
|
|
$type = $this->wc_data->get_type();
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields = [
|
2023-07-20 00:11:25 +03:00
|
|
|
'ID' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return ! empty( $this->wc_data->get_id() ) ? $this->wc_data->get_id() : null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'id' => function () {
|
2024-08-06 16:54:18 -06:00
|
|
|
return ! empty( $this->ID ) ? Relay::toGlobalId( 'post', "{$this->ID}" ) : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'type' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_type() ) ? $this->wc_data->get_type() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'slug' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_slug() ) ? $this->wc_data->get_slug() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'name' => function () {
|
2021-10-22 18:12:37 -04:00
|
|
|
return ! empty( $this->wc_data->get_name() ) ? html_entity_decode( $this->wc_data->get_name() ) : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'date' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data ) ? $this->wc_data->get_date_created() : null;
|
2019-04-06 12:39:55 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'modified' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data ) ? $this->wc_data->get_date_modified() : null;
|
2019-04-06 12:39:55 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'status' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_status() ) ? $this->wc_data->get_status() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'featured' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_featured();
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'description' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_description() )
|
2020-02-16 00:04:40 -05:00
|
|
|
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
2020-09-22 08:17:01 -04:00
|
|
|
? apply_filters( 'the_content', $this->wc_data->get_description() )
|
2019-09-03 15:36:48 -04:00
|
|
|
: null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'descriptionRaw' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_description() ) ? $this->wc_data->get_description() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'shortDescription' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
$short_description = ! empty( $this->wc_data->get_short_description() )
|
2019-11-04 22:49:03 -05:00
|
|
|
? apply_filters(
|
2020-02-16 00:04:40 -05:00
|
|
|
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
2019-11-04 22:49:03 -05:00
|
|
|
'get_the_excerpt',
|
2020-09-22 08:17:01 -04:00
|
|
|
$this->wc_data->get_short_description(),
|
|
|
|
|
get_post( $this->wc_data->get_id() )
|
2019-11-04 22:49:03 -05:00
|
|
|
)
|
|
|
|
|
: null;
|
2020-02-16 00:04:40 -05:00
|
|
|
|
|
|
|
|
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
2019-09-03 15:36:48 -04:00
|
|
|
return apply_filters( 'the_excerpt', $short_description );
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'shortDescriptionRaw' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_short_description() ) ? $this->wc_data->get_short_description() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'sku' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_sku() ) ? $this->wc_data->get_sku() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'dateOnSaleFrom' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_date_on_sale_from() ) ? $this->wc_data->get_date_on_sale_from() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'dateOnSaleTo' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_date_on_sale_to() ) ? $this->wc_data->get_date_on_sale_to() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'reviewsAllowed' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_reviews_allowed() ) ? $this->wc_data->get_reviews_allowed() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'purchaseNote' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_purchase_note() ) ? $this->wc_data->get_purchase_note() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'menuOrder' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_menu_order();
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'averageRating' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_average_rating();
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'reviewCount' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_review_count();
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2019-10-17 21:39:38 -04:00
|
|
|
'onSale' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_on_sale();
|
2019-04-08 14:07:11 -04:00
|
|
|
},
|
2019-10-17 21:39:38 -04:00
|
|
|
'purchasable' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_purchasable();
|
2019-04-08 14:07:11 -04:00
|
|
|
},
|
2020-01-11 15:39:04 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Editor/Shop Manager only fields
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
'catalogVisibility' => [
|
2023-07-20 00:11:25 +03:00
|
|
|
'callback' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_catalog_visibility() ) ? $this->wc_data->get_catalog_visibility() : null;
|
2020-01-11 15:39:04 -05:00
|
|
|
},
|
|
|
|
|
'capability' => $this->post_type_object->cap->edit_posts,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'totalSales' => [
|
2023-07-20 00:11:25 +03:00
|
|
|
'callback' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_total_sales();
|
2020-01-11 15:39:04 -05:00
|
|
|
},
|
|
|
|
|
'capability' => $this->post_type_object->cap->edit_posts,
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2020-01-11 15:39:04 -05:00
|
|
|
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
|
|
|
|
* Connection resolvers fields
|
|
|
|
|
*
|
|
|
|
|
* These field resolvers are used in connection resolvers to define WP_Query argument
|
|
|
|
|
* Note: underscore naming style is used as a quick identifier
|
|
|
|
|
*/
|
2023-07-20 00:11:25 +03:00
|
|
|
'upsell_ids' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_upsell_ids() )
|
|
|
|
|
? array_map( 'absint', $this->wc_data->get_upsell_ids() )
|
2022-08-26 14:53:36 -04:00
|
|
|
: [ '0' ];
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'attributes' => function () {
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $this->wc_data->get_attributes() ) ? $this->wc_data->get_attributes() : [];
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'default_attributes' => function () {
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $this->wc_data->get_default_attributes() ) ? $this->wc_data->get_default_attributes() : [ '0' ];
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2019-10-17 21:39:38 -04:00
|
|
|
'image_id' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_image_id() ) ? $this->wc_data->get_image_id() : null;
|
2019-06-05 19:15:37 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'gallery_image_ids' => function () {
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $this->wc_data->get_gallery_image_ids() ) ? $this->wc_data->get_gallery_image_ids() : [ '0' ];
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'category_ids' => function () {
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $this->wc_data->get_category_ids() ) ? $this->wc_data->get_category_ids() : [ '0' ];
|
2019-03-31 22:48:57 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'tag_ids' => function () {
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $this->wc_data->get_tag_ids() ) ? $this->wc_data->get_tag_ids() : [ '0' ];
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'parent_id' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_parent_id() ) ? $this->wc_data->get_parent_id() : null;
|
2019-06-05 19:15:37 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'post' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->post ) ? $this->wc_data->post : null;
|
2019-11-13 21:06:01 -05:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-10-17 21:39:38 -04:00
|
|
|
|
2020-10-22 16:01:35 -04:00
|
|
|
if (
|
|
|
|
|
apply_filters(
|
|
|
|
|
"graphql_{$type}_product_model_use_pricing_and_tax_fields",
|
2021-01-26 18:14:33 -05:00
|
|
|
'grouped' !== $this->wc_data->get_type()
|
2020-10-22 16:01:35 -04:00
|
|
|
)
|
|
|
|
|
) {
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields += [
|
2023-07-20 00:11:25 +03:00
|
|
|
'price' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_price() )
|
2023-06-21 13:21:29 -04:00
|
|
|
? wc_graphql_price( \wc_get_price_to_display( $this->wc_data ) )
|
2020-09-07 16:42:59 -04:00
|
|
|
: null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'priceRaw' => function () {
|
2023-03-17 19:52:15 +01:00
|
|
|
return ! empty( $this->wc_data->get_price() ) ? $this->wc_data->get_price() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'regularPrice' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_regular_price() )
|
2023-06-21 13:21:29 -04:00
|
|
|
? wc_graphql_price( \wc_get_price_to_display( $this->wc_data, [ 'price' => $this->wc_data->get_regular_price() ] ) )
|
2020-09-07 16:42:59 -04:00
|
|
|
: null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'regularPriceRaw' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_regular_price() ) ? $this->wc_data->get_regular_price() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'salePrice' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_sale_price() )
|
2023-06-21 13:21:29 -04:00
|
|
|
? wc_graphql_price(
|
2023-03-10 19:46:39 +01:00
|
|
|
\wc_get_price_to_display(
|
2023-03-17 19:52:15 +01:00
|
|
|
$this->wc_data,
|
2023-03-10 19:46:39 +01:00
|
|
|
[
|
|
|
|
|
'price' => $this->wc_data->get_sale_price(),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-09-07 16:42:59 -04:00
|
|
|
: null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'salePriceRaw' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_sale_price() ) ? $this->wc_data->get_sale_price() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'taxStatus' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_tax_status() ) ? $this->wc_data->get_tax_status() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'taxClass' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_tax_class();
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end if
|
2019-10-17 21:39:38 -04:00
|
|
|
|
2020-10-22 16:01:35 -04:00
|
|
|
if (
|
|
|
|
|
apply_filters(
|
|
|
|
|
"graphql_{$type}_product_model_use_inventory_fields",
|
|
|
|
|
'simple' === $type || 'variable' === $type
|
|
|
|
|
)
|
|
|
|
|
) {
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields += [
|
2023-07-20 00:11:25 +03:00
|
|
|
'manageStock' => function () {
|
2023-09-15 21:03:55 -04:00
|
|
|
return ! empty( $this->wc_data->get_manage_stock() ) ? $this->wc_data->get_manage_stock() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'stockQuantity' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_stock_quantity() ) ? $this->wc_data->get_stock_quantity() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'backorders' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_backorders() ) ? $this->wc_data->get_backorders() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'backordersAllowed' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->backorders_allowed();
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'soldIndividually' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_sold_individually();
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-09-15 23:31:24 -04:00
|
|
|
'lowStockAmount' => function () {
|
|
|
|
|
return ! empty( $this->wc_data->get_low_stock_amount() ) ? $this->wc_data->get_low_stock_amount() : null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'weight' => function () {
|
2020-10-27 23:55:16 -04:00
|
|
|
return ! empty( $this->wc_data->get_weight() ) ? $this->wc_data->get_weight() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'length' => function () {
|
2020-10-27 23:55:16 -04:00
|
|
|
return ! empty( $this->wc_data->get_length() ) ? $this->wc_data->get_length() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'width' => function () {
|
2020-10-27 23:55:16 -04:00
|
|
|
return ! empty( $this->wc_data->get_width() ) ? $this->wc_data->get_width() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'height' => function () {
|
2020-10-27 23:55:16 -04:00
|
|
|
return ! empty( $this->wc_data->get_height() ) ? $this->wc_data->get_height() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
|
|
|
|
'shippingClassId' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_image_id() ) ? $this->wc_data->get_shipping_class_id() : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'shippingRequired' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->needs_shipping();
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'shippingTaxable' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_shipping_taxable();
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'cross_sell_ids' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_cross_sell_ids() )
|
|
|
|
|
? array_map( 'absint', $this->wc_data->get_cross_sell_ids() )
|
2022-08-26 14:53:36 -04:00
|
|
|
: [ '0' ];
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'stockStatus' => function () {
|
2020-10-22 16:01:35 -04:00
|
|
|
return ! empty( $this->wc_data->get_stock_status() ) ? $this->wc_data->get_stock_status() : null;
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end if
|
2019-10-17 21:39:38 -04:00
|
|
|
|
2020-10-22 16:01:35 -04:00
|
|
|
switch ( true ) {
|
|
|
|
|
case apply_filters( "graphql_{$type}_product_model_use_virtual_data_fields", 'simple' === $type ):
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields += [
|
2023-07-20 00:11:25 +03:00
|
|
|
'virtual' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_virtual();
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'downloadExpiry' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->get_download_expiry();
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'downloadable' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_downloadable();
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'downloadLimit' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return ! empty( $this->wc_data->get_download_limit() ) ? $this->wc_data->get_download_limit() : null;
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'downloads' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_downloads() ) ? $this->wc_data->get_downloads() : null;
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-10-17 21:39:38 -04:00
|
|
|
break;
|
2020-10-22 16:01:35 -04:00
|
|
|
case apply_filters( "graphql_{$type}_product_model_use_variation_pricing_fields", 'variable' === $type ):
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields = [
|
2023-07-20 00:11:25 +03:00
|
|
|
'price' => function () {
|
2019-10-17 21:39:38 -04:00
|
|
|
return $this->get_variation_price();
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'regularPrice' => function () {
|
2020-01-11 15:39:04 -05:00
|
|
|
return $this->get_variation_price( 'regular' );
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'salePrice' => function () {
|
2020-01-11 15:39:04 -05:00
|
|
|
return $this->get_variation_price( 'sale' );
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'variation_ids' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_children() )
|
|
|
|
|
? array_map( 'absint', $this->wc_data->get_children() )
|
2022-08-26 14:53:36 -04:00
|
|
|
: [ '0' ];
|
2020-01-11 15:39:04 -05:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'priceRaw' => function () {
|
2020-08-11 19:36:47 -04:00
|
|
|
return $this->get_variation_price( '', true );
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'regularPriceRaw' => function () {
|
2020-08-11 19:36:47 -04:00
|
|
|
return $this->get_variation_price( 'regular', true );
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'salePriceRaw' => function () {
|
2020-08-11 19:36:47 -04:00
|
|
|
return $this->get_variation_price( 'sale', true );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
] + $fields;
|
2019-10-17 21:39:38 -04:00
|
|
|
break;
|
2020-10-22 16:01:35 -04:00
|
|
|
case apply_filters( "graphql_{$type}_product_model_use_external_fields", 'external' === $type ):
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields += [
|
2023-07-20 00:11:25 +03:00
|
|
|
'externalUrl' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* External product
|
|
|
|
|
*
|
|
|
|
|
* @var \WC_Product_External $data
|
|
|
|
|
*/
|
|
|
|
|
$data = $this->wc_data;
|
|
|
|
|
return ! empty( $data->get_product_url() ) ? $data->get_product_url() : null;
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'buttonText' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* External product
|
|
|
|
|
*
|
|
|
|
|
* @var \WC_Product_External $data
|
|
|
|
|
*/
|
|
|
|
|
$data = $this->wc_data;
|
|
|
|
|
return ! empty( $data->get_button_text() ) ? $data->get_button_text() : null;
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-10-17 21:39:38 -04:00
|
|
|
break;
|
2021-01-21 22:41:43 -05:00
|
|
|
case apply_filters( "graphql_{$type}_product_model_use_grouped_fields", 'grouped' === $type ):
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields += [
|
2023-07-20 00:11:25 +03:00
|
|
|
'addToCartText' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->add_to_cart_text() ) ? $this->wc_data->add_to_cart_text() : null;
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'addToCartDescription' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->add_to_cart_description() )
|
|
|
|
|
? $this->wc_data->add_to_cart_description()
|
2019-10-17 21:39:38 -04:00
|
|
|
: null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'grouped_ids' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_children() )
|
|
|
|
|
? array_map( 'absint', $this->wc_data->get_children() )
|
2022-08-26 14:53:36 -04:00
|
|
|
: [ '0' ];
|
2019-10-17 21:39:38 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-10-17 21:39:38 -04:00
|
|
|
break;
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end switch
|
2019-10-17 21:39:38 -04:00
|
|
|
|
2020-09-07 16:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Defines aliased fields
|
|
|
|
|
*
|
|
|
|
|
* These fields are used primarily by WPGraphQL core Node* interfaces
|
|
|
|
|
* and some fields act as aliases/decorator for existing fields.
|
|
|
|
|
*/
|
2022-08-26 14:53:36 -04:00
|
|
|
$fields += [
|
2023-07-20 00:11:25 +03:00
|
|
|
'commentCount' => function () {
|
2021-01-21 22:41:43 -05:00
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
|
2023-06-13 23:17:02 +03:00
|
|
|
return ! empty( $this->reviewCount ) ? $this->reviewCount : null;
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'commentStatus' => function () {
|
2021-01-21 22:41:43 -05:00
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
|
2023-06-13 23:17:02 +03:00
|
|
|
return isset( $this->reviewsAllowed ) && $this->reviewsAllowed ? 'open' : 'closed';
|
2020-09-07 16:42:59 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-09-07 16:42:59 -04:00
|
|
|
|
2020-09-22 08:17:01 -04:00
|
|
|
$this->fields = array_merge( $this->fields, $fields );
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end if
|
2019-03-31 00:21:31 -04:00
|
|
|
}
|
|
|
|
|
}
|