I'm using the WooCommerce REST API to e.g. get all products with variations, but I encounter a fairly large problem regarding the number of fired requests. I need help optimizing the situation below.
Situation: A webshop with 50 products and 5 variations for each product.
Total count of request = 51
How can I do this without firing of 51 requests? Is't possible to get all products with their variations eager loaded somehow?
Get WooCommerce variations programmatically In order to get all variations, we use the $product->get_available_variations() method. This code gets all the possible WooCommerce product variations for the current $product.
You just need to click on 'Process variations' button by navigating to WooCommerce > Settings > Variations as Products.
Variable products in WooCommerce let you offer a set of variations on a product, with control over prices, stock, image and more for each variation. They can be used for a product like a shirt, where you can offer a large, medium and small and in different colors.
Yes, you can do it by customizing the WooCommerce Product REST API Response.
Here I have attached some code that will help you.
add_filter('woocommerce_rest_prepare_product_object', 'custom_change_product_response', 20, 3);
add_filter('woocommerce_rest_prepare_product_variation_object', 'custom_change_product_response', 20, 3);
function custom_change_product_response($response, $object, $request) {
$variations = $response->data['variations'];
$variations_res = array();
$variations_array = array();
if (!empty($variations) && is_array($variations)) {
foreach ($variations as $variation) {
$variation_id = $variation;
$variation = new WC_Product_Variation($variation_id);
$variations_res['id'] = $variation_id;
$variations_res['on_sale'] = $variation->is_on_sale();
$variations_res['regular_price'] = (float)$variation->get_regular_price();
$variations_res['sale_price'] = (float)$variation->get_sale_price();
$variations_res['sku'] = $variation->get_sku();
$variations_res['quantity'] = $variation->get_stock_quantity();
if ($variations_res['quantity'] == null) {
$variations_res['quantity'] = '';
}
$variations_res['stock'] = $variation->get_stock_quantity();
$attributes = array();
// variation attributes
foreach ( $variation->get_variation_attributes() as $attribute_name => $attribute ) {
// taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
$attributes[] = array(
'name' => wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $variation ),
'slug' => str_replace( 'attribute_', '', wc_attribute_taxonomy_slug( $attribute_name ) ),
'option' => $attribute,
);
}
$variations_res['attributes'] = $attributes;
$variations_array[] = $variations_res;
}
}
$response->data['product_variations'] = $variations_array;
return $response;
}
I have done by this way. I have got all variations in the single parameter product_variations
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With