• Resolved Horizont

    (@horizont)


    This is not a proper “issue”. This is more an enquiry about functionalities.
    I have a product with 2 variations that i choose via a drop down menu.
    When i add those variations on the backend i specify a name (eg. Bike 1 / Bike 2) in a “sanitized” way.
    Using the get_product_variations and or get_variation_attributes functions i am able to extract the name of the variation only in a slug form (eg. bike-1 / bike-2).
    Where exactly is the name i put in manually stored so that i can pick it as it is?

    PS: For now i am using a workaround which is working perfectly but it’s non-ideal to say the least.
    Basically i am adding the same name on the description field of the variation and then extracting it from the variable_attribute key of the array resulting from get_poroduct_variations…and properly sanitizing it removing the <p> that is added automatically.

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter Horizont

    (@horizont)

    Thanks for the heads-up RK, i am already trying on slack woocommerce community as well.
    I’ll keep up to date on this one as well to see if someone is able to help me.

    Hello @horizont ,

    While you are getting a reply from the Slack WooCommerce Community, I would like to share my 2 cents on the topic which I found interesting.

    Here is my approach to getting all the possible variations name under a product –

    global $product;
    
    $variations = $product->get_available_variations();
    $variations_ids = wp_list_pluck( $variations, 'variation_id' );
    
    foreach ($variations_ids as $variations_id) {
    	$variation = wc_get_product($variations_id);
    	echo __("<li>" . $variation->get_name() ."</li>");
    
    }

    I hope this will help you to get more ideas.

    Thank you ??

    Thread Starter Horizont

    (@horizont)

    Hey @rur165.
    Thanks for your contribution. As i am flooded with other requests i’ll try it as soon as i can and get back at you ??
    Thanks again for your help, much appreciated ??

    Thread Starter Horizont

    (@horizont)

    If I am not mistaken you get all the variations ids specifically for a product because with those you can go back to the product details themselves and pick the “display” name from there.
    Which is fine.
    I actually needed to include only the

    $variation = wc_get_product($variations_id);
    	echo __("<li>" . $variation->get_name() ."</li>");

    part because i obviously already had a foreach cycle and var ids in place to build my workaround with them anyways.
    Problem is, the name is the full name, which is “Product name – Variation name” so i will have to strip the product name anyways if I only want the Variation Name.
    Anyways, it certainly is better than my workaround because it relies on me adding the Variation name also in the Description field which isn’t ideal or even conceptually right.
    Am i correct with this?

    Hello @horizont ,

    The code I have shared is for suggestion only. It does not matter how you get access to the variation ID as long as you get it right.

    Apart from the above method, I could not get a direct way to get variations. Also, I am not fully sure about the term variation name because those are actually attributed under each variation which you can get following this example – https://stackoverflow.com/a/42998914.

    I hope you will be able to modify the use case as per your need.

    Thank you ??

    Plugin Support abwaita a11n

    (@abwaita)

    Hi @horizont,

    This thread has been inactive for a while, so I’ll be marking it as resolved. Hopefully, the info shared above was helpful!

    If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get sanitized version of variation product name rather than slug’ is closed to new replies.