• Hello,

    I’m using the plugin WooCommerce Variation Swatches to add variations of my products.

    I would like to show the variation on the archive page with the “add to cart” button.

    I have founded this code :

    /**
     * Replace add to cart button in the loop.
     */
    function iconic_change_loop_add_to_cart() {
    	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    	add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
    }
    
    add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );
    
    /**
     * Use single add to cart button for variable products.
     */
    function iconic_template_loop_add_to_cart() {
    	global $product;
    
    	if ( ! $product->is_type( 'variable' ) ) {
    		woocommerce_template_loop_add_to_cart();
    		return;
    	}
    
    	remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    	add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );
    
    	woocommerce_template_single_add_to_cart();
    }
    
    /**
     * Customise variable add to cart button for loop.
     *
     * Remove qty selector and simplify.
     */
    function iconic_loop_variation_add_to_cart_button() {
    	global $product;
    
    	?>
    	<div class="woocommerce-variation-add-to-cart variations_button">
    		<button type="submit" class="single_add_to_cart_button button"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
    		<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
    		<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
    		<input type="hidden" name="variation_id" class="variation_id" value="0" />
    	</div>
    	<?php
    }

    But there is 2 problems :
    – For the products with no variation, there is 2 times the button “add to cart”
    – For the products with variation, when I select the variation and click on “add to cart” there is not the pop up showing that the product has been added to the cart.

    Here you can see the archive page with the code :
    Sans-titre

    • This topic was modified 5 years, 7 months ago by busterland.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    Can you please share the site link so that I can check it?

    Thread Starter busterland

    (@busterland)

    Hello,

    The problem is that I have made these modifications on localhost. The modifications are not on the production website because of these errors.

    What do you need to check?

    Okay, can you please tell me the exact steps to replicate this issue?

    Thread Starter busterland

    (@busterland)

    Okay so I have added these 2 plugins (free version) :
    WooCommerce Variation Swatches : To get variation on my products
    WooCommerce Additional Variation Images : To get images on my variations

    Then I have added some variation on one product :
    – On the tab Product -> Attribute : create one attribute with type = Image
    – On this attribute, click on “Configure terms” on the right and add a new term.
    – On the product: edit it and go to Attribute, select your attribute and check box “Used for variation”. Then in the tab Variation, add the variation term created.

    To finish I have added the code you can see before in my function.php file in child theme.

    I hope I didn’t forget a step. Tell me if you need more informations.

    Thanks a lot for your help !

    Thread Starter busterland

    (@busterland)

    Hello,

    Do you have the time to check this ?

    Tell me if you have question.

    Thread Starter busterland

    (@busterland)

    Hello Amit,
    I give you all the steps. Could you check it please?
    Thanks

    Currently, we don’t have a solution for this issue. The plugin is also calling the add to cart button which is causing this issue. Have you also reached the plugin support?

    Thread Starter busterland

    (@busterland)

    I’ll forgive this, there is many variation so it’s not a good way to display it on archive page.
    You can close this topic
    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Show variation on archive page’ is closed to new replies.