1

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.

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.

6
  • I'm highly intrigued by this question, but it is late and I am travel-weary. And jet-lagged. I think it is probably a pretty complex answer, but you might want to start by taking a look at the add_cart_item_data filter. That should allow you to change the price in the cart. Apr 8, 2015 at 22:35
  • Another question, do the variations exist in the admin at all? Apr 9, 2015 at 23:39
  • @helgatheviking I'll take a look at add_cart_item_data, I suspect it is the same as I found in cart-item-data.php. The variations do not exist in the admin (products > edit product X > variations) because of the mentioned reasons.
    – Brugman
    Apr 10, 2015 at 10:17
  • The add_cart_item_data let's you modify the values of things in the cart. I am using it in my own plugins to change item prices. It is definitely different from a display template. Can you explain what does exist in the admin? When adding a variation to the cart you usually need a variation ID. I'll have to have a think about how to get around that. Apr 10, 2015 at 10:21
  • A colleague figured out we could leverage the woocommerce_before_calculate_totals hook. This receives the cart object so he applied the technique I spoke about for get_cart() and it seems to be working. I'll post the code we used as an answer if it passes all tests. Should it fail I'll def check out add_cart_item_data.
    – Brugman
    Apr 13, 2015 at 14:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.