从添加到购物车通知中删除继续购物按钮



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

由于我们只销售 2 种产品,我们希望完全删除继续购物按钮,但仍说出其余消息,并保留"X 产品"作为链接。

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

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;
}

使用 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;
    }
}
/* 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
*/

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

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

最新更新