【问题标题】:Removing Continue Shopping button from Added to Cart Notice从添加到购物车通知中删除继续购物按钮
【发布时间】:2018-03-01 21:24:05
【问题描述】:

目前,当有人向我们的网站添加产品时,它会显示:“X 产品”已添加到您的购物车 |然后在右侧的通知中有一个“继续购物”按钮。

由于我们只销售 2 种产品,因此我们希望完全移除继续购物按钮,但仍会说出其余的信息,并保留“X 产品”作为链接。

我一直在使用以下代码(但它用 Checkout 代替了 Continue Shopping,我宁愿完全删除按钮)。我只是不知道如何删除按钮,但仍然保持消息的其余部分完全相同:

add_filter( 'woocommerce_continue_shopping_redirect', 'my_changed_woocommerce_continue_shopping_redirect', 10, 1 );
function my_changed_woocommerce_continue_shopping_redirect( $return_to ){

    $return_to = wc_get_page_permalink( 'checkout' );

    return $return_to;
}


add_filter( 'wc_add_to_cart_message_html', 'my_changed_wc_add_to_cart_message_html', 10, 2 );
function my_changed_wc_add_to_cart_message_html($message, $products){

    if (strpos($message, 'Continue shopping') !== false) {
        $message = str_replace("Continue shopping", "Checkout", $message);
    }

    return $message;

}

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    使用 preg_replace 查找包含完整链接的字符串,并返回包含所有原始 HTML 减去链接的新消息。

    add_filter('wc_add_to_cart_message_html','remove_continue_shoppping_button',10,2);
    
    function remove_continue_shoppping_button($message, $products) {
        if (strpos($message, 'Continue shopping') !== false) {
            return preg_replace('/<a.*<\/a>/m','', $message);
        } else {
            return $message;
        }
    }
    

    【讨论】:

      【解决方案2】:
      /* Start Disable Continue Shopping Message after Add to Cart
      */
      
      add_filter( 'wc_add_to_cart_message', function( $string, $product_id = 0 ) {
          $start = strpos( $string, '<a href=' ) ?: 0;
          $end = strpos( $string, '</a>', $start ) ?: 0;
          return substr( $string, $end ) ?: $string;
      });
      
      /* End Disable Continue Shopping Message after Add to Cart
      */
      

      【讨论】:

        【解决方案3】:

        如果有人对以下代码感兴趣,可以将其完美修复,只需将 id-407 替换为您的购物车页面的任何页面 id:

            /* Remove Continue Shopping Button Add Cart */
        body.page-id-407 .woocommerce-message .button {
            display: none
        }
        

        【讨论】:

        • 这不会删除任何内容。它只是隐藏了它 - 一种绝对可怕的做法。
        • 没关系。对于严格的可视化方法,因为它确实从 CSS 角度隐藏了它,但更好的方法是使用“add_filter”调用在主函数循环中删除对象的呈现,这将提高性能。
        • 不管页面ID如何,最好使用这些css .woocommerce-cart .wc-forward.button { display:none;}
        猜你喜欢
        • 2023-04-05
        • 1970-01-01
        • 2018-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多