• Resolved tortitudo

    (@tortitudo)


    I would like to replace the “Add to cart” button with a “View Product” button almost everywhere. This is because I expect that my customers will want to read about the product before adding it to the cart.

    The “Add to Cart” button appears below products in many different cases, such as when using the Products by Category block, Hand Picked Products block, and Related products block (by default included on the single post page). After many hours of searching, I am unable to find a solution, or even a partial solution. Or, to be precise, the solution I found doesn’t appear to be working, which is why I can’t continue researching and came here. The code is:

    #this line is supposed to remove the default button entirely
    
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
     
    #and this line is supposed to add some custom html - a button with the correct link, of course
    
    add_action( 'woocommerce_after_shop_loop_item', 'wptechnic_custom_view_product_button', 10);
    function wptechnic_custom_view_product_button() {
    global $product;
    $link = $product->get_permalink();
    echo '<a href="' . $link . '" class="button wptechnic-custom-view-product-button">View Product</a>';
    }

    Can anyone point me in the right direction why this does not work? Other functions and filters in my function.php file work correctly.

    I am using wordpress 6.4.1 and my woocommerce version is 8.2.2

    Thanks for the help!

    • This topic was modified 1 year, 3 months ago by tortitudo.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tortitudo

    You can use the Say What plugin to change the “add to cart” text because the code snippet you’re currently using doesn’t function the same way for the block. You can find the instructions on how to do this here: https://www.ads-software.com/support/topic/customize-add-to-cart-notice/#post-13914076

    For more information on how that works, have a look at the following guide: https://woo.com/document/woocommerce-localization/#translate-or-replace-default-text

    I hope this helps! Please let us know how it goes or if you need further assistance.

    Thread Starter tortitudo

    (@tortitudo)

    Thanks for the quick reply Shameem, but I’m not sure that’s gonna work. I already know how to replace the text on the button (via Loco translate or via a php filter), and I even how to replace the URL of the button from the (via filter).
    But even with both changes applied and working, pressing the button STILL adds to cart. I think I need to replace the ENTIRE button HTML, because there are many hints in the HTML that pressing it will do that.

    Here is the actual HTML. You can see that even if I change the href to product permalink, and the button text to “View product”, the button will still add it to the cart.

    <a href="?add-to-cart=17" aria-label="my custom text" data-quantity="1" data-product_id="17" data-product_sku="xxxmysku" rel="nofollow" class="wp-block-button__link wp-element-button add_to_cart_button ajax_add_to_cart">Add to cart</a>

    I think I need to remove the class add_to_cart_button ajax_add_to_cart. I’m an IT guy so I would love to learn about a technical solution to this. From my research on the internet, I have learned that there is a thing called “overriding the block output” with php to add custom code. That sounds like it could work. I could look at the block output and change the class with php. How can I do that?

    Saif

    (@babylon1999)

    Hello @tortitudo,

    WooCommerce blocks are JS-based so most of the hooks in the default PHP templates won’t be triggered.

    If you are working on creating a custom solution around blocks,?kindly refer to this section of the development repository.

    You could also try translating the text using TranslatePress, which works great with Gutenberg blocks: https://translatepress.com/translate-gutenberg-blocks-in-wordpress/


    Link to image: https://d.pr/i/jILD2b


    Link to image: https://d.pr/i/o6CEAj

    Hope this helps!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘php ACTION not working – replacing “Add to Cart” buttons with “View Product”’ is closed to new replies.