• Resolved willirl

    (@willirl)


    Hi Favorites is useful to display a user’s favorite products. In my implementation the user then has to click the product which takes them to the Woocommerce Product Page to order. Is it possible to add Woocommerce Products Page functionality to the Favorites page, like price, add-to-cart etc.? A non-technical solution would be even better! Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Yes to copy your favorites to your cart create a button:
    <button id="favorites_to_cart">Add favorites to cart</button>
    Create an ajax function in your function.php

    function favorites_to_cart() {
    	global $woocommerce;
    	$clips = get_user_favorites();
    	foreach ($products as $product) {
    		$woocommerce->cart->add_to_cart( $product );
    	}
    	echo 'Your favorites is copied to the cart.';
    	wp_die();
    }
    
    add_action('wp_ajax_nopriv_favorites_to_cart', 'favorites_to_cart'); // ajax call for non-login user.
    add_action('wp_ajax_favorites_to_cart', 'favorites_to_cart'); // ajax call for login user.

    Create a javascript function

    jQuery(document).ready(function($) {
    
          var favorites_to_cart = function() {
            $.ajax({
              url: favorites_data.ajaxurl,
              data: {
                'action':'favorites_to_cart'
              },
              success:function(data) {
                alert(data);
              },  
              error: function(errorThrown){
                console.log('Error: ', errorThrown);
              }
            });
          };
    
          $( "#favorites_to_cart" ).on( "click", function() {
            favorites_to_cart();
          });
    
        });

    Good luck!

    • This reply was modified 4 years, 3 months ago by JesperT.
    Thread Starter willirl

    (@willirl)

    Jesper, that is really excellent, it works a treat, thank you very much.

    What I originally was hoping for was a add-to-cart button for each product. Is this possible? Your solution already improves the offering a lot, a button for each would be icing on the cake…. so only if it is easy!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add_to_cart on list of Product Favorites’ is closed to new replies.