Compare page add remove button and attributes ordering

This topic has 8 replies, 3 voices, and was last updated 1 months ago ago by Andrew Mitchell

  • Avatar: webshockstudio
    wssrob
    Participant
    February 12, 2025 at 04:54

    Dear support, I have 2 more question:
    1) is it possible to add a button to the compare page, to add and remove products?
    In the compare page it’s not possible to add or remove products directly, at the moment, so a customer should go back to the catalog, add or remove a product from the compare list, and then go back to the compare page to see the changes….

    2) is it possible to order each woocommerce attribute in the compare page? In the theme options I saw there is the ordering of price, image, excerpt, sku, and attributes, but I’d like to order EACH SINGLE woocommerce attribute inside the table.

    Thanks, kind regards!

    7 Answers
    Avatar: Jack Richardson
    Jack Richardson
    Support staff
    February 12, 2025 at 16:18

    Dear @wssrob,

    Thank you for reaching out to us.

    Unfortunately, we are unable to check the comparison feature on your website, as the necessary buttons do not appear neither on the product pages nor on your store in products content. However, by default, the comparison table includes such items, as shown in the following screenshot: https://prnt.sc/gwDMQSK9LSOz. If these items are missing on your site, we recommend reviewing the settings in your Theme Options under the Compare section.

    Regarding the sorting of attributes, this functionality is not available by default, as it is a rather specific customization. However, if you wish, you may refer to the following snippet for sorting product attributes (snippet is just as an example and you should write your own keys and values). Please note that our support does not cover additional customizations. If you require further assistance, you may submit a request https://www.8theme.com/account/#etheme_customization_panel or implement the necessary changes manually.

    Snippet to be added in “xstore-child/functions.php”:

    add_filter('woocommerce_display_product_attributes', function($attributes, $product) {
    	$custom_ordered_attributes = array();
    	if ( isset($attributes['attribute_custom02']) ) 
    		$custom_ordered_attributes['attribute_custom02'] = $attributes['attribute_custom02'];
    	if ( isset($attributes['attribute_custom']) ) 
    		$custom_ordered_attributes['attribute_custom'] = $attributes['attribute_custom'];
    	return $custom_ordered_attributes;
    }, 10, 2);

    Before: https://prnt.sc/5HL1J6YxxgOw
    After: https://prnt.sc/VP8k4aDL5Kw1

    Product settings: https://prnt.sc/k18Lbfgm8R6X

    Important Note: This sorting method will apply globally across your website. Please ensure that all necessary attributes are included to avoid any unintended omissions.

    Should you have any further questions, please do not hesitate to contact us.

    Best regards,
    Jack Richardson
    8Theme Team

    Avatar: webshockstudio
    wssrob
    Participant
    February 12, 2025 at 17:35

    Dear Jack, thanks for your reply. The compare table and the compare buttons below the product are not active in the live / production website that you saw.
    I have a local staging environment, but I don’t see the “action” row, and can’t ADD or DELETE product directly from the compare page – table (I’m using the off-canvas compare and a specific page for comparison, don’t know if that’s relevant).
    This should be a working feature so I hope further assistance is possible.

    Regarding the attributes ordering, I can’t replicate your code and make it work in my environment. Do I have to install the jith compare plugin for the compare feature to work?

    I tried your code, and also a more generic

    add_filter('woocommerce_display_product_attributes', function ($attributes) {
        $custom_order = [
            'myattribute1' => 1,
            'myattribute2' => 2,
            'myattribute3' => 3,
        ];
    
        uasort($attributes, function ($a, $b) use ($custom_order) {
            $a_key = isset($custom_order[$a['slug']]) ? $custom_order[$a['slug']] : PHP_INT_MAX;
            $b_key = isset($custom_order[$b['slug']]) ? $custom_order[$b['slug']] : PHP_INT_MAX;
          
            return $a_key <=> $b_key;
        });
    
        return $attributes;
    }, 10, 1);

    But neither work. Your code make all the attributes disappear from the table, my code doesn’t change the order. I understand this second request is much more advanced customization, so If you cannot provide further assistance I will eventually get in touch and ask for a quote.

    Avatar: webshockstudio
    wssrob
    Participant
    February 13, 2025 at 11:00

    Dear Jack, customization of the order is solved, I edited the compare.php in plugins/et-core-plugin/packages/xstore-compare/templates , it wasn’t super easy as there are 2 array and intersecating stuff, compare_fields and product_table_info.

    I put there the code to add the remove buttons to the table, and it worked after a bit of fiddling with the code and syntax required.

    I solved on my own the problems, but thanks for the hints

    Avatar: Jack Richardson
    Jack Richardson
    Support staff
    February 13, 2025 at 16:12

    Dear @wssrob,

    We would like to kindly remind you that any changes made directly to the project files will be lost with each new update of plugin. For this reason, we strongly recommend implementing such modifications in the child-theme/functions.php file, as this ensures that your changes are preserved.

    However, if you choose to modify the files of the XStore Core plugin directly, we advise you to save a copy of the modified file on your local PC. After updating to the latest version, you can then reapply your changes to the updated file as needed.

    Should you require any further assistance, please do not hesitate to reach out.

    Best regards,
    Jack Richardson
    8Theme Team

    Avatar: webshockstudio
    wssrob
    Participant
    February 13, 2025 at 17:42

    Thanks Jack for the reminder, usually I put my code in functions.php of my child theme, but the file compare.php in plugins/et-core-plugin/packages/xstore-compare/templates does contain a lot of stuff, and I’m not sure on how to bring my mods to that file in functions.php, so I guess I’ll have to live with that, unless you have some suggestions for me to do an update proof version of these mods.

    To add the remove button I used this:

                    echo '<tr class="xstore-compare-row-remove">';
                    echo '<td class="xstore-compare-remove-label">' . esc_html__('Remove from table', 'xstore-core') . '</td>';
    
                    // Add remove button for each product
                    foreach ($product_table_info as $product_info) {
                        echo '<td class="xstore-compare-remove-button" data-product_id="' . esc_attr($product_info['id']) . '">';
                        if (!strstr($product_info['id'], 'ghost-product')) {
                            $instance->print_button($product_info['id'], array(
                                'only_icon' => true,
                                'has_tooltip' => false,
                                'custom_icon' => '<button class="remove-compare-product" style="background: none; border: none; color: red; cursor: pointer; font-size: 32px !important; margin-top:-15px !important; font-weight: bold;">&times;</button>'
                            ));
                        }
                        echo '</td>';
                    }
                    echo '</tr>';
    

    And to order the attributes I used this:

    case 'attributes': 
        $product_attributes = $product_parent ? $instance->get_product_attributes($product_parent) : $instance->get_product_attributes($product);
        
        // Define the order
        $custom_order = array(
            'Attribute1' => 1,
            'Attribute2' => 2,
            'Attribute3' => 3 // you get the idea
        );
    
        // Temp array 
        $ordered_attributes = array();
        foreach ($custom_order as $attr_name => $order) {
            $ordered_attributes[$order] = $attr_name;
        }
        ksort($ordered_attributes);
    
        // Adding attributes value to product_table_info e preparing compare fields
        foreach ($ordered_attributes as $order => $attr_name) {
            $label_parsed = str_replace(' ', '-', $attr_name);
            $attribute_key = 'attribute_' . $label_parsed;
            
            if (!in_array($attribute_key, $compare_fields)) {
                $compare_fields[] = $attribute_key;
                $fields_strings[$attribute_key] = $attr_name;
            }
    
            foreach ($product_attributes as $product_attribute) {
                if ($product_attribute['label'] === $attr_name) {
                    $product_table_info[$products_count][$attribute_key] = $product_attribute['value'];
                    break;
                }
            }
        }
    
        // Add other attributes
        foreach ($product_attributes as $product_attribute) {
            if (!isset($custom_order[$product_attribute['label']])) {
                $label_parsed = str_replace(' ', '-', $product_attribute['label']);
                $attribute_key = 'attribute_' . $label_parsed;
                
                if (!in_array($attribute_key, $compare_fields)) {
                    $compare_fields[] = $attribute_key;
                    $fields_strings[$attribute_key] = $product_attribute['label'];
                }
                
                $product_table_info[$products_count][$attribute_key] = $product_attribute['value'];
            }
        }
        break;

    Thanks!!

    Avatar: Jack Richardson
    Jack Richardson
    Support staff
    February 14, 2025 at 15:31

    Dear @wssrob,

    Unfortunately, there are no alternative methods for implementing your improvements. Therefore, we kindly ask you to monitor updates and apply the necessary changes manually.

    If this topic has been resolved, please mark it as solved.

    We appreciate your understanding and respect for our support time.

    Best regards,
    Jack Richardson
    8Theme Team

    Avatar: Andrew Mitchell
    Andrew Mitchell
    Support staff
    February 14, 2025 at 21:38

    Dear wssrob,

    In the spirit of gratitude, we want to express our appreciation for your trust in our products. As a valued customer, your experience matters greatly. Would you consider sharing it by giving our theme a deserving 5-star rating on ThemeForest?

    Click here to share your thoughts: https://themeforest.net/downloads

    Being part of our community means a lot, and your feedback contributes immensely.

    Best Regards,
    The 8Theme Team

  • Viewing 8 results - 1 through 8 (of 8 total)

The issue related to '‘compare page add remove button and attributes ordering’' has been successfully resolved, and the topic is now closed for further responses

Helpful Topics

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.