co-cart / co-cart

🛒 CoCart is a free REST API designed to decouple your slow WooCommerce storefront and convert it blazing-fast, build in any web framework, and save you countless hours.
https://cocartapi.com
Other
374 stars 64 forks source link

Get Variation ID from matching attributes if Variation ID is missing #114

Closed waseem99 closed 4 years ago

waseem99 commented 4 years ago

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

seb86 commented 4 years ago

Can you explain more? Im currently doing variable product validation which another issue is already open for.

waseem99 commented 4 years ago

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.

seb86 commented 4 years ago

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.

seb86 commented 4 years ago

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?

waseem99 commented 4 years ago

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.

seb86 commented 4 years ago

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.

waseem99 commented 4 years ago

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 ?

seb86 commented 4 years ago

Can you share a little bit of code in a gist snippet?

waseem99 commented 4 years ago

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 :/

seb86 commented 4 years ago

I will see what I can do.

seb86 commented 4 years ago

Turns out WooCommerce already has the answer were are looking for.

https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-ajax.php#L469

waseem99 commented 4 years ago

That's great

seb86 commented 4 years ago

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
        );
    }