I’m writing a plugin and cannot get the correct variation title
In Woocommerce the variation i use is called “unfolded laminated”. but when i try to get the title of a variation i get: “Variation #781 of Chart”
This is the code is use:
foreach ($order->get_items() as $item) {
$product_variation_id = $item['variation_id'];
if ($product_variation_id)
{
$product = new WC_Product($product_variation_id);
$productname = get_item_variations($product_variation_id);
}
else
{
$product = new WC_Product($item['product_id']);
$productname = get_the_title($item['product_id']);
}
}
How do i get the correct title?
I was actually looking this up trying to figure it out and here’s my solution because the variation title was returning a slug.
Tested on WC 2.4.13
I stumbled across this just now looking for a ‘quick’ fix. Since no-one has posted an answer I thought I would.
I just used WordPress
get_post_meta
to fetchattribute_options
. This is where all the option titles are stored (variation titles are product options).Will return an array
Here is a solution for this problem which is tested with WooCommerce 3.2.6
In this function to return the title of the input variation, you can change the delimiter ‘, ‘ to a space or whatever is needed.