【问题标题】:WooCommerce wc_add_notice() with custom notice_type and cart updateWooCommerce wc_add_notice() 与自定义notice_type 和购物车更新
【发布时间】:2018-01-09 23:34:05
【问题描述】:

我正在尝试将通知类型添加到 WooCommerce wc_add_notice() 类型集(successnoticeerror)。称它为'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 可以解决......?

谢谢。

【问题讨论】:

  • 当有Warning 消息时是否要隐藏Notice?这不是woocommerce_check_cart_items 函数同时添加了通知和警告吗?
  • @RaunakGupta 第一次进入购物车页面:两者都已打印。然后,如果我更新数量:通知消息已更新,警告消息未更新。我想: 1. 删除旧警告消息 2. 显示新警告消息,就像通知消息一样

标签: jquery ajax woocommerce customization hook-woocommerce


【解决方案1】:

使用您的自定义类添加错误或成功通知类,如下所示:“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;
  }

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 2019-08-07
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多