Last active
April 7, 2021 11:27
-
-
Save jacobarriola/5d055ba102c001f0c556427a1ae78cd2 to your computer and use it in GitHub Desktop.
Add WooCommerce Product Bundles to GraphQL API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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