Yoo Woo
Yoo Woo

Reputation: 9

WooCommerce 'if ( is_cart() )' in functions.php not working?

I've added the code below within my functions.php so I can change the WooCommerce standard 'Add to cart notice'.

The notice does change but the if ( is_cart() ) does not seem to work. It outputs FALSE on the cart page.

I must have overseen something..?

add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );

function yw_add_to_cart_message($message, $product_id = null) {

    $titles[] = get_the_title( $product_id );

    $titles = array_filter( $titles );

    if ( is_cart() ) {
        $cart_link  = '<div class="uk-width-medium-1-5 uk-text-right"><a href="' . WC_Cart::get_checkout_url() . '"><i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '</a></div>';
    } else {
        $cart_link  = '<div class="uk-width-medium-1-5 uk-text-right"><a href="' . WC_Cart::get_cart_url() . '"><i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '</a></div>';
    }

    $added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div>' . $cart_link . '</div>';

    $message = sprintf( '%s', $added_text );

    return $message;
}

Upvotes: 0

Views: 3921

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253868

You have to use get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' instead of is_cart():

add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );

function yw_add_to_cart_message($message, $product_id = null) {

    $titles[] = get_the_title( $product_id );

    $titles = array_filter( $titles );

    if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
        $cart_link  = '<a href="' . WC_Cart::get_checkout_url() . '"><i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '</a>';
    } else {
        $cart_link  = '<a href="' . WC_Cart::get_cart_url() . '"><i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '</a>';
    }

    $added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div><div class="uk-width-medium-1-5 uk-text-right">' . $cart_link . '</div></div>';

    $message = sprintf( '%s', $added_text );

    return $message;
}

Related threads: Here and Here

Upvotes: 1

Related Questions