• Resolved perfectleads

    (@perfectleads)


    I want to replace “add to cart” with “Checkout”, and the code works fine, but I wanted it to work only on product pages, not on the shop page for example.
    I added this code at the top as requested here, but nothing – it either replaces every “add to cart” with “Checkout” ( no !) or it doesn’t (with !).
    the code in question:
    if ( ! is_singular( [ 'product' ] ) ) return;

    the entire code

    if ( ! is_singular( [ 'product' ] ) ) return;
    add_action( ‘wp_head’, function () {if(is_woocommerce()) { ?>
    if ( ! is_product() ) return;
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'cw_btntext_cart' );
    add_filter( 'woocommerce_product_add_to_cart_text', 'cw_btntext_cart' );
    function cw_btntext_cart() {
        return __( 'Checkout', 'woocommerce' );
    })									
    <?php }} );

    I tried to replace ‘product’ with product IDs, titles, tags, with ‘ and without, 2-3 hours of trying to get this right, in vain.
    – my theme is porto if it matters

    • This topic was modified 4 years, 11 months ago by perfectleads.
    • This topic was modified 4 years, 11 months ago by perfectleads.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi @perfectleads,

    You will want to use the WooCommerce specific is_product() function for determining whether the snippet is running on a prpoduct page.

    Plugin Author Shea Bunge

    (@bungeshea)

    Actually, taking a second look at your code, if you only use the one filter, then it looks like you don’t need to do any additional checking at all:

    add_filter( 'woocommerce_product_single_add_to_cart_text', function () {
    	return __( 'Checkout', 'woocommerce' );
    } );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Code to place snippets only on certain Woocommerce product pages is Not working’ is closed to new replies.