• Resolved wowiwe

    (@wowiwe)


    We have attributes such as BROCHURE, SPECIFICATIONS, YOUTUBE, VIMEO and they all have a value which is displayed like you see on this page.

    What we need to achieve is turning this values into buttons really and so far I have not been able to find a solution that would help with this.

    There was a suggestion on?https://nicolamustone.blog/2016/03/11/make-product-attributes-linkable/ but neither adding the code to functions.php or in a code snippets worked, it rendered a code error 500.

    Is there another solution to this? Thank you.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • This is a low-tech alternative, but how about just adding the links or buttons to the WooCommerce Short Description for the product(s) instead of using attributes? It looks like it will put the data about where it is now with your current theme, require no additional functions.php code, and less work.

    Thread Starter wowiwe

    (@wowiwe)

    Thank you Linux4me2. It is great suggestion however the data was migrated from a Joomla website and we are looking at about 7000 records that would need to be changed.

    That sounds like an opportunity to write a little PHP script to convert the attributes to the HTML you want and save it to the products’ Short Description.

    If it were me, I’d do one manually, then look in the database to get the HTML WooCommerce created to use in the script.

    With the script, you could also remove the attributes as you go.

    Thread Starter wowiwe

    (@wowiwe)

    I love your suggestion minus the bit to save it into the product’s short description. We still would like them as attributes.

    Do you know of php scripts that could be called through a code snippet to add the option of an URL to the attribute to accomplish the task?

    I don’t now of a script to add the URL to the attributes other than the one you linked to above, which I haven’t tested, though when I did a quick search, it looks like there are a number of search results related to doing it.

    It might be worth trying to resolve the error you got when you tried the one above. Was there anything in your server error logs to indicate the cause of the error?

    There’s also a WooCommerce snippet to add archive links, which you might be able to adapt to your needs.

    Hi @wowiwe

    Thanks for reaching out!

    What we need to achieve is turning this values into buttons really and so far I have not been able to find a solution that would help with this.

    I did some research and found these articles could be a good starting point:

    Meanwhile, these forums are meant for general support with the core functionality of WooCommerce itself. What you want to achieve is a bit complex and would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    Hope this helps!

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue.

    Hi,

    I guess the Code in that article is writing to the database.

    A better solution would be to generate the attribut links, when a product page is opened.
    AND
    Only for those attributes that make sense to be clickable.

    With the following code sample and a proper configuration of the search extention Relevanssi or any other search extention, the clickable link will take the clicked term and redirect to the search page with the products listed that share the same search term.

    Use this code in a child-theme functions.php OR the plugin Code Snipped.
    I recommend Code Snipped.

    /**
    * Make specific custom attributes clickable links and ignore non-alphabetic words in German.
    */
    function make_specific_attributes_links( $attribute_html, $attribute, $values ) {
    $specific_attributes = array(
    ‘Kultivar-Model’,
    ‘Japanisches Wort’
    );
    
    // Check if it’s a custom attribute (not a global attribute) and is in the specific attributes list
    if ( ! $attribute[‘is_taxonomy’] && in_array( $attribute[‘name’], $specific_attributes ) ) {
    $attribute_values = array();
    
    foreach ( $values as $value ) {
    // Check if the value contains only alphabetic characters (German)
    if ( preg_match( ‘/^[a-zA-Z??ü??ü?]+$/’, $value ) ) {
    // Create the search URL
    $search_url = home_url( ‘/?s=’ . urlencode( $value ) . ‘&post_type=product’ );
    
    // Generate the clickable link
    $attribute_link = ‘‘ . esc_html( $value ) . ‘‘;
    
    // Add the clickable link to the array of attribute values
    $attribute_values[] = $attribute_link;
    } else {
    // Non-alphabetic value, add it as plain text
    $attribute_values[] = esc_html( $value );
    }
    }
    
    // Implode the attribute values with the original separators
    $attribute_html = ” . implode( ‘, ‘, $attribute_values ) . ”;
    }
    
    return $attribute_html;
    }
    add_filter( ‘woocommerce_attribute’, ‘make_specific_attributes_links’, 10, 3 );

    I’ve had to adjust the code to make it more flexible.

    /**
     * Make specific custom attributes clickable links and ignore non-alphabetic words in German.
     */
    function make_specific_attributes_links( $attribute_html, $attribute, $values ) {
        $specific_attributes = array(
            'Kultivar-Model',
            'Japanisches Wort',
            'Pflege Hinweise',
            'Mat. Eigenschaften',
            'USE YOUR OWN ATTRIBUTES'
        );
    
        // Check if it's a custom attribute (not a global attribute) and is in the specific attributes list
        if ( ! $attribute['is_taxonomy'] && in_array( $attribute['name'], $specific_attributes ) ) {
            $attribute_values = array();
    
            foreach ( $values as $value ) {
                // Check if the value contains only alphabetic characters (German)
                if ( preg_match( '/^[a-zA-Z??ü??ü?\s]+$/', $value ) ) {
                    // Create the search URL
                    $search_url = home_url( '/?s=' . urlencode( $value ) . '&post_type=product' );
    
                    // Generate the clickable link
                    $attribute_link = '<a href="' . esc_url( $search_url ) . '">' . esc_html( $value ) . '</a>';
    
                    // Add the clickable link to the array of attribute values
                    $attribute_values[] = $attribute_link;
                } else {
                    // Non-alphabetic value, add it as plain text
                    $attribute_values[] = esc_html( $value );
                }
            }
    
            // Implode the attribute values with the original separators
            $attribute_html = '<p>' . implode( ', ', $attribute_values ) . '</p>';
        }
    
        return $attribute_html;
    }
    add_filter( 'woocommerce_attribute', 'make_specific_attributes_links', 10, 3 );
    
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Woocommerce product attributes to be able to use as links/buttons’ is closed to new replies.