WooCommerce – Pricing a highly customizable variable product with an API from functions.php

I have a client who needs to sell a very custom product. The product has 4 attributes with 6, 24, 11 and 11 options. That adds up to over 17000 unique combinations. These can’t be entered into variations because (1) it’s too much manual work, (2) it unnecessarily fills the WP database, and (3) the pricing for them changes.

The pricing comes from a complex spreadsheet that needs to be adjusted a few times a year. Because spreadsheets have no connectivity on the web I have converted it into an API, all this API needs is the product id and the choice for each of the attributes, and it’ll return the price.

Read More

I noticed that when I enter the attributes and their options into the attributes fields, along with 1 mandatory any-any-any-any variation, the dropdowns appear on the frontend with all options and the visitor can even add the product to the cart. Yay!

The last part I can’t figure out however. On the cart page I need to manipulate the price based on the chosen attributes. This code should work I thought:

function prefix_get_api_price( $price, $product )
{
    // if we have a product that uses the price api
    if ( $product->is_type( 'variation' ) && get_field( 'has_api_price', $product->post->ID ) )
    {
        // get attributes
        $variation_attributes = $product->get_variation_attributes();
        // call the api, pass product id and chosen configuration
        $api_results = price_api( $product->id, $variation_attributes );
        // set the new/real/api price
        $price = $api_results['price'];
    }
    // return
    return $price;
}
add_filter( 'woocommerce_get_price', 'prefix_get_api_price', 10, 2 );

But get_variation_attributes() only gets the variations that I set for the product (which is none, the any-any-any-any doesn’t even come up), instead of the configuration choices the user made.

How do I get the choices in here?

Or how would you achieve the same result differently?


Other things I’ve tried:

cart-item-data.php

The attribute choices are shown on the cart page using the template cart-item-data.php. I hijacked this file to use its variables to modify the price here. This works! However it doesn’t seem to set the price so other parts of Woo run with it. The subtotal and total for the cart remained 2 USD for the 2 products. (The ‘system’ price was 1 USD.) Because this is also a very hacky method I didn’t explore this any further.

WC()->cart->get_cart()

This functions returns everything in the cart, along with the product data it also has the selected product attributes. Awesome. When trying to integrate this with the above code I unfortunately noticed that there’s is no solid way to match the products in get_cart() with the data I get in the woocommerce_get_price hooked function. Let me explain with an example: I can add Custom Product Alpha into my cart 3 times, configuration AAAA AABB and BBAA. Now the hook starts passing custom products to me. I guess I could say the first I get is AAAA, the 2nd is AABB and the third is BBAA. And I did that, but as I already noticed when debugging, the woocommerce_get_price hook gets triggered with the custom product a lot more than just once for every product, I don’t know why but it does. So sure I set the custom price for the first 3 times but then it got overwritten again with the system default. I could possibly loop this.. Nope none of this worked because inside the hook I don’t get all of the cart probably because the hook is first called halfway into the cart page. I can’t even properly explain what I tried with this one. Even if it worked it would’ve been a dirty hack as well.

Gravity Forms Product Add-ons (link)

This was my initial plan until I found the spreadsheet’s complexity goes way beyond any math this plugin will let me do.

Related posts

Leave a Reply