2019-03-16 21:59:41 -04:00
|
|
|
<?php
|
2019-03-22 16:36:34 -04:00
|
|
|
/**
|
|
|
|
|
* Factory
|
|
|
|
|
*
|
|
|
|
|
* This class serves as a factory for all the resolvers of queries and mutations.
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Data
|
2019-03-22 16:36:34 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
|
*/
|
2019-03-16 21:59:41 -04:00
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Data;
|
2019-03-16 21:59:41 -04:00
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
2019-04-06 12:39:55 -04:00
|
|
|
use WPGraphQL\AppContext;
|
2023-06-13 23:17:02 +03:00
|
|
|
use WPGraphQL\WooCommerce\Model\Coupon;
|
2023-07-19 23:01:50 +03:00
|
|
|
use WPGraphQL\WooCommerce\Model\Customer;
|
2023-06-13 23:17:02 +03:00
|
|
|
use WPGraphQL\WooCommerce\Model\Order;
|
2019-10-25 19:13:36 -04:00
|
|
|
use WPGraphQL\WooCommerce\Model\Order_Item;
|
2019-11-14 20:13:26 -05:00
|
|
|
use WPGraphQL\WooCommerce\Model\Product;
|
2023-06-13 23:17:02 +03:00
|
|
|
use WPGraphQL\WooCommerce\Model\Product_Variation;
|
2019-10-25 19:13:36 -04:00
|
|
|
use WPGraphQL\WooCommerce\Model\Shipping_Method;
|
2023-07-19 23:01:50 +03:00
|
|
|
use WPGraphQL\WooCommerce\Model\Tax_Rate;
|
|
|
|
|
use function WC;
|
2019-03-16 21:59:41 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Factory
|
|
|
|
|
*/
|
|
|
|
|
class Factory {
|
2019-12-05 19:56:50 -05:00
|
|
|
/**
|
|
|
|
|
* Returns the current woocommerce customer object tied to the current session.
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return \WPGraphQL\WooCommerce\Model\Customer
|
2019-12-05 19:56:50 -05:00
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
public static function resolve_session_customer() {
|
2020-05-01 15:06:22 -04:00
|
|
|
return new Customer();
|
2019-12-05 19:56:50 -05:00
|
|
|
}
|
|
|
|
|
|
2019-03-21 21:43:24 -04:00
|
|
|
/**
|
2019-03-31 00:21:31 -04:00
|
|
|
* Returns the Customer store object for the provided user ID
|
2019-03-28 01:15:32 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param int $id - user ID of the customer being retrieved.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context - AppContext object.
|
2019-03-28 01:15:32 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return null|\GraphQL\Deferred object
|
2019-03-28 01:15:32 -04:00
|
|
|
* @access public
|
|
|
|
|
*/
|
2019-04-06 12:39:55 -04:00
|
|
|
public static function resolve_customer( $id, AppContext $context ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
if ( 0 === absint( $id ) ) {
|
2019-03-28 01:15:32 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-13 23:17:02 +03:00
|
|
|
|
|
|
|
|
return $context->get_loader( 'wc_customer' )->load_deferred( absint( $id ) );
|
2019-03-28 01:15:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-31 00:21:31 -04:00
|
|
|
* Returns the WooCommerce CRUD object for the post ID
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param int $id - post ID of the crud object being retrieved.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context - AppContext object.
|
2019-03-31 00:21:31 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return null|\GraphQL\Deferred object
|
2019-03-31 00:21:31 -04:00
|
|
|
* @access public
|
|
|
|
|
*/
|
2019-04-06 12:39:55 -04:00
|
|
|
public static function resolve_crud_object( $id, AppContext $context ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
if ( 0 === absint( $id ) ) {
|
2019-03-31 00:21:31 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2020-06-12 16:50:21 -04:00
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
return $context->get_loader( 'wc_post' )->load_deferred( absint( $id ) );
|
2019-03-31 00:21:31 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-11 22:42:21 -04:00
|
|
|
/**
|
2019-04-12 18:28:08 -04:00
|
|
|
* Returns the order item Model for the order item.
|
2019-04-11 22:42:21 -04:00
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @param \WC_Order_Item $item - order item crud object instance.
|
2019-04-11 22:42:21 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return \WPGraphQL\WooCommerce\Model\Order_Item
|
2019-04-11 22:42:21 -04:00
|
|
|
* @access public
|
2023-07-19 23:01:50 +03:00
|
|
|
* @throws \GraphQL\Error\UserError Invalid object.
|
2019-04-11 22:42:21 -04:00
|
|
|
*/
|
|
|
|
|
public static function resolve_order_item( $item ) {
|
|
|
|
|
/**
|
|
|
|
|
* If $id is an instance of WC_Order_Item
|
|
|
|
|
*/
|
|
|
|
|
if ( is_a( $item, \WC_Order_Item::class ) ) {
|
|
|
|
|
return new Order_Item( $item );
|
|
|
|
|
} else {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Object provided to order item resolver is an invalid type', 'graphql-for-ecommerce' ) );
|
2019-04-11 22:42:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 14:44:51 -04:00
|
|
|
/**
|
2019-04-12 18:28:08 -04:00
|
|
|
* Returns the tax rate Model for the tax rate ID.
|
2019-04-12 14:44:51 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param int $id - Tax rate ID.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context - AppContext object.
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return null|\GraphQL\Deferred object
|
2019-04-12 14:44:51 -04:00
|
|
|
*/
|
2020-03-20 22:59:47 -04:00
|
|
|
public static function resolve_tax_rate( $id, AppContext $context ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
if ( 0 === absint( $id ) ) {
|
2020-03-20 22:59:47 -04:00
|
|
|
return null;
|
2019-04-12 14:44:51 -04:00
|
|
|
}
|
2020-03-20 22:59:47 -04:00
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
return $context->get_loader( 'tax_rate' )->load_deferred( absint( $id ) );
|
2019-04-12 14:44:51 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-12 18:28:08 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the shipping method Model for the shipping method ID.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id - Shipping method ID.
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return \WPGraphQL\WooCommerce\Model\Shipping_Method
|
2019-04-12 18:28:08 -04:00
|
|
|
* @access public
|
2023-07-19 23:01:50 +03:00
|
|
|
* @throws \GraphQL\Error\UserError Invalid object.
|
2019-04-12 18:28:08 -04:00
|
|
|
*/
|
|
|
|
|
public static function resolve_shipping_method( $id ) {
|
|
|
|
|
$wc_shipping = \WC_Shipping::instance();
|
|
|
|
|
$methods = $wc_shipping->get_shipping_methods();
|
|
|
|
|
if ( empty( $methods[ $id ] ) ) {
|
|
|
|
|
throw new UserError(
|
2019-07-09 13:01:19 -04:00
|
|
|
/* translators: shipping method ID */
|
2026-06-30 10:16:21 -04:00
|
|
|
sprintf( __( 'No Shipping Method assigned to ID %s was found ', 'graphql-for-ecommerce' ), $id )
|
2019-04-12 18:28:08 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$method = $methods[ $id ];
|
|
|
|
|
|
|
|
|
|
return new Shipping_Method( $method );
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 02:09:49 -04:00
|
|
|
/**
|
|
|
|
|
* Resolves the WooCommerce cart instance.
|
2019-12-03 16:26:10 -05:00
|
|
|
*
|
|
|
|
|
* @return \WC_Cart
|
|
|
|
|
*/
|
|
|
|
|
public static function resolve_cart() {
|
2020-05-07 02:09:49 -04:00
|
|
|
return WC()->cart;
|
2019-12-03 16:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 17:59:35 -04:00
|
|
|
/**
|
|
|
|
|
* Resolves a cart item by key.
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param string $key Cart item key.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context AppContext object.
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return null|\GraphQL\Deferred object
|
2019-05-07 17:59:35 -04:00
|
|
|
*/
|
2020-03-20 22:59:47 -04:00
|
|
|
public static function resolve_cart_item( $key, AppContext $context ) {
|
|
|
|
|
if ( empty( $key ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 23:17:02 +03:00
|
|
|
return $context->get_loader( 'cart_item' )->load_deferred( $key );
|
2019-05-07 17:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:59:47 -04:00
|
|
|
/**
|
|
|
|
|
* Resolves a downloadable item by ID.
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param int $id Downloadable item ID.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context AppContext object.
|
2020-06-12 16:50:21 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @return null|\GraphQL\Deferred object
|
2020-03-20 22:59:47 -04:00
|
|
|
*/
|
|
|
|
|
public static function resolve_downloadable_item( $id, AppContext $context ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
if ( 0 === absint( $id ) ) {
|
2020-03-20 22:59:47 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-13 23:17:02 +03:00
|
|
|
|
|
|
|
|
return $context->get_loader( 'downloadable_item' )->load_deferred( absint( $id ) );
|
2020-03-20 22:59:47 -04:00
|
|
|
}
|
|
|
|
|
|
2019-10-24 13:56:12 -04:00
|
|
|
/**
|
2024-11-06 19:23:36 -05:00
|
|
|
* Resolves Relay node for some WPGraphQL for WooCommerce types.
|
2019-10-24 13:56:12 -04:00
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param mixed $node Node object.
|
|
|
|
|
* @param int $id Object unique ID.
|
|
|
|
|
* @param string $type Node type.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context AppContext instance.
|
2019-10-24 13:56:12 -04:00
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function resolve_node( $node, $id, $type, $context ) {
|
|
|
|
|
switch ( $type ) {
|
|
|
|
|
case 'customer':
|
|
|
|
|
$node = self::resolve_customer( $id, $context );
|
|
|
|
|
break;
|
|
|
|
|
case 'shop_coupon':
|
|
|
|
|
case 'shop_order':
|
|
|
|
|
case 'shop_order_refund':
|
|
|
|
|
case 'product':
|
|
|
|
|
case 'product_variation':
|
|
|
|
|
$node = self::resolve_crud_object( $id, $context );
|
|
|
|
|
break;
|
|
|
|
|
case 'shipping_method':
|
|
|
|
|
$node = self::resolve_shipping_method( $id );
|
|
|
|
|
break;
|
|
|
|
|
case 'tax_rate':
|
2020-03-20 22:59:47 -04:00
|
|
|
$node = self::resolve_tax_rate( $id, $context );
|
2019-10-24 13:56:12 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-06 19:23:36 -05:00
|
|
|
* Resolves Relay node type for some WPGraphQL for WooCommerce types.
|
2019-10-24 13:56:12 -04:00
|
|
|
*
|
|
|
|
|
* @param string|null $type Node type.
|
|
|
|
|
* @param mixed $node Node object.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public static function resolve_node_type( $type, $node ) {
|
|
|
|
|
switch ( true ) {
|
|
|
|
|
case is_a( $node, Coupon::class ):
|
|
|
|
|
$type = 'Coupon';
|
|
|
|
|
break;
|
|
|
|
|
case is_a( $node, Customer::class ):
|
|
|
|
|
$type = 'Customer';
|
|
|
|
|
break;
|
|
|
|
|
case is_a( $node, Order::class ):
|
2023-06-21 13:21:29 -04:00
|
|
|
$type = $node->get_type() === 'shop_order_refund' ? 'Refund' : 'Order';
|
2019-10-24 13:56:12 -04:00
|
|
|
break;
|
2023-06-13 23:17:02 +03:00
|
|
|
case is_a( $node, Product::class ) && 'simple' === $node->get_type():
|
2019-10-24 17:34:58 -04:00
|
|
|
$type = 'SimpleProduct';
|
|
|
|
|
break;
|
2023-06-13 23:17:02 +03:00
|
|
|
case is_a( $node, Product::class ) && 'variable' === $node->get_type():
|
2019-10-24 17:34:58 -04:00
|
|
|
$type = 'VariableProduct';
|
|
|
|
|
break;
|
2023-06-13 23:17:02 +03:00
|
|
|
case is_a( $node, Product::class ) && 'external' === $node->get_type():
|
2019-10-24 17:34:58 -04:00
|
|
|
$type = 'ExternalProduct';
|
|
|
|
|
break;
|
2023-06-13 23:17:02 +03:00
|
|
|
case is_a( $node, Product::class ) && 'grouped' === $node->get_type():
|
2019-10-24 17:34:58 -04:00
|
|
|
$type = 'GroupProduct';
|
2019-10-24 13:56:12 -04:00
|
|
|
break;
|
|
|
|
|
case is_a( $node, Product_Variation::class ):
|
|
|
|
|
$type = 'ProductVariation';
|
|
|
|
|
break;
|
|
|
|
|
case is_a( $node, Shipping_Method::class ):
|
|
|
|
|
$type = 'ShippingMethod';
|
|
|
|
|
break;
|
|
|
|
|
case is_a( $node, Tax_Rate::class ):
|
|
|
|
|
$type = 'TaxRate';
|
|
|
|
|
break;
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end switch
|
2019-10-24 13:56:12 -04:00
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
|
}
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|