Home Forums WoodMart support forum Limit number of products for compare

Limit number of products for compare

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #226675

    marmalabcreative
    Participant

    Hello there,

    I would like to limit the compare product to 3. How can be done this in current compare.php? or somewhere else?

    Thanks

    #226689

    Artem Temos
    Keymaster

    Hi,

    Try to add the following PHP code snippet to the child theme functions.php file to do this

    function woodmart_get_compared_products_table() {
    	$products = woodmart_get_compared_products_data();
    	$fields = woodmart_get_compare_fields();
    	$empty_compare_text = woodmart_get_opt( 'empty_compare_text' );
    	?>
    	<div class="woodmart-compare-table">
    		<?php
    		if ( ! empty( $products ) ) {
    			array_unshift( $products, array() );
    			foreach ( $fields as $field_id => $field ) {
    				if ( ! woodmart_is_products_have_field( $field_id, $products ) ) {
    					continue;
    				}
    				?>
    					<div class="woodmart-compare-row compare-<?php echo esc_attr( $field_id ); ?>">
    						<?php $i = 0; ?>
    						<?php foreach ( $products as $product_id => $product ) : ?>
    							<?php if ( $i > 3 ) : ?>
    								<?php continue; ?>
    							<?php endif; ?>
    							<?php if ( ! empty( $product ) ) : ?>
    								<div class="woodmart-compare-col compare-value" data-title="<?php echo esc_attr( $field ); ?>">
    									<?php woodmart_compare_display_field( $field_id, $product ); ?>
    								</div>
    							<?php else: ?>
    								<div class="woodmart-compare-col compare-field">
    									<?php echo esc_html( $field ); ?>
    								</div>
    							<?php endif; ?>
    						<?php $i++; ?>
    						<?php endforeach ?>
    					</div>
    				<?php
    			}	
    		} else {
    			?>
    				<p class="woodmart-empty-compare woodmart-empty-page">
    					<?php esc_html_e('Compare list is empty.', 'woodmart'); ?>
    				</p>
    				<?php if ( $empty_compare_text ) : ?>
    					<div class="woodmart-empty-page-text"><?php echo wp_kses( $empty_compare_text, array('p' => array(), 'h1' => array(), 'h2' => array(), 'h3' => array(), 'strong' => array(), 'em' => array(), 'span' => array(), 'div' => array() , 'br' => array()) ); ?></div>
    				<?php endif; ?>
    				<p class="return-to-shop">
    					<a class="button" href="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>">
    						<?php esc_html_e( 'Return to shop', 'woodmart' ); ?>
    					</a>
    				</p>
    			<?php
    		}
    		?>
    	</div>
    	<?php
    }

    Regards

    #226723

    marmalabcreative
    Participant

    Hello Artem,

    The function is only hiding the products that are extra e.g. 4th, 5th, etc. But I can still add products to compare and I see on the menu bar that there are more than 3 products for compare.

    #226726

    Artem Temos
    Keymaster

    Sorry, but there is no option in our theme to limit the number of products in the compare completely, unfortunately.

    #226738

    marmalabcreative
    Participant

    Any plans to add it in the future?

    #226749

    Artem Temos
    Keymaster

    Currently, we don’t have such plans. We will consider this as a feature request for our future updates.

    #226755

    marmalabcreative
    Participant

    In this case:

    Can you point me which file I can edit on my own in order to limit adding items to 3?

    #226757

    Artem Temos
    Keymaster

    Sorry, but there is no such mechanism that you can simply change to 3. We don’t have instruction about this additional customization.

    #226855

    marmalabcreative
    Participant

    I understand that. I just ask which if you suggest me to edit in order to do it myself, like compare.php and/or function.php, etc..?

    #226885

    Artem Temos
    Keymaster

    It may require a lot of files to customize. It is not just a single place and we don’t have a complete instruction, unfortunately.

Viewing 10 posts - 1 through 10 (of 10 total)