@@ -55,48 +55,90 @@ public static function register_interface( &$type_registry ) {
5555 'type ' => 'Product ' ,
5656 'description ' => __ ( 'A product object ' , 'wp-graphql-woocommerce ' ),
5757 'args ' => array (
58- 'id ' => array (
58+ 'id ' => array (
5959 'type ' => array ( 'non_null ' => 'ID ' ),
6060 ),
61+ 'idType ' => array (
62+ 'type ' => 'ProductIdTypeEnum ' ,
63+ 'description ' => __ ( 'Type of ID being used identify product ' , 'wp-graphql-woocommerce ' ),
64+ ),
6165 ),
6266 'resolve ' => function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
63- $ id_components = Relay::fromGlobalId ( $ args ['id ' ] );
64- if ( ! isset ( $ id_components ['id ' ] ) || ! absint ( $ id_components ['id ' ] ) ) {
65- throw new UserError ( __ ( 'The ID input is invalid ' , 'wp-graphql-woocommerce ' ) );
67+ $ id = isset ( $ args ['id ' ] ) ? $ args ['id ' ] : null ;
68+ $ id_type = isset ( $ args ['idType ' ] ) ? $ args ['idType ' ] : 'global_id ' ;
69+
70+ $ product_id = null ;
71+ switch ( $ id_type ) {
72+ case 'sku ' :
73+ $ product_id = \wc_get_product_id_by_sku ( $ id );
74+ break ;
75+ case 'slug ' :
76+ $ post = get_page_by_path ( $ id , OBJECT , 'product ' );
77+ $ product_id = ! empty ( $ post ) ? absint ( $ post ->ID ) : 0 ;
78+ break ;
79+ case 'database_id ' :
80+ $ product_id = absint ( $ id );
81+ break ;
82+ case 'global_id ' :
83+ default :
84+ $ id_components = Relay::fromGlobalId ( $ id );
85+ if ( empty ( $ id_components ['id ' ] ) || empty ( $ id_components ['type ' ] ) ) {
86+ throw new UserError ( __ ( 'The "global ID" is invalid ' , 'wp-graphql-woocommerce ' ) );
87+ }
88+ $ product_id = absint ( $ id_components ['id ' ] );
89+ break ;
90+ }
91+
92+ if ( empty ( $ product_id ) ) {
93+ /* translators: %1$s: ID type, %2$s: ID value */
94+ throw new UserError ( sprintf ( __ ( 'No product ID was found corresponding to the %1$s: %2$s ' ), $ id_type , $ id ) );
95+ } elseif ( get_post ( $ product_id )->post_type !== 'product ' ) {
96+ /* translators: %1$s: ID type, %2$s: ID value */
97+ throw new UserError ( sprintf ( __ ( 'No product exists with the %1$s: %2$s ' ), $ id_type , $ id ) );
6698 }
67- $ product_id = absint ( $ id_components ['id ' ] );
68- return Factory::resolve_crud_object ( $ product_id , $ context );
99+
100+ $ product = Factory::resolve_crud_object ( $ product_id , $ context );
101+
102+ return $ product ;
69103 },
70104 )
71105 );
72106
73- $ post_by_args = array (
74- 'id ' => array (
75- 'type ' => 'ID ' ,
76- 'description ' => __ ( 'Get the product by its global ID ' , 'wp-graphql-woocommerce ' ),
77- ),
78- 'productId ' => array (
79- 'type ' => 'Int ' ,
80- 'description ' => __ ( 'Get the product by its database ID ' , 'wp-graphql-woocommerce ' ),
81- ),
82- 'slug ' => array (
83- 'type ' => 'String ' ,
84- 'description ' => __ ( 'Get the product by its slug ' , 'wp-graphql-woocommerce ' ),
85- ),
86- 'sku ' => array (
87- 'type ' => 'String ' ,
88- 'description ' => __ ( 'Get the product by its sku ' , 'wp-graphql-woocommerce ' ),
89- ),
90- );
91-
107+ /**
108+ * DEPRECATED
109+ *
110+ * Will be removed in v0.5.x.
111+ */
92112 register_graphql_field (
93113 'RootQuery ' ,
94114 'productBy ' ,
95115 array (
96- 'type ' => 'Product ' ,
97- 'description ' => __ ( 'A product object ' , 'wp-graphql-woocommerce ' ),
98- 'args ' => $ post_by_args ,
99- 'resolve ' => function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
116+ 'type ' => 'Product ' ,
117+ 'isDeprecated ' => true ,
118+ 'deprecationReason ' => __ (
119+ 'This query has been deprecation, and will be removed in v0.5.x. Please use "product(id: value, idType: DATABASE_ID|SLUG|SKU)" instead ' ,
120+ 'wp-graphql-woocommerce '
121+ ),
122+ 'description ' => __ ( 'A product object ' , 'wp-graphql-woocommerce ' ),
123+ 'args ' => array (
124+ 'id ' => array (
125+ 'type ' => 'ID ' ,
126+ 'description ' => __ ( 'Get the product by its global ID ' , 'wp-graphql-woocommerce ' ),
127+ ),
128+ 'productId ' => array (
129+ 'type ' => 'Int ' ,
130+ 'description ' => __ ( 'Get the product by its database ID ' , 'wp-graphql-woocommerce ' ),
131+ ),
132+ 'slug ' => array (
133+ 'type ' => 'String ' ,
134+ 'description ' => __ ( 'Get the product by its slug ' , 'wp-graphql-woocommerce ' ),
135+ ),
136+ 'sku ' => array (
137+ 'type ' => 'String ' ,
138+ 'description ' => __ ( 'Get the product by its sku ' , 'wp-graphql-woocommerce ' ),
139+ ),
140+ ),
141+ 'resolve ' => function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
100142 $ product_id = 0 ;
101143 $ id_type = '' ;
102144 if ( ! empty ( $ args ['id ' ] ) ) {
0 commit comments