Files

152 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2019-03-31 00:21:31 -04:00
<?php
2021-01-21 22:41:43 -05:00
/**
* Model - Coupon
*
* Resolves coupon crud object model
*
* @package WPGraphQL\WooCommerce\Model
* @since 0.0.1
*/
2019-03-31 00:21:31 -04:00
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Model;
2019-03-31 00:21:31 -04:00
use GraphQLRelay\Relay;
2020-02-13 23:00:55 -06:00
use WC_Coupon;
2019-03-31 00:21:31 -04:00
/**
* Class Coupon
2023-06-13 23:17:02 +03:00
*
* @property \WC_Coupon $wc_data
*
* @property int $ID
* @property string $id
* @property string $code
* @property string $date
* @property string $modified
* @property string $description
* @property string $discountType
* @property string $amount
* @property string $dateExpiry
* @property string $usageCount
* @property string $individualUse
* @property string $usageLimit
* @property string $usageLimitPerUser
* @property string $limitUsageToXItems
* @property string $freeShipping
* @property string $excludeSaleItems
* @property string $minimumAmount
* @property string $maximumAmount
* @property array $emailRestrictions
* @property array $product_ids
* @property array $excluded_product_ids
* @property array $product_category_ids
* @property array $excluded_product_category_ids
* @property array $used_by_ids
*
* @package WPGraphQL\WooCommerce\Model
2019-03-31 00:21:31 -04:00
*/
2020-04-30 17:12:56 -04:00
class Coupon extends WC_Post {
2019-03-31 00:21:31 -04:00
/**
* Coupon constructor
*
2021-07-05 13:51:52 -04:00
* @param int|\WC_Data $id - shop_coupon post-type ID.
2019-03-31 00:21:31 -04:00
*/
public function __construct( $id ) {
2020-09-22 08:17:01 -04:00
$data = new WC_Coupon( $id );
2019-03-31 22:48:57 -04:00
2020-09-22 08:17:01 -04:00
parent::__construct( $data );
2020-04-30 17:12:56 -04:00
}
2019-03-31 00:21:31 -04:00
/**
* Initializes the Coupon field resolvers
*/
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 () {
2023-06-13 23:17:02 +03:00
return ! empty( $this->ID ) ? Relay::toGlobalId( 'shop_coupon', "{$this->ID}" ) : null;
2019-03-31 00:21:31 -04:00
},
'code' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_code() ) ? $this->wc_data->get_code() : null;
2019-03-31 00:21:31 -04:00
},
'date' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_date_created() ) ? $this->wc_data->get_date_created() : null;
2019-03-31 00:21:31 -04:00
},
'modified' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_date_modified() ) ? $this->wc_data->get_date_modified() : null;
2019-03-31 00:21:31 -04: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-03-31 00:21:31 -04:00
},
'discountType' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_discount_type() ) ? $this->wc_data->get_discount_type() : null;
2019-03-31 00:21:31 -04:00
},
'amount' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_amount() ) ? $this->wc_data->get_amount() : null;
2019-03-31 00:21:31 -04:00
},
'dateExpiry' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_date_expires() ) ? $this->wc_data->get_date_expires() : null;
2019-03-31 00:21:31 -04:00
},
'usageCount' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->get_usage_count();
2019-03-31 00:21:31 -04:00
},
'individualUse' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->get_individual_use();
2019-03-31 00:21:31 -04:00
},
'usageLimit' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_usage_limit() ) ? $this->wc_data->get_usage_limit() : null;
2019-03-31 00:21:31 -04:00
},
'usageLimitPerUser' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_usage_limit_per_user() ) ? $this->wc_data->get_usage_limit_per_user() : null;
2019-03-31 00:21:31 -04:00
},
'limitUsageToXItems' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_limit_usage_to_x_items() ) ? $this->wc_data->get_limit_usage_to_x_items() : null;
2019-03-31 00:21:31 -04:00
},
'freeShipping' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->get_free_shipping();
2019-03-31 00:21:31 -04:00
},
'excludeSaleItems' => function () {
2023-06-13 23:17:02 +03:00
return $this->wc_data->get_exclude_sale_items();
2019-03-31 00:21:31 -04:00
},
'minimumAmount' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_minimum_amount() ) ? $this->wc_data->get_minimum_amount() : null;
2019-03-31 00:21:31 -04:00
},
'maximumAmount' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_maximum_amount() ) ? $this->wc_data->get_maximum_amount() : null;
2019-03-31 00:21:31 -04:00
},
'emailRestrictions' => function () {
2020-09-22 08:17:01 -04:00
return ! empty( $this->wc_data->get_email_restrictions() ) ? $this->wc_data->get_email_restrictions() : null;
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
*/
'product_ids' => function () {
return ! empty( $this->wc_data->get_product_ids() ) ? $this->wc_data->get_product_ids() : [ '0' ];
2019-03-31 00:21:31 -04:00
},
'excluded_product_ids' => function () {
return ! empty( $this->wc_data->get_excluded_product_ids() ) ? $this->wc_data->get_excluded_product_ids() : [ '0' ];
2019-03-31 00:21:31 -04:00
},
'product_category_ids' => function () {
return ! empty( $this->wc_data->get_product_categories() ) ? $this->wc_data->get_product_categories() : [ '0' ];
2019-03-31 00:21:31 -04:00
},
'excluded_product_category_ids' => function () {
return ! empty( $this->wc_data->get_excluded_product_categories() ) ? $this->wc_data->get_excluded_product_categories() : [ '0' ];
2019-03-31 00:21:31 -04:00
},
'used_by_ids' => function () {
return ! empty( $this->wc_data->get_used_by() ) ? $this->wc_data->get_used_by() : [ '0' ];
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
}
}