Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add WooCommerce Product Bundles to GraphQL API
<?php
namespace App\GraphQL;
const TYPE_BUNDLE_PRODUCT = 'BundleProduct';
/**
* Register BundleProduct Type
*/
add_action( 'graphql_register_types', function () {
/**
* Register our bundle object type
*/
register_graphql_object_type( TYPE_BUNDLE_PRODUCT, [
'description' => 'A product bundle object',
'interfaces' => [ 'Node', 'Product' ],
'fields' =>
[
'bundlePriceMin' => [
'type' => 'String',
'resolve' => function ( $source ) {
return $source->get_bundle_price( 'min' );
}
],
'bundlePriceMax' => [
'type' => 'String',
'resolve' => function ( $source ) {
return $source->get_bundle_price( 'max' );
}
],
'groupMode'=> [
'type' => 'String',
'resolve' => function ( $source ) {
return $source->get_group_mode();
}
],
'price' => [
'type' => 'String',
'resolve' => function ( $source ) {
return $source->get_price();
},
],
'salePrice' => [
'type' => 'String',
'resolve' => function ( $source ) {
return $source->get_sale_price();
},
],
'regularPrice' => [
'type' => 'String',
'resolve' => function ( $source ) {
return $source->get_regular_price();
},
],
'bundledItems' => [
'type' => 'String',
'resolve' => function ( $source ) {
return 'tbd. still need to solve this.';
},
],
// still need to add more fields...
],
]
);
/**
* Register root query for bundle
*
* @todo - still need to provide args to pass in filter values such as ID
*/
register_graphql_field(
'RootQuery',
'bundleProduct',
[
'type' => TYPE_BUNDLE_PRODUCT,
]
);
} );
/**
* Add the `bundle` type to GraphQL product types
*/
add_filter( 'graphql_woocommerce_product_types', function ( $product_types ) {
$product_types['bundle'] = TYPE_BUNDLE_PRODUCT;
return $product_types;
} );
/**
* Add the bundle to the product input->where to allow for filtering by bundles
*/
add_filter( 'graphql_product_types_enum_values', function ( $values ) {
$values['BUNDLE'] = [
'value' => 'bundle',
'description' => 'A bundle product',
];
return $values;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.