doubeng9407 2018-03-07 21:38
浏览 84
已采纳

在WooCommerce成功消息中更改呈现的html

After adding a product to the cart in WooCommmerce you get a notice saying:

“Product" has been added to your cart. View cart

The link element (View cart) is rendered first and the string element (“Product" has been added to your cart.) second. I'd like to switch these element so the string is rendered first and link second.

HTML:

<div class="woocommerce-message" role="alert">
<a href="http://example.com/cart/" class="button wc-forward">View cart</a> “Happy Ninja” has been added to your cart.</div>
</div>

Tried tracking down the code but got stuck at: https://github.com/woocommerce/woocommerce/blob/master/templates/notices/success.php

Looking for a snippet to add to functions.php

  • 写回答

1条回答 默认 最新

  • duangang4001 2018-03-08 00:14
    关注

    Try this:

    add_filter( 'wc_add_to_cart_message_html', function( $message ) {
        return preg_replace(
            // Or use ^(<a href=".+?" class="button wc-forward">.+?</a>) *(.+)
            '#^(<a href=".+?" class="button wc-forward">.+?</a>) +(.+)#',
            '$2 $1',
            trim( $message )
        );
    } );
    

    If no plugins (or if the theme doesn't) modify the default HTML markup (as you can see on line #121 and line #123), the code above should work well.

    [EDIT #3] Alternatively, you can completely re-generate the message/HTML, as in the following example:

    add_filter( 'wc_add_to_cart_message_html', 'my_wc_add_to_cart_message_html', 10, 2 );
    function my_wc_add_to_cart_message_html( $message, $products ) {
        $titles = array();
        $count  = 0;
    
        foreach ( $products as $product_id => $qty ) {
            $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
            $count += $qty;
        }
    
        $titles     = array_filter( $titles );
        $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
    
        // Output success messages - the "View cart" or "Continue shopping" link comes after the product link/info.
        if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
            $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
            $message   = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ) );
        } else {
            $message   = sprintf( '%s <a href="%s" class="button wc-forward">%s</a>', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ) );
        }
    
        return $message;
    }
    

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法