• Resolved abonawaff53

    (@snipermilan)


    Hi,

    I have three attributes, and I would like to conditionally hide one or two from the front end depending on site lang.

    Attributes are, size(cm), color, size(in).

    A single product page will either display size(cm) and color, or size(in) alone.

    In the back-end, here is a short version of what my variations look like:

    Size(cm) Color Size(in)

    1- [50] [red] [any]
    2- [any] [any] [20]

    I use this function to unset the attribute that I don’t need

    if ( sitelang === 'fr' ){
    add_filter( 'woocommerce_product_get_attributes', 'my_attribute_hider' );
      function my_attribute_hider ( $attributes ) {
        if ( isset( $attributes["pa_size-in"] ) ){
          unset( $attributes["pa_size-in"] );
        }
    }
    if ( sitelang === 'en' ){
    add_filter( 'woocommerce_product_get_attributes', 'my_attribute_hider' );
      function my_attribute_hider ( $attributes ) {
        if ( isset( $attributes["pa_color"] ) ){
          unset( $attributes["pa_color"] );
        }
        if ( isset( $attributes["pa_size-cm"] ) ){
            unset( $attributes["pa_size-cm"] );
        }
    }

    Price of variation #1 will always override price of #2 .. so on single product pages where only the attribute Size(in) is available, price will be wrong .. If I re-arrange the variations order now price of pages that have the attributes Size(cm) and Color will be wrong.

    Any idea how one would achieve this ?

    I tried this filter with no luck:

    add_filter('woocommerce_available_variation', 'remove_variation');
    function remove_variation( $variation ){
      if ( !empty($variation['attributes']['attribute_pa_color']) ){
      return $variation;
      }
      else{
        return '';
      }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey @snipermilan,

    I have three attributes, and I would like to conditionally hide one or two from the front end depending on site lang.

    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.

    Kind regards,

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    This thread has been inactive for a bit, so I’m going to mark it as resolved now. Hopefully, the above information was helpful to you!

    Please feel free to open a new thread if you have any further questions.

    Cheers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditionally remove variations from frontend’ is closed to new replies.