Skip Navigation

[Resolved] If then statement product attributes on custom datasheet page

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 19 replies, has 2 voices.

Last updated by jelle-jacobd 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1924615

Hi Jamal,

You have helped me setup the script discussed in this ticket. https://toolset.com/forums/topic/toolset-chat-support-ticket-by-jelle-jacobd-1611574006/

I noticed if the attribute for the parent has more then 1 value it isn't shown at all, it does if it has one value.

hidden link

"Materiaal Armatuur" is missing here because in this case it has 2 values.

What should I do?

#1925011

Hello and thank you for contacting the Toolset support.

I run a small test locally and the shortcode returns all the values separated by a comma, for example:

Blue, Green, Red

So what's wrapped inside this conditional will not display, it will display only where there is no value.

[wpv-conditional if="('[show-product-attribute attribute="pa_materiaal-armatuur"]' eq '' )"][wpv-post-taxonomy type="pa_materiaal-armatuur" format="name"][/wpv-conditional]

I am a bit lost, can explain again and add some screenshots. Allow me access to check it further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1926621

Hello Jelle,

I am sorry, but what you are trying to do seems wrong to me. You want to get the attributes from a product variation. I believe that the attributes are tied to the parent product instead of its variations. For example, the following returns the attributes of the parent, notice the item="$parent":

[wpv-post-taxonomy type="product_type" format="slug" item="$parent"]

Then, I don't think that the shortcode we have built together can query attributes from a variation as I am not even sure if that's possible.

Can you explain more what this child view is meant to do?

#1926627

Hi Jamal,

Thanks again. What I'm trying to do is show the values for a specific attribute from the parent product if it is not set by "used for variations". Doe that make sense?

Thanks!

Br, Jelle

#1928103

I think, I understand. In this case, on the "Datasheet child (variable)" view, that displays product variation, we need to set the shortcodes to look for data in the parent product. So, we need to pass the item="$parent" attribute to the taxonomy shortcode in the conditional:

[wpv-conditional if="( '[wpv-post-taxonomy type="pa_materiaal-armatuur" format="slug" item="$parent"]' ne '' )"]

[/wpv-conditional]

Then we need to pass the parent product id to the shortcode like this:

[show-product-attribute attribute="pa_materiaal-armatuur" product_id="[wpv-search-term param="product_id"]"]

The shortcode code has a bug, so I updated to:

add_shortcode( 'show-product-attribute', 'show_product_attribute_func');
function show_product_attribute_func($atts){
    global $product;
     
    $atts = shortcode_atts(
        array(
            'attribute' => '',
            'product_id' => $product->ID,
        ), $atts );
    $attribute = $atts['attribute'];
    $product_id = $atts['product_id'];
	
	if ( !empty($attribute) ) {
		$my_product = wc_get_product( $product_id );
        if ( !empty( $my_product ) ) {
			$value = $my_product->get_attribute( $attribute );
            return $value;
        }
    }
     
    return "";
}

It seems to give the expected result for all the referred products. Check this screenshot hidden link

This being said, I don't see why would we need to use the show-product-attribute in a conditional display. Let's just display it directly.

[wpv-conditional if="('[show-product-attribute attribute="pa_materiaal-armatuur" product_id="[wpv-search-term param="product_id"]"]' eq '' )"][wpv-post-taxonomy type="pa_materiaal-armatuur" format="name"][/wpv-conditional]
#1928109

Hi Jamal,

Thanks a lot and thanks for explaining. Yes it works, really nice!

I'm using the conditional statements in case the attribute value for the variations is the same as the parent, so no double values are shown, does that make sense? Or is the shortcode taking care of that itself?

Br, Jelle

#1928181

The conditional was checking against an empty value. And now, the shortcode can take a product_id in the argument, we directly pass the parent product_id to it.

I am a bit confused because the shortcode is not used to get any information from the variation. It is designed to get the attribute of the variable product(the parent product). Am I missing something? If yes, please provide concrete examples of what you would expect in the referred products. That would be extremely helpful. For example:
- In this link(xxx) I expect to have (yyy) because (zzz).
- In this link(xxx2) I expect to have (yyy2) because (zzz2).

#1929387

Hi Jamal,

Of course:

No value set for variations:
Link to variable product with NO value for the attribute "Materiaal Armatuur" set for variations. It is only active for the parent product. Please check the screenshot here: hidden link.

Product page (no selection/dropdown available for "Materiaal Armatuur") which is correct.
hidden link

Datasheet specific variation, which is showing the attribute "Materiaal Armatuur" but with the value from the parent product. Because no value set for this specific variation is set like in the screenshot above. The only thing I ran into is that the parent attribute sometimes has multiple values. F.e. Aluminum en Plastic. You solved this by changing the shortcode.
hidden link

Value(s) set for variation:
If an attribute is setup with multiple values and with the option checked "Used for variations" :
hidden link

The datasheet should show the value from the variation itself. So this can change between products and variations because it is possible to select "Aluminium" or Plastic". The datasheet should only show the value set for this specific product, not the value(s) from the parent product. In this case "Aluminium" and Plastic" but one of them.

Does this makes sense?

Br, Jelle

#1930117

Thank you. It does, I think that I understand better. The shortcode code does not handle the case of variations, it only handles the case of the parent variable product. I looked online and I think that the shortcode needs to check if it is a variation, and in that case get the attribute of the variation. There is a function that can be used on the parent product:

$product->get_available_variations();

Source: https://stackoverflow.com/a/46326100

I can't really troubleshoot the code in your live website, so I wonder if you can prepare a Duplicator copy and let me work on it locally.

#1932797

Thanks Jamal. Sounds good. Would be really nice. In that case I don't need al the if then statements:)

I have staging environment available is that oke as well?

#1934825

Actually debugging code is much hard on an online server(staging or product). It is much easier in a local development environment. I would rather prefer to debug it locally than on a live server. I prepared a Duplicator copy on your site, but I need your approval to download it and work on it. Please let me know if that's fine with you.

#1935717

Hi Jamal,

Oke I understand. Of course please go ahead.

Thank you!

Br, Jelle

#1936595

I worked on this a bit and I think that we should use an additional shortcode that will look for an attribute value in the variation instead of the parent product(variable).

However, I searched the products to find a variable product that uses the attribute for variation and I couldn't find any. hidden link

I understand, that for a similar product, you would like to get the value of "Materiaal armatuur" for each variation.
And for another product that does not use this attribute for variation, you want to get the value from the parent product, because variations do not have a value, right?

Please share URLs for products of these different types, I couldn't find any of them. All I could find, when searching, by the attribute, are simple products, not variable products with variations.

#1936675

Hi Jamal,

Correct, that's exactly what I need, thanks. Below an example of a product which should use the value of the parent and vice versa. As it turns out "Materiaal Armatuur" isn't the best test case. In most cases it is the same, sorry. We should use Hoogte (Height) or Breedte (Width) instead. Which is more often different from the parent.

Parent available, but attribute "Hoogte/Height" not set for variations (should use parent value)
hidden link

Parent available, but attribute is set for variations (should use value of variation)
hidden link

Thanks!

#1936767

Thank you! As I said before, I think that it would be better to have another shortcode that returns the attribute value for the variation, and if it does not exists, it will return the attribute value of the parent product. I come up with the following code:

add_shortcode( 'show-variation-attribute', 'show_variation_attribute_func');
function show_variation_attribute_func($atts){
    // global $product;

    $variation_id = isset( $_GET['variation_id'] ) ? $_GET['variation_id'] : "";
     
    $atts = shortcode_atts(
        array(
            'attribute' => '',
            // 'product_id' => $product->ID,
            'variation_id' => $variation_id,
        ), $atts );
    $attribute = $atts['attribute'];
    $variation_id = $atts['variation_id'];

    if ( ! empty( $attribute ) ) {
		$product_variation = new WC_Product_Variation( $variation_id );
        if ( !empty( $product_variation ) ) {
			$value = $product_variation->get_attribute( $attribute );
            if ( ! empty( $value ) ) {
                error_log('returning variation value' . $value);
                return $value;
            }

            $parent_id = $product_variation->get_parent_id();
            $parent = wc_get_product( $parent_id );

            if ( !empty( $parent ) ) {
                $value = $parent->get_attribute( $attribute );
                return $value;
            }
        }
    }

    return '';
}

When using this shortcode, there is no more need to check if the variation has an attribute value, just to display its parent value. The shortcode does it without needing to use the conditional shortcode. So, the code for Breedte in the content template becomes:

 [wpv-conditional if="( '[wpv-post-taxonomy type="pa_breedte" format="slug" item="$parent"]' ne '' )"]
  <tr>
  <th>Breedte (in mm)</th>
  <td>: [show-variation-attribute attribute="breedte" variation_id="[wpv-search-term param="variation_id"]"]</td>
  </tr>
  [/wpv-conditional]

The conditional(line 1-6) will display Breedte of the product, only if the parent post has this attribute. The shortcode(line 4) will display the attribute value of the variation. If it does not exist it will display the parent's attribute variation.
Check this screenshot hidden link