Alternative for the wc_add_to_cart_message hook in Woocommerce for WP

后端 未结 5 511
轮回少年
轮回少年 2020-12-17 00:48

I used the add to cart message hook in Woocommerce to edit the text and remove some classes from certain buttons. It seems this hook is now deprecated in Woocommerce 2.1 and

5条回答
  •  天命终不由人
    2020-12-17 01:20

    this could be a solution. please change if you have better ways or ideas:

    the filter-name has changed with the 2.1-version to "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 "%s" to your cart.', 'woocommerce' ), join( __( '" and "', 'woocommerce' ), array_filter( array_merge( array( join( '", "', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );
    
    } else {
        $added_text = sprintf( __( '"%s" 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(
            '%s → %s',
            $return_to, __( 'Continue Shopping', 'woocommerce' ),
            $added_text
        );
    
    else :
    
        $message    = sprintf(
            '%s → %s',
            get_permalink( wc_get_page_id( 'cart' ) ),
            __( 'View Cart', 'woocommerce' ),
            $added_text );
    
    endif;
    
    return $message;
    }
    

    hope it helps. cheers

提交回复
热议问题