我正在尝试将通知类型添加到WooCommerce wc_add_notice()类型集(success、notice、error)。称它为'warning',它应该比'notice'“更严重”,但对用户来说没有'error'那么严重。
我开始将wp-content/plugins/woocommerce/teplates/notices/notice.php复制到wp-content/themes/mytheme/woocommerce/notices/warning.php中。
然后我将它的类名修改为woocommerce-warning
<?php foreach ( $messages as $message ) : ?>
<div class="woocommerce-warning"><?php echo wp_kses_post( $message ); ?></div>
<?php endforeach; ?>然后,为了使其正常工作,我必须添加以下代码
add_filter('woocommerce_notice_types', function ($notice_types) {
$notice_types[] = "warning";
return $notice_types;
});到我的functions.php,让WooCommerce打印我的新通知类型。
当然,还需要一些CSS来调整颜色和样式:
.woocommerce-warning {
border-top-color:gold;
border-bottom-color:gold;
}
.woocommerce-warning::before {
content: "\e016";
color: gold;
top: 0.2em;
left: 0.5em;
font-size: 1.8em;
}现在,通过一个简单的wc_add_notice('message', 'warning');,我的警告消息就在那里了!
最初它看起来很有效,但现在我意识到它的行为与最初的通知类型不同:如果我更新购物车,即更正产品数量(我认为这是一个AJAX调用,可能有一些jquery在那里..?),我的验证器函数会添加一个不会更新的警告通知(它只是与旧消息一起保留在那里)。将类型warning替换为notice消息将按预期更新。
add_filter('woocommerce_check_cart_items', function() {
$prod = WC()->cart->get_cart_contents_count();
wc_add_notice('Notice: cart containing '.$prod.' products', 'notice'); // message gets updated
wc_add_notice('Warnign: cart containing '.$prod.' products', 'warning'); // this doesn't
});你知道为什么吗?我的猜测是:也许需要一些jquery来解决...?
谢谢。
发布于 2020-06-12 11:38:50
使用您的自定义类添加错误或成功通知类,如下所示:“woocommerce-message woocommerce-warning”
要让它的js正常工作并消失,然后再让css:
.woocommerce-message.woocommerce-warning {
border-top-color:gold;
border-bottom-color:gold;
}
.woocommerce-message.woocommerce-warning::before {
content: "\e016";
color: gold;
top: 0.2em;
left: 0.5em;
font-size: 1.8em;
}https://stackoverflow.com/questions/45465968
复制相似问题