Files

255 lines
9.0 KiB
PHP
Raw Permalink Normal View History

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 ) ) {
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
}
/**
* 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
*/
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();
$fields = [
'ID' => function () {
2023-06-13 23:17:02 +03:00
return ! empty( $this->wc_data->get_id() ) ? $this->wc_data->get_id() : null;
},
'id' => function () {
return ! empty( $this->ID ) ? Relay::toGlobalId( 'post', "{$this->ID}" ) : null;
2019-03-31 00:21:31 -04:00
},
'name' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_name() ) ? $this->wc_data->get_name() : null;
},
'date' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data ) ? $this->wc_data->get_date_created() : null;
},
'modified' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data ) ? $this->wc_data->get_date_modified() : null;
},
'description' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_description() ) ? $this->wc_data->get_description() : null;
},
'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
},
'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() ] ) )
: null;
},
'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() ] ) )
: null;
},
'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;
},
'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;
},
'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;
},
'onSale' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->is_on_sale();
},
'status' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_status() ) ? $this->wc_data->get_status() : null;
},
'purchasable' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->is_purchasable() ) ? $this->wc_data->is_purchasable() : null;
},
'virtual' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->is_virtual();
},
'downloadable' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->is_downloadable();
},
'downloads' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_downloads() ) ? $this->wc_data->get_downloads() : null;
},
'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;
},
'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;
},
'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;
},
'taxClass' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->get_tax_class();
},
'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;
},
'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;
},
'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;
},
'backorders' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_backorders() ) ? $this->wc_data->get_backorders() : null;
},
'backordersAllowed' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->backorders_allowed();
},
'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
},
'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
},
'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
},
'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
},
'menuOrder' => function () {
return $this->wc_data->get_menu_order();
2019-03-31 00:21:31 -04: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
},
'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
},
'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
},
'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
},
'priceRaw' => function () {
return ! empty( $this->wc_data->get_price() ) ? $this->wc_data->get_price() : null;
},
'regularPriceRaw' => function () {
return ! empty( $this->wc_data->get_regular_price() ) ? $this->wc_data->get_regular_price() : null;
},
2020-01-11 15:39:04 -05:00
'salePriceRaw' => function () {
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
*/
'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;
},
'parentId' => function () {
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
},
'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
},
'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
},
'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
},
];
2019-03-31 00:21:31 -04:00
2020-09-22 08:17:01 -04:00
$this->fields = array_merge( $this->fields, $fields );
}//end if
2019-03-31 00:21:31 -04:00
}
}