Alternative for wc_add_to_cart_message hook in Woocommerce for WP

I used the add to cart link in Woocommerce to edit text and remove some classes from certain buttons. It seems that this hook is now deprecated in Woocommerce 2.1, and I can not find an alternative.

I want to remove the 'button' class from the Continue Shopping button. This class is defined in the core of Woocommerce, which I want to leave unedited for correct future updates.

The line I'm trying to edit is in woocommerce / includes / wc-cart-functions.php line 94.

$message = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );

Has anyone found a suitable alternative for this hook? Thanks in advance!

+5
source share
4 answers

It worked for me

add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
    global $woocommerce;

        $return_to  = get_permalink(woocommerce_get_page_id('shop'));
        $message    = sprintf('<a href="%s" class="button wc-forwards">%s</a> %s', $return_to, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
    return $message;
}

: Kaarel Kaspar

+9

Woocommerce 2.3 +,

    add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
    function custom_add_to_cart_message( $message  ){
    global $woocommerce;

    $added_text = __( 'Product was successfully added to your Network Kit.', 'woocommerce' );
    // Output success messages
    if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :

        $return_to  = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );

        $message    = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );

    else :

        $message    = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', wc_get_page_permalink( 'cart' ), __( 'View your Network Kit', 'woocommerce' ), $added_text );

    endif;

    return $message;
}
+3

. , :

2.1- "wc_add_to_cart_message"

add_filter( 'wc_add_to_cart_message', 'foo' );
function foo() {

$product_id = $_REQUEST[ 'product_id' ];

if ( is_array( $product_id ) ) {

    $titles = array();

    foreach ( $product_id as $id ) {
        $titles[] = get_the_title( $id );
    }

    $added_text = sprintf( __( 'Added &quot;%s&quot; to your cart.', 'woocommerce' ), join( __( '&quot; and &quot;', 'woocommerce' ), array_filter( array_merge( array( join( '&quot;, &quot;', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );

} else {
    $added_text = sprintf( __( '&quot;%s&quot; was successfully added to your cart.', 'woocommerce' ), get_the_title( $product_id ) );
}

// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :

    $return_to  = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );

    $message    = sprintf(
        '<a href="%s" class="alert-link">%s &rarr;</a> %s',
        $return_to, __( 'Continue Shopping', 'woocommerce' ),
        $added_text
    );

else :

    $message    = sprintf(
        '<a href="%s" class="alert-link">%s &rarr;</a> %s',
        get_permalink( wc_get_page_id( 'cart' ) ),
        __( 'View Cart', 'woocommerce' ),
        $added_text );

endif;

return $message;
}

, .

0

, 3.0! G, , .

:

: wc_add_to_cart_message 3.0! wc_add_to_cart_message_html. /sitepath.com/wp-includes/functions.php 4329

Note: woocommerce_get_page_id is deprecated since version 3.0! Use wc_get_page_id instead. at /sitepath.com/wp-includes/functions.php on line 4329

As you can see, the solution to the problem (error message).

Use wc_add_to_cart_message_html

wc_get_page_id instead use wc_get_page_id instead

0
source

All Articles