【问题标题】:replacing "view cart" button with "proceed to checkout" button upon adding a product to cart将产品添加到购物车时,将“查看购物车”按钮替换为“继续结帐”按钮
【发布时间】:2016-01-15 18:09:28
【问题描述】:

将产品添加到购物车后重新加载产品页面后,顶部会显示一条通知,上面写着“产品已添加到购物车”,然后是“查看购物车”按钮。

但是,在将产品添加到购物车后,我想将客户引导至结帐页面(我将在其中添加购物车页面功能),而不是购物车页面。如何将通知中的购物车链接替换为结帐页面的链接?

wc_add_to_cart_message() 函数(在includes/wc-cart-functions.php 中)似乎生成了这个特定的通知,我应该重写这个函数吗?如果有,怎么做?

【问题讨论】:

    标签: notifications woocommerce customization


    【解决方案1】:

    试试这个...

    function rei_wc_add_to_cart_message( $message, $product_id ) {
        $titles = array();
    
        if ( is_array( $product_id ) ) {
            foreach ( $product_id as $id ) {
                $titles[] = get_the_title( $id );
            }
        } else {
            $titles[] = get_the_title( $product_id );
        }
    
        $titles     = array_filter( $titles );
        $added_text = 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 ) );
    
        // Output success messages
        if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
            $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', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
        } else {
            $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'checkout' ) ), esc_html__( 'Proceed to Checkout', 'woocommerce' ), esc_html( $added_text ) );
        }
    
        return $message;
    }
    add_filter('wc_add_to_cart_message','rei_wc_add_to_cart_message',10,2);
    

    【讨论】:

    • 完美运行,感谢@Reigel。一个关于未来类似问题的问题:我怎样才能发现自己存在诸如“wc_add_to_cart_message”之类的钩子?
    • 如果您知道如何在文件夹中的文件上搜索文本,您可以只搜索您要查找的单词。我使用 notepad++ 在 woocommerce 文件夹中搜索“查看购物车”。
    • 谢谢,我刚刚找到了 6 个实例,但没有一个与 'wc_add_to_cart_message' 挂钩!
    • wc-cart-functions.php 中有 1 个
    • 发现了,下次遇到类似问题我会尝试按照同样的方法。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多