Files

336 lines
10 KiB
PHP
Raw Permalink Normal View History

2019-04-11 22:42:21 -04:00
<?php
/**
* Model - Order_Item
*
* Resolves model for order item crud objects
*
2019-10-25 19:13:36 -04:00
* @package WPGraphQL\WooCommerce\Model
2019-04-11 22:42:21 -04:00
* @since 0.0.2
*/
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Model;
2019-04-11 22:42:21 -04:00
2023-04-04 12:02:25 -04:00
use GraphQLRelay\Relay;
use WPGraphQL\Model\Model;
2019-04-11 22:42:21 -04:00
/**
* Class Order_Item
2023-06-13 23:17:02 +03:00
*
* @property \WC_Order_Item $wc_data
*
* @property int $ID
* @property string $id
* @property int $databaseId
* @property int $orderId
* @property string $type
*
* @property ?string $code
* @property ?string $discount
* @property ?string $discountTax
* @property ?string $coupon_id
*
* @property ?float $amount
* @property ?string $name
* @property ?string $taxStatus
* @property ?string $taxClass
* @property ?float $total
* @property ?float $totalTax
* @property ?string $taxes
*
* @property ?string $methodTitle
* @property ?string $method_id
* @property ?string $shippingTaxTotal
* @property ?array $taxes
* @property ?string $taxClass
*
* @property ?string $rateCode
* @property ?string $label
* @property ?float $taxTotal
* @property ?float $shippingTaxTotal
* @property ?bool $isCompound
* @property ?int $rate_id
*
* @property ?int $productId
* @property ?int $variationId
* @property ?int $quantity
* @property ?float $subtotal
* @property ?float $subtotalTax
* @property ?float $total
* @property ?float $totalTax
* @property ?string $taxes
* @property ?array $itemDownloads
* @property ?string $taxStatus
* @property ?string $taxClass
*
* @package WPGraphQL\WooCommerce\Model
2019-04-11 22:42:21 -04:00
*/
class Order_Item extends Model {
/**
2020-02-13 23:00:55 -06:00
* Stores order item type.
2019-04-11 22:42:21 -04:00
*
2023-06-13 23:17:02 +03:00
* @var string
2019-04-11 22:42:21 -04:00
*/
protected $item_type;
2019-07-09 03:55:55 -04:00
/**
* Stores parent order model.
*
* @var \WPGraphQL\WooCommerce\Model\Order
2019-07-09 03:55:55 -04:00
*/
protected $order;
2019-04-11 22:42:21 -04:00
/**
* Order_Item constructor
*
* @param \WC_Order_Item $item Order item crud object.
* @param null|\WPGraphQL\WooCommerce\Model\Order $cached_order Preloaded parent order model.
2019-04-11 22:42:21 -04:00
*/
2023-06-13 23:17:02 +03:00
public function __construct( $item, $cached_order = null ) {
2019-04-11 22:42:21 -04:00
$this->data = $item;
$this->item_type = $item->get_type();
2019-07-09 13:01:19 -04:00
$order_id = $item->get_order_id();
2023-06-13 23:17:02 +03:00
$this->order = ! empty( $cached_order ) ? $cached_order : new Order( $order_id );
$allowed_restricted_fields = [
2019-04-11 22:42:21 -04:00
'isRestricted',
'isPrivate',
'isPublic',
'id',
2020-09-16 16:32:14 -04:00
'databaseId',
];
2019-04-11 22:42:21 -04:00
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
2019-05-01 22:14:12 -04:00
$restricted_cap = apply_filters( 'order_item_restricted_cap', '' );
2023-06-13 23:17:02 +03:00
parent::__construct( $restricted_cap, $allowed_restricted_fields, 1 );
2019-04-11 22:42:21 -04:00
}
/**
* Forwards function calls to WC_Data sub-class instance.
*
* @param string $method - function name.
* @param array $args - function call arguments.
* @return mixed
*/
public function __call( $method, $args ) {
return $this->data->$method( ...$args );
}
2019-04-11 22:42:21 -04:00
/**
* Initializes the Order field resolvers
*/
protected function init() {
if ( empty( $this->fields ) ) {
$this->fields = [
'ID' => function () {
2019-04-11 22:42:21 -04:00
return $this->data->get_id();
},
2023-04-04 12:02:25 -04:00
'databaseId' => function () {
2023-06-13 23:17:02 +03:00
return ! empty( $this->ID ) ? $this->ID : null;
2019-04-11 22:42:21 -04:00
},
'orderId' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_order_id() ) ? $this->data->get_order_id() : null;
},
'id' => function () {
2023-06-13 23:17:02 +03:00
return ( ! empty( $this->orderId ) && ! empty( $this->ID ) ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
? Relay::toGlobalId( 'order_item', $this->orderId . '+' . $this->ID ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
: null;
2023-04-04 12:02:25 -04:00
},
'type' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_type() ) ? $this->data->get_type() : null;
},
];
2019-04-11 22:42:21 -04:00
switch ( $this->item_type ) {
case 'coupon':
$this->fields = array_merge(
$this->fields,
[
'code' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_code() ) ? $this->data->get_code() : null;
},
'discount' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_discount() ) ? $this->data->get_discount() : null;
},
'discountTax' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : null;
},
'coupon_id' => [
'callback' => function () {
2019-04-11 22:42:21 -04:00
$coupon_id = \wc_get_coupon_id_by_code( $this->data->get_code() );
return ! empty( $coupon_id ) ? $coupon_id : null;
},
'capability' => 'edit_shop_orders',
],
]
2019-04-11 22:42:21 -04:00
);
break;
case 'fee':
$this->fields = array_merge(
$this->fields,
[
'amount' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_amount() ) ? $this->data->get_amount() : null;
},
'name' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_name() ) ? $this->data->get_name() : null;
},
'taxStatus' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_tax_status() ) ? $this->data->get_tax_status() : null;
},
'taxClass' => function () {
2019-04-12 11:43:51 -04:00
if ( $this->data->get_tax_status() === 'taxable' ) {
return ! empty( $this->data->get_tax_class() )
? $this->data->get_tax_class()
2019-07-03 18:25:18 -04:00
: '';
2019-04-12 11:43:51 -04:00
}
return null;
},
'total' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : null;
},
'totalTax' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : null;
},
'taxes' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_taxes() )
? \wc_graphql_map_tax_statements( $this->data->get_taxes() )
: null;
},
]
2019-04-11 22:42:21 -04:00
);
break;
case 'shipping':
$this->fields = array_merge(
$this->fields,
[
'name' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_name() ) ? $this->data->get_name() : null;
},
'methodTitle' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_method_title() ) ? $this->data->get_method_title() : null;
},
'total' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : null;
},
'totalTax' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : null;
},
'taxes' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_taxes() )
? \wc_graphql_map_tax_statements( $this->data->get_taxes() )
: null;
},
'taxClass' => function () {
2019-04-16 17:48:39 -04:00
return ! empty( $this->data->get_tax_class() ) ? $this->data->get_tax_class() : 'standard';
2019-04-11 22:42:21 -04:00
},
'method_id' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_method_id() ) ? $this->data->get_method_id() : null;
},
]
2019-04-11 22:42:21 -04:00
);
break;
case 'tax':
$this->fields = array_merge(
$this->fields,
[
'rateCode' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_rate_code() ) ? $this->data->get_rate_code() : null;
},
'label' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_label() ) ? $this->data->get_label() : null;
},
'taxTotal' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_tax_total() ) ? $this->data->get_tax_total() : null;
},
'shippingTaxTotal' => function () {
2020-09-16 16:32:14 -04:00
return ! is_null( $this->data->get_shipping_tax_total() ) ? $this->data->get_shipping_tax_total() : 0;
2019-04-11 22:42:21 -04:00
},
'isCompound' => function () {
2020-09-16 16:32:14 -04:00
return ! is_null( $this->data->is_compound() ) ? $this->data->is_compound() : false;
2019-04-11 22:42:21 -04:00
},
'rate_id' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_rate_id() ) ? $this->data->get_rate_id() : null;
},
]
2019-04-11 22:42:21 -04:00
);
break;
default:
$this->fields = array_merge(
$this->fields,
[
'productId' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_product_id() ) ? $this->data->get_product_id() : null;
},
'variationId' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_variation_id() ) ? $this->data->get_variation_id() : null;
},
'quantity' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_quantity() ) ? $this->data->get_quantity() : null;
},
'subtotal' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : null;
},
'subtotalTax' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_subtotal_tax() ) ? $this->data->get_subtotal_tax() : null;
},
'total' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : null;
},
'totalTax' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : null;
},
'taxes' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_taxes() )
? \wc_graphql_map_tax_statements( $this->data->get_taxes() )
: null;
},
'itemDownloads' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_item_downloads() ) ? $this->data->get_item_downloads() : null;
},
'taxStatus' => function () {
2019-04-11 22:42:21 -04:00
return ! empty( $this->data->get_tax_status() ) ? $this->data->get_tax_status() : null;
},
'taxClass' => function () {
2019-04-12 11:43:51 -04:00
if ( $this->data->get_tax_status() === 'taxable' ) {
return ! empty( $this->data->get_tax_class() )
? $this->data->get_tax_class()
2019-07-03 18:25:18 -04:00
: '';
2019-04-12 11:43:51 -04:00
}
return null;
},
]
2019-04-11 22:42:21 -04:00
);
break;
}//end switch
}//end if
2019-04-11 22:42:21 -04:00
parent::prepare_fields();
}
2019-07-09 03:55:55 -04:00
/**
* Determines if the order item should be considered private
*
* @since 0.2.0
2020-02-13 23:00:55 -06:00
*
* @return bool
2019-07-09 03:55:55 -04:00
*/
protected function is_private() {
return $this->order->is_private();
}
/**
* Retrieve the cap to check if the data should be restricted for the order
*
* @since 0.2.0
2020-02-13 23:00:55 -06:00
*
* @return string
2019-07-09 03:55:55 -04:00
*/
protected function get_restricted_cap() {
2023-06-13 23:17:02 +03:00
return '';
2019-07-09 03:55:55 -04:00
}
2019-04-11 22:42:21 -04:00
}