• I am using elementor and their custom add to cart button stopped working. It now redirects to the homepage.

    The address it provides is https://DOMAIN.com/?add-to-cart=10 #.

    The problem is there’s a space between product id and the hashtag, which is forcing it to reload the homepage. With the custom add to cart button I am not able to change the URL, as it grabs from the product id you specify.

    Is there a way to remove the spacing in the URL?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @corneliusw, You can use the “woocommerce_loop_add_to_cart_link” filter to change the URL and customize according to your needs.

    Hi;

    Please use this snippet. hope to work:

    add_action( 'init', 'ywp_goto_product_after_add_to_cart' );
    function ywp_goto_product_after_add_to_cart() {
        if ( ! isset ( $_GET['add-to-cart'] ) ) retuen;
        
        // Remove #
        $product_id = intval( $_GET['add-to-cart'] );
    
        // Check id is a product id
        $product    = wc_get_product( $product_id );
    
        if ( $product ) {
            $url    = get_the_permalink( $product_id );
            wp_safe_redirect( $url );
            exit;
        }
    }

    Good luck

    @yazdaniwp THANK YOUUUUUUUUU SO MUCH. Been worried because these issues have made me lose a lot of sales. Thanks. It worked, I hard to add “checkout” in place of $url because I want them redirected to my Checkout Page. Thanks.

    @yazdaniwp Pls, can you help. I notice when I try adding a product https://mydomain.com/?add-to-cart=1260 it gives me this error message saying your cart is empty. This is my code, can you direct me on the right one>>>

    add_action( 'init', 'ywp_goto_product_after_add_to_cart' );
    function ywp_goto_product_after_add_to_cart() {
        if ( ! isset ( $_GET['add-to-cart'] ) ) return;
        
        // Remove #
        $product_id = intval( $_GET['add-to-cart'] );
    
        // Check id is a product id
        $product    = wc_get_product( $product_id );
    
        if ( $product ) {
            $url    = get_the_permalink( $product_id );
            wp_safe_redirect( 'cart' );
            exit;
        }
    }
    • This reply was modified 4 years, 2 months ago by Henry I..
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add to Cart button redirecting to homepage’ is closed to new replies.