Closed waseem99 closed 4 years ago
Can you explain more? Im currently doing variable product validation which another issue is already open for.
I have a variable product with 6565 variations. So as per the documentation I have to add variation id, variation attributes as well. To achieve this, I have to hardcore 6565 ids. So if the API can add variable product to cart with variation attributes only that will be amazing or if there is a function to get variation id from variation attributes.
Well i just added a condition that a variation id must be set in order to add the variable product to the cart based on another users feedback. I was going to try and set the attributes based on the variation ID if the attributes are not set but you want it the otherway around.
I guess this needs brainstorming so more before i commit anything final in my PR.
Why do you have to hardcode the IDs and not attributes? Which API are you using to fetch products, WC REST API or CoCarts Products add-on?
I am using this way to add variation to cart: Variation products require two things in order to add to the cart correctly. First is the variation_id. The second is the attributes of that variation. You need to pass both parameters in order for a valid variable product to be added to the cart. Without the attributes of the variation, there is no way to identify the variation added.
I'm well aware of what is needed to be able to add the variation to the cart, I'm just a little lost as to why you can get the attributes but not the ID for the variations.
https://ginyaki.com/menu/ so I get attributes when people choose option, now with variations id, how shall I add if I have 6565 variations ?
Can you share a little bit of code in a gist snippet?
I don't believe that will help So a more generic request, if to add variable products to cart one has only pass variation attributes as parameter that will be amazing and I am failing to illustrate it :/
I will see what I can do.
Turns out WooCommerce already has the answer were are looking for.
https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-ajax.php#L469
That's great
Code Note 🔢
/**
* Find matching product variation
*
* @param $product_id
* @param $attributes
* @return int
*/
function find_matching_product_variation_id($product_id, $attributes)
{
return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
new \WC_Product($product_id),
$attributes
);
}
If you have a variable product with huge number of variations, it is very difficult to hardcode all the ids specially if they ain't in a specific order