2019-03-31 00:21:31 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Model - Product_Variation
|
|
|
|
|
*
|
|
|
|
|
* Resolves product variation 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_Variation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @property \WC_Product_Variation $wc_data
|
|
|
|
|
*
|
|
|
|
|
* @property int $ID
|
|
|
|
|
* @property string $id
|
|
|
|
|
* @property string $name
|
|
|
|
|
* @property string $date
|
|
|
|
|
* @property string $modified
|
|
|
|
|
* @property string $description
|
|
|
|
|
* @property string $sku
|
|
|
|
|
* @property string $price
|
|
|
|
|
* @property float $priceRaw
|
|
|
|
|
* @property string $regularPrice
|
|
|
|
|
* @property float $regularPriceRaw
|
|
|
|
|
* @property string $salePrice
|
|
|
|
|
* @property float $salePriceRaw
|
|
|
|
|
* @property string $dateOnSaleFrom
|
|
|
|
|
* @property string $dateOnSaleTo
|
|
|
|
|
* @property bool $onSale
|
|
|
|
|
* @property string $status
|
|
|
|
|
* @property bool $purchasable
|
|
|
|
|
* @property bool $virtual
|
|
|
|
|
* @property bool $downloadable
|
|
|
|
|
* @property array $downloads
|
|
|
|
|
* @property int $downloadLimit
|
|
|
|
|
* @property int $downloadExpiry
|
|
|
|
|
* @property string $taxStatus
|
|
|
|
|
* @property string $taxClass
|
|
|
|
|
* @property bool $manageStock
|
|
|
|
|
* @property int $stockQuantity
|
|
|
|
|
* @property string $stockStatus
|
|
|
|
|
* @property string $backorders
|
|
|
|
|
* @property bool $backordersAllowed
|
|
|
|
|
* @property float $weight
|
|
|
|
|
* @property float $length
|
|
|
|
|
* @property float $width
|
|
|
|
|
* @property float $height
|
|
|
|
|
* @property int $menuOrder
|
|
|
|
|
* @property string $purchaseNote
|
|
|
|
|
* @property string $catalogVisibility
|
|
|
|
|
* @property bool $hasAttributes
|
|
|
|
|
* @property string $type
|
|
|
|
|
* @property int $parent_id
|
|
|
|
|
* @property string $parentId
|
|
|
|
|
* @property int $shipping_class_id
|
|
|
|
|
* @property int $image_id
|
|
|
|
|
* @property array $attributes
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Model
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
2020-04-30 17:12:56 -04:00
|
|
|
class Product_Variation extends WC_Post {
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
|
|
|
|
* Product_Variation constructor
|
|
|
|
|
*
|
2021-07-05 13:51:52 -04:00
|
|
|
* @param int|\WC_Data $id - product_variation post-type ID.
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @throws \Exception If product variation cannot be retrieved.
|
2019-03-31 00:21:31 -04:00
|
|
|
*/
|
|
|
|
|
public function __construct( $id ) {
|
2020-10-22 16:01:35 -04:00
|
|
|
$data = \wc_get_product( $id );
|
2021-07-05 13:51:52 -04:00
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
// Check if product variation is valid.
|
|
|
|
|
if ( ! is_object( $data ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new \Exception( __( 'Failed to retrieve product variation data source', 'graphql-for-ecommerce' ) );
|
2023-06-13 23:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-22 08:17:01 -04:00
|
|
|
parent::__construct( $data );
|
2020-04-30 17:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-27 10:22:36 -05:00
|
|
|
/**
|
|
|
|
|
* Returns the product variation type.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get_type() {
|
|
|
|
|
return $this->wc_data->get_type();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
2020-02-13 23:00:55 -06:00
|
|
|
* Initializes the ProductVariation field resolvers.
|
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();
|
|
|
|
|
|
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
|
|
|
'name' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_name() ) ? $this->wc_data->get_name() : null;
|
2019-04-28 16:03:24 -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-28 16:03:24 -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-28 16:03:24 -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() ) ? $this->wc_data->get_description() : null;
|
2019-04-28 16:03:24 -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
|
|
|
'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, [ 'price' => $this->wc_data->get_price() ] ) )
|
2019-06-02 00:29:16 -04:00
|
|
|
: null;
|
2019-04-28 16:03:24 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'regularPrice' => function () {
|
2023-03-21 20:32:06 +01: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() ] ) )
|
2019-06-02 00:29:16 -04:00
|
|
|
: null;
|
2019-04-28 16:03:24 -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( \wc_get_price_to_display( $this->wc_data, [ 'price' => $this->wc_data->get_sale_price() ] ) )
|
2020-01-11 15:39:04 -05:00
|
|
|
: null;
|
|
|
|
|
},
|
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-04-28 16:03:24 -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-04-28 16:03:24 -04:00
|
|
|
},
|
2019-04-29 16:11:42 -04:00
|
|
|
'onSale' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return $this->wc_data->is_on_sale();
|
2019-04-28 16:03:24 -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-04-28 16:03:24 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'purchasable' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->is_purchasable() ) ? $this->wc_data->is_purchasable() : null;
|
2019-04-28 16:03:24 -04:00
|
|
|
},
|
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-04-28 16:03:24 -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-04-28 16:03:24 -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-04-28 16:03:24 -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-04-28 16:03:24 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'downloadExpiry' => function () {
|
2023-06-13 23:17:02 +03:00
|
|
|
return ! empty( $this->wc_data->get_download_expiry() ) ? $this->wc_data->get_download_expiry() : null;
|
2019-04-28 16:03:24 -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;
|
2019-04-28 16:03:24 -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();
|
2019-04-28 16:03:24 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'manageStock' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_manage_stock() ) ? $this->wc_data->get_manage_stock() : null;
|
2019-04-28 16:03:24 -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;
|
2019-04-28 16:03:24 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'stockStatus' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_stock_status() ) ? $this->wc_data->get_stock_status() : null;
|
2019-04-28 16:03:24 -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;
|
2019-04-28 16:03:24 -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();
|
2019-04-28 16:03:24 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'weight' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_weight() ) ? $this->wc_data->get_weight() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'length' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_length() ) ? $this->wc_data->get_length() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'width' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_width() ) ? $this->wc_data->get_width() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'height' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_height() ) ? $this->wc_data->get_height() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'menuOrder' => function () {
|
2023-08-22 17:14:39 -04:00
|
|
|
return $this->wc_data->get_menu_order();
|
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
|
|
|
'catalogVisibility' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_catalog_visibility() ) ? $this->wc_data->get_catalog_visibility() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'hasAttributes' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->has_attributes() ) ? $this->wc_data->has_attributes() : 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-04-23 16:24:45 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'priceRaw' => function () {
|
2022-12-07 23:26:55 +01:00
|
|
|
return ! empty( $this->wc_data->get_price() ) ? $this->wc_data->get_price() : null;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'regularPriceRaw' => function () {
|
2022-12-07 23:26:55 +01:00
|
|
|
return ! empty( $this->wc_data->get_regular_price() ) ? $this->wc_data->get_regular_price() : null;
|
|
|
|
|
},
|
2020-01-11 15:39:04 -05:00
|
|
|
|
2023-07-20 00:11:25 +03:00
|
|
|
'salePriceRaw' => function () {
|
2022-12-07 23:26:55 +01:00
|
|
|
return ! empty( $this->wc_data->get_sale_price() ) ? $this->wc_data->get_sale_price() : null;
|
|
|
|
|
},
|
2020-01-11 15:39:04 -05:00
|
|
|
|
2019-03-31 00:21:31 -04:00
|
|
|
/**
|
|
|
|
|
* Connection resolvers fields
|
|
|
|
|
*
|
2020-01-11 15:39:04 -05:00
|
|
|
* These field resolvers are used in connection resolvers to define WP_Query argument.
|
2019-03-31 00:21:31 -04:00
|
|
|
* Note: underscore naming style is used as a quick identifier
|
|
|
|
|
*/
|
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;
|
|
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'parentId' => function () {
|
2024-08-06 16:54:18 -06:00
|
|
|
return ! empty( $this->wc_data->get_parent_id() ) ? Relay::toGlobalId( 'post', (string) $this->wc_data->get_parent_id() ) : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'shipping_class_id' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_shipping_class_id() ) ? $this->wc_data->get_shipping_class_id() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03: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-03-31 00:21:31 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'attributes' => function () {
|
2020-09-22 08:17:01 -04:00
|
|
|
return ! empty( $this->wc_data->get_attributes() ) ? $this->wc_data->get_attributes() : null;
|
2019-03-31 00:21:31 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-03-31 00:21:31 -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
|
|
|
}
|
|
|
|
|
}
|