• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 23675172456

28 Mar 2026 02:10AM UTC coverage: 70.983% (-18.4%) from 89.424%
23675172456

Pull #1003

github

web-flow
Merge 05339093d into 6fb7b226f
Pull Request #1003: devops: WC email template tests, COT cursor HPOS fix, checkout account auth

71 of 81 new or added lines in 5 files covered. (87.65%)

3346 existing lines in 124 files now uncovered.

12576 of 17717 relevant lines covered (70.98%)

55.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.25
/includes/type/interface/class-product.php
1
<?php
2
/**
3
 * WPInterface Type - Product
4
 *
5
 * Registers Product interface.
6
 *
7
 * @package WPGraphQL\WooCommerce\Type\WPInterface
8
 * @since   0.3.0
9
 */
10

11
namespace WPGraphQL\WooCommerce\Type\WPInterface;
12

13
use GraphQL\Error\UserError;
14
use GraphQLRelay\Relay;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\WooCommerce\Data\Factory;
17

18
/**
19
 * Class - Product
20
 */
21
class Product {
22
        /**
23
         * Registers the "Product" interface.
24
         *
25
         * @return void
26
         */
27
        public static function register_interface() {
28
                // Register the fields to the Product Interface
29
                // the product interface is defined by the post_type registration.
30
                register_graphql_field(
110✔
31
                        'RootQuery',
110✔
32
                        'product',
110✔
33
                        [
110✔
34
                                'type'        => 'Product',
110✔
35
                                'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
110✔
36
                                'args'        => [
110✔
37
                                        'id'     => [
110✔
38
                                                'type'        => [ 'non_null' => 'ID' ],
110✔
39
                                                'description' => __( 'The ID for identifying the product', 'wp-graphql-woocommerce' ),
110✔
40
                                        ],
110✔
41
                                        'idType' => [
110✔
42
                                                'type'        => 'ProductIdTypeEnum',
110✔
43
                                                'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
110✔
44
                                        ],
110✔
45
                                ],
110✔
46
                                'resolve'     => static function ( $source, array $args, AppContext $context ) {
110✔
47
                                        $id      = isset( $args['id'] ) ? $args['id'] : null;
8✔
48
                                        $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
8✔
49

50
                                        $product_id = null;
8✔
51
                                        switch ( $id_type ) {
52
                                                case 'sku':
8✔
UNCOV
53
                                                        $product_id = \wc_get_product_id_by_sku( $id );
×
UNCOV
54
                                                        break;
×
55
                                                case 'slug':
8✔
56
                                                        $query = new \WP_Query(
1✔
57
                                                                [
1✔
58
                                                                        'name'           => $id,
1✔
59
                                                                        'post_type'      => 'product',
1✔
60
                                                                        'post_status'    => 'publish',
1✔
61
                                                                        'posts_per_page' => 1,
1✔
62
                                                                        'fields'         => 'ids',
1✔
63
                                                                ]
1✔
64
                                                        );
1✔
65
                                                        /** @var int $post_id */
66
                                                        $post_id    = ! empty( $query->posts ) ? $query->posts[0] : 0;
1✔
67
                                                        $product_id = absint( $post_id );
1✔
68
                                                        break;
1✔
69
                                                case 'database_id':
7✔
70
                                                        $product_id = absint( $id );
5✔
71
                                                        break;
5✔
72
                                                case 'global_id':
2✔
73
                                                default:
74
                                                        $id_components = Relay::fromGlobalId( $id );
2✔
75
                                                        if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
2✔
76
                                                                throw new UserError( __( 'The "global ID" is invalid', 'wp-graphql-woocommerce' ) );
×
77
                                                        }
78
                                                        $product_id = absint( $id_components['id'] );
2✔
79
                                                        break;
2✔
80
                                        }
81

82
                                        if ( empty( $product_id ) ) {
8✔
83
                                                /* translators: %1$s: ID type, %2$s: ID value */
84
                                                throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
85
                                        }
86
                                        $product = get_post( $product_id );
8✔
87
                                        if ( ! is_object( $product ) || 'product' !== $product->post_type ) {
8✔
88
                                                /* translators: %1$s: ID type, %2$s: ID value */
89
                                                throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
90
                                        }
91

92
                                        return Factory::resolve_crud_object( $product_id, $context );
8✔
93
                                },
110✔
94
                        ]
110✔
95
                );
110✔
96
        }
97

98
        /**
99
         * Defines Product fields. All child type must have these fields as well.
100
         *
101
         * @return array
102
         */
103
        public static function get_fields() {
104
                return [
110✔
105
                        'type'              => [
110✔
106
                                'type'        => 'ProductTypesEnum',
110✔
107
                                'description' => __( 'Product type', 'wp-graphql-woocommerce' ),
110✔
108
                        ],
110✔
109
                        'name'              => [
110✔
110
                                'type'        => 'String',
110✔
111
                                'description' => __( 'Product name', 'wp-graphql-woocommerce' ),
110✔
112
                        ],
110✔
113
                        'featured'          => [
110✔
114
                                'type'        => 'Boolean',
110✔
115
                                'description' => __( 'If the product is featured', 'wp-graphql-woocommerce' ),
110✔
116
                        ],
110✔
117
                        'catalogVisibility' => [
110✔
118
                                'type'        => 'CatalogVisibilityEnum',
110✔
119
                                'description' => __( 'Catalog visibility', 'wp-graphql-woocommerce' ),
110✔
120
                        ],
110✔
121
                        'description'       => [
110✔
122
                                'type'        => 'String',
110✔
123
                                'description' => __( 'Product description', 'wp-graphql-woocommerce' ),
110✔
124
                                'args'        => [
110✔
125
                                        'format' => [
110✔
126
                                                'type'        => 'PostObjectFieldFormatEnum',
110✔
127
                                                'description' => __( 'Format of the field output', 'wp-graphql-woocommerce' ),
110✔
128
                                        ],
110✔
129
                                ],
110✔
130
                                'resolve'     => static function ( $source, $args ) {
110✔
UNCOV
131
                                        if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
×
132
                                                // @codingStandardsIgnoreLine.
UNCOV
133
                                                return $source->descriptionRaw;
×
134
                                        }
UNCOV
135
                                        return $source->description;
×
136
                                },
110✔
137
                        ],
110✔
138
                        'shortDescription'  => [
110✔
139
                                'type'        => 'String',
110✔
140
                                'description' => __( 'Product short description', 'wp-graphql-woocommerce' ),
110✔
141
                                'args'        => [
110✔
142
                                        'format' => [
110✔
143
                                                'type'        => 'PostObjectFieldFormatEnum',
110✔
144
                                                'description' => __( 'Format of the field output', 'wp-graphql-woocommerce' ),
110✔
145
                                        ],
110✔
146
                                ],
110✔
147
                                'resolve'     => static function ( $source, $args ) {
110✔
UNCOV
148
                                        if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
×
149
                                                // @codingStandardsIgnoreLine.
UNCOV
150
                                                return $source->shortDescriptionRaw;
×
151
                                        }
152
                                        // @codingStandardsIgnoreLine.
UNCOV
153
                                        return $source->shortDescription;
×
154
                                },
110✔
155
                        ],
110✔
156
                        'sku'               => [
110✔
157
                                'type'        => 'String',
110✔
158
                                'description' => __( 'Product SKU', 'wp-graphql-woocommerce' ),
110✔
159
                        ],
110✔
160
                        'dateOnSaleFrom'    => [
110✔
161
                                'type'        => 'String',
110✔
162
                                'description' => __( 'Date on sale from', 'wp-graphql-woocommerce' ),
110✔
163
                        ],
110✔
164
                        'dateOnSaleTo'      => [
110✔
165
                                'type'        => 'String',
110✔
166
                                'description' => __( 'Date on sale to', 'wp-graphql-woocommerce' ),
110✔
167
                        ],
110✔
168
                        'totalSales'        => [
110✔
169
                                'type'        => 'Int',
110✔
170
                                'description' => __( 'Number total of sales', 'wp-graphql-woocommerce' ),
110✔
171
                        ],
110✔
172
                        'reviewsAllowed'    => [
110✔
173
                                'type'        => 'Boolean',
110✔
174
                                'description' => __( 'If reviews are allowed', 'wp-graphql-woocommerce' ),
110✔
175
                        ],
110✔
176
                        'purchaseNote'      => [
110✔
177
                                'type'        => 'String',
110✔
178
                                'description' => __( 'Purchase note', 'wp-graphql-woocommerce' ),
110✔
179
                        ],
110✔
180
                        'menuOrder'         => [
110✔
181
                                'type'        => 'Int',
110✔
182
                                'description' => __( 'Menu order', 'wp-graphql-woocommerce' ),
110✔
183
                        ],
110✔
184
                        'averageRating'     => [
110✔
185
                                'type'        => 'Float',
110✔
186
                                'description' => __( 'Product average count', 'wp-graphql-woocommerce' ),
110✔
187
                        ],
110✔
188
                        'reviewCount'       => [
110✔
189
                                'type'        => 'Int',
110✔
190
                                'description' => __( 'Product review count', 'wp-graphql-woocommerce' ),
110✔
191
                        ],
110✔
192
                        'image'             => [
110✔
193
                                'type'        => 'MediaItem',
110✔
194
                                'description' => __( 'Main image', 'wp-graphql-woocommerce' ),
110✔
195
                                'resolve'     => static function ( $source, array $args, AppContext $context ) {
110✔
196
                                        // @codingStandardsIgnoreLine.
UNCOV
197
                                        if ( empty( $source->image_id ) || ! absint( $source->image_id ) ) {
×
198
                                                return null;
×
199
                                        }
UNCOV
200
                                        return $context->get_loader( 'post' )->load_deferred( $source->image_id );
×
201
                                },
110✔
202
                        ],
110✔
203
                        'onSale'            => [
110✔
204
                                'type'        => 'Boolean',
110✔
205
                                'description' => __( 'Is product on sale?', 'wp-graphql-woocommerce' ),
110✔
206
                        ],
110✔
207
                        'purchasable'       => [
110✔
208
                                'type'        => 'Boolean',
110✔
209
                                'description' => __( 'Can product be purchased?', 'wp-graphql-woocommerce' ),
110✔
210
                        ],
110✔
211
                        'virtual'           => [
110✔
212
                                'type'        => 'Boolean',
110✔
213
                                'description' => __( 'Is product virtual?', 'wp-graphql-woocommerce' ),
110✔
214
                        ],
110✔
215
                        'metaData'          => \WPGraphQL\WooCommerce\Type\WPObject\Meta_Data_Type::get_metadata_field_definition(),
110✔
216
                ];
110✔
217
        }
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc