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

Woocommerce 从“添加到购物车”通知中删除“继续购物”按钮,woocommerce,Woocommerce,目前,当有人向我们的网站添加产品时,会显示:“X产品”已添加到您的购物车中,然后在右侧的通知中有一个“继续购物”按钮 由于我们只销售2种产品,我们希望完全删除“继续购物”按钮,但仍会显示消息的其余部分,并保留“X产品”作为链接 我一直在使用下面的代码(但它用Checkout代替了Continue Shopping,我更愿意完全删除按钮)。我只是不知道如何删除按钮,但仍然保持消息的其余部分完全相同: add_filter( 'woocommerce_continue_shopping_redire

目前,当有人向我们的网站添加产品时,会显示:“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;

}

如果有人对以下代码感兴趣,请将id-407替换为购物车页面的任何页面id:

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

使用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;
    }
}
add_filter('wc_add_to_cart_message_html','remove_continue_shopping_button',10,2);
功能删除\继续\购物\按钮($message,$products){
if(strpos($message,'Continue shopping')!==false){
返回preg_replace('/
/*开始禁用添加到购物车后继续购物消息
*/
添加筛选器('wc\u add\u to\u cart\u message',函数($string,$product\u id=0){

$start=strpos($string,'这并没有删除任何内容。它只是隐藏了它-这是一种绝对可怕的做法。这对于严格的视觉方法来说是可以的,因为它确实从CSS角度隐藏了它,但更好的方法是使用'add_filter'调用删除主函数循环中对象的呈现,这将有助于实现更高性能的方法。
/* 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
*/