Custom Attributes Breaking

I have come a long way in creating my custom product page, but I’m running into a problem that I can’t figure out for the life of me.
I have created a PHP filter and placed it in my functions.php file. The filter is working correctly and returning the proper information. Specifically to my issue… I have the filter returning the ‘original’ and ‘current’ price for my variable products. They are visible on the screen within my text element where I have placed the looper consumer for my custom filter looper provider in the parent DIV.

In short… the filter works. I can see the data on my page. But when I place that same looper string as a custom attribute for my variation images, the data doesn’t come through at all.

I have 6 custom attributes on my variation images. These attributes perform various functions, and 4 of them work just fine. But the moment I add these pricing filters to them, they break all of the other attributes.

Is there a limit to how many custom attributes the theme will allow for a single element?

Hey @fletcher_austin,

Thanks for writing in! How are you displaying the filter? Is it a shortcode or dynamic content? We wold love to check your site if we can log in. You can create a secure note in your next reply with the following info:
– Link to your site
– WP login URL
– WP username
– WP password
– WP Administrator Role
– Confirmation that we can access and make changes to your site

To know how to create a secure note, please check this out: How The Forum Works

image

Best Regards.

I’m using the dynamic content filter I created. I’m using the same filter with a different key in the same place, and it works fine. I will create 2 versions of the product page for you to look at. They will be identical except for the two added attributes on the variation image. You’ll be able to see that the filter is working correctly because the data will be displayed below the variations just fine… but trying to connect that data to the variation image is not working.

Looking forward to your insights.

When you log in to see the different pages, you will have to adjust the conditionality of those templates to display properly. The product I’m using to test everything is the “Slim Single Torch Lighter”.

For some reason the conditionality to show this build on that specific product isn’t working. I have to show it on ALL products for the frontend display to be used. If you know why assigning a specific product isn’t working, I’d love to know that as well.

And to be clear… the only difference between these two pages is that on the variation image I have 2 additional custom attributes that don’t work… and actually ‘break’ the other ones at the same time.

Hello @fletcher_austin,

Are you referring to these two functions?

add_filter('cs_looper_custom_variations', function($result, $params) {
    $product_id = $params['product_id'];
    $product = wc_get_product($product_id);

    if (!$product) {
        return [];
    }

    if ('simple' === $product->get_type()) {
        return [get_simple_product_data($product)]; // Return as an array of one to keep consistent with variations structure
    }

    // Handle variable products
    if ('variable' === $product->get_type()) {
        return get_variable_product_data($product);
    }

    // Return empty array if product type is neither simple nor variable
    return [];
}, 10, 2);
function get_variable_product_data($product) {
    $variations_data = [];
   

foreach ($product->get_children() as $child_id) {
        $variation = wc_get_product($child_id);
        if (!$variation || !$variation->is_in_stock() || '' === $variation->get_price()) {
            continue;
        }

        // Fetching variation attributes
        $variation_attributes = $variation->get_attributes();
        $attributes_data = [];
        $variation_attribute_names = [];
        $variation_values = [];

        foreach ($variation_attributes as $attribute => $value) {
            $taxonomy = str_replace('attribute_pa_', '', $attribute);
            $attribute_label = wc_attribute_label($taxonomy);
            $value_name = '';

            if (taxonomy_exists($taxonomy)) {
                $term = get_term_by('slug', $value, $taxonomy);
                $value_name = $term ? $term->name : $value;
            } else {
                $value_name = apply_filters('woocommerce_variation_option_name', $value);
            }

            $variation_attribute_names[$taxonomy] = $attribute_label;
            $variation_values[$taxonomy] = $value_name;
        }

        // Fetching prices without HTML formatting
        $original_price = $variation->get_regular_price();
        $current_price = $variation->get_sale_price() ? $variation->get_sale_price() : $variation->get_price();

        $variations_data[] = [
            'id'                      => $variation->get_id(),
            'sku'                     => $variation->get_sku(),
            'name'                    => $variation->get_name(),
            'image'                   => wp_get_attachment_url($variation->get_image_id()),
            'add_to_cart_url'         => htmlspecialchars_decode($variation->add_to_cart_url()),
            'stock_quantity'          => $variation->get_stock_quantity(),
            'original_var_price'      => wc_price($original_price),
            'current_var_price'       => wc_price($current_price),
            'attribute_names'         => $variation_attribute_names,
            'variation_values'        => $variation_values,
        ];
    }
    return $variations_data;
}

Be advised that custom PHP coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

Thanks.

I ended up just bypassing the theme functionality and going with a straight forward JS solution. The issue never really got resolved, and I’m not sure why those two custom attributes were breaking everything else, but I found a different solution.

Thanks for your efforts. Sorry I didn’t get back to this thread before you checked it.

No worries, @fletcher_austin.
Cheers.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.