Scenario
- WP Ajax is not used at the Add to cart event, so, the single product page reloads completely once the user adds something to the cart, it is not my installation so I cannot change freely that WooCommerce setting.
- There is a certain message that is displayed once the user add to cart an item, as a WooCommerce notice at the single product layout.
- This message must not change if the product allows backorders, it must change if it is "In Stock", a single string must be added.
- The store have simple and variable products so the feature must work properly on any case.
I have to mention what I have been doing in order to achieve that, and give a little background on the functionality.
First Approach
A custom message was added directly through 'wc_add_to_cart_message_html'. This was implemented due to a previous request to customize the aforementioned message without further requirements.
Second Approach (current, because it is still on development)
The previously mentioned message changes if it is found that the product doesn't allows backorders, the change is made through jQuery at 'woocommerce_single_product_summary'.
A product object is created and it is asked if the product allows backorders through the following method:
$product->backorders_allowed()
That would do just fine if it wasn't that there are variable products too.
What I mean is that despite a variable product can be configured to allow backorders, I mean, the parent product of all the possible variations, the inventory management relies right now on the variations, every variation have its own "In Stock" stock status setting, and, one among many could have "On backorder" stock status setting.
Third approach (hypothetical)
In order to get the current variation ID I can get it here on the DOM:
$('input.variation_id').val();
Why?, because I want to use that value to call the WooCommerce product variation constructor and check yet again but now at a variation level if the product is able to be ordered on backorder, like this:
$p_variation = new WC_Product_Variation( $var_id );
$p_variation->backorders_allowed()
But I have a problem, I don't know how to get this DOM value (the current variation that was just added to the cart) and send it to the function that would contain the latest mentioned constructor and method.
In other cases I have been able to send DOM data to the server (to be processed by PHP) by invoking a defined function as an action through WP Ajax, alongside with data and instructions to take into account if the action was successfully executed, on the change event of the variation select html object on the DOM, but with the reload of the whole page I find myself lost.
Because is quite different to invoke something through a select changing than on a submit of the add to cart form.
I don't know if it is possible to call the backorders_allowed method at all, I assume that a product variation have the same properties that a parent product.
I lack of ideas right now and I would appreciate if someone could help me a little bit with this.
tshirt
, and I addtshirt - red
to my cart, a string needs to be added to the "Added to cart" string that shows up in the woocommerce message area at the top only if the red tshirt variations allows backorders? – Frits