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