【问题标题】:How to hide specific messages in woocommerce?如何在 woocommerce 中隐藏特定消息?
【发布时间】:2017-02-18 17:27:20
【问题描述】:

我想在不修改基本 woocommerce 插件的情况下隐藏/删除来自 woocommerce 的特定消息。 与优惠券相关的消息有多种类型,例如

  1. 优惠码已申请!
  2. 对不起!优惠券 12345 已应用于您的购物车。 (这里我本质上是想隐藏优惠券代码)

还有其他几个与这些优惠券代码类似的代码。

我只是想隐藏这些类型的优惠券/购物车消息,其他都可以,例如“产品已成功添加!”或任何其他错误消息。

基本上目的是显示所有其他消息(错误和成功消息),但不想在这些消息中显示优惠券消息和优惠券代码。

那么,有没有办法通过做任何钩子等来做到这一点,比如我发现消除所有消息字符串的方法(如果我没记错的话)。

add_filter( 'woocommerce_coupon_message', '__return_empty_string' );
  1. 另一件事是,当我将产品添加到汽车中时,购物车页面上会多次重复一条消息。 “优惠码已申请!” 2,3 至 4 次。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    好的,找到解决办法

    转到woocommerce模板,复制通知文件夹并编辑所需的模板,在我的例子中是error.php

    复制/编辑代码

    <ul class="woocommerce-error">
        <?php
         foreach ( $messages as $message ) : 
         if ( $message == "Coupon code already applied!" ) {
                $message = "";//empty error string
    
            }  else if (strpos($message, 'does not exist!') !== false) {
                    $message = ""; //empty error string
    
                }
               else if (strpos($message, 'Sorry, it seems the coupon') !== false) {
                    $message = "";//empty error string
    
                }
               else if (strpos($message, 'Sorry, this coupon is not applicable to your cart contents') !== false) {
                    $message = "Sorry, the discount is not applicable to your cart contents"; //updated error string
    
                }
        ?> 
            <li><?php echo wp_kses_post( $message ); ?></li>
        <?php
        break;
         endforeach; ?>
    </ul>
    

    【讨论】:

      【解决方案2】:

      所以您的问题的症结是 - 我如何自定义 WooCommerce 优惠券消息?

      我有一半的答案 - 我已经使用“woocommerce_coupon_message”过滤器定制了优惠券消息(绿色框的消息)。但是我还没有能够使用“woocommerce_coupon_error”过滤器获得优惠券错误消息(红色框出的消息)。

      我尝试了基于 Class WC_Cart 中的几种不同方法的条件语句,但无济于事。在打印错误消息之前,我似乎无法“拦截”(然后自定义)错误消息。如果有人对优惠券错误有解决方案,我很乐意听到。

      无论如何...下面的函数与“woocommerce_before_cart”和“woocommerce_before_checkout_form”操作挂钩,因此该函数适用于任一页面。

      它显然可以自定义到无止境,但我的示例基本上是测试有效的优惠券,然后您可以更改消息或不返回任何内容。您还可以测试其他条件以抛出各种自定义通知!比修改模板文件好得多! :-)

      add_action( 'woocommerce_before_cart', 'custom_coupon_messages' );
      add_action( 'woocommerce_before_checkout_form', 'custom_coupon_messages' );
      function custom_coupon_messages() {
          global $woocommerce;
      
          //Set coupon codes.
          $coupon_code = 'Bigly-Yuge';
      
          //Set coupon objects.
          $coupon_test = new WC_Coupon( 'Bigly-Yuge' );
      
          //Get the cart subtotal. Should return as a Double.
          $cart_subtotal = WC()->cart->subtotal;
      
          //If coupon test is passed add coupon.
          if ( $coupon_test->is_valid() && $woocommerce->cart->has_discount( $coupon_code ) ) {
              //Filter the coupon success message to display a custom message.
              add_filter( 'woocommerce_coupon_message', 'filter_woocommerce_coupon_message', 10, 3 );
              function filter_woocommerce_coupon_message( $msg, $msg_code, $instance ) {
                  //Set a custom coupon message.
                  $msg_code = 'case self::WC_COUPON_SUCCESS';
                  $msg = __( 'You saved BIGLY YUGE!', 'woocommerce' );
      
                  return $msg;
                  return $msg_code;
      
                  //Or return nothing (no message will be displayed - comment out the above/uncomment below).
                  // return '';
              };
              //Print the above notice to screen.
              wc_print_notices();
          }
          elseif ( $cart_subtotal > 499 ) {
              //Print a notice (the blue boxed one)
              wc_print_notice( 'Spend $500 to qualify for the BIGLY YUGE discount!!!', 'notice' );
          }
      }
      

      【讨论】:

        【解决方案3】:

        我知道这是一个旧线程,但我想我刚刚想出了如何为优惠券错误消息执行此操作。

        在我的商店中,我使用 WooCommerce 智能优惠券,它可以将优惠券作为礼品卡出售。我不希望人们能够使用礼品卡购买礼品卡,因此我已将礼品卡类别添加到优惠券使用限制的排除类别列表中。

        无论如何,如果有人在购物车中有礼品卡时尝试使用礼品卡优惠券代码,我想更改错误消息。这是我使用的代码:

        function filter_woocommerce_coupon_error( $err, $err_code, $instance ) {
        
            if ( $err_code == '114' ) {
        
                global $woocommerce;                
                $categories = $instance->get_excluded_product_categories();
        
                if ( in_array( '15', $categories ) ) {
        
                    $err = sprintf( __( 'Sorry, you cannot use a Gift Card to purchase another Gift Card.' ) );
        
                }
            }   
        
            return $err;
        
        };
        
        add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );
        

        可以在文件woocommerce/includes/class-wc-coupon.php 中找到特定错误消息的$err_code

        就我而言,我想编辑错误代码 114 (E_WC_COUPON_EXCLUDED_CATEGORIES) 的错误消息。我还希望我的自定义消息仅在购物车中的礼品卡触发 114 错误时出现,而不是每个 114 错误都出现。为了解决这个问题,我添加了if ( in_array( '15', $categories ) )。它的作用是检查错误消息是否由类别 15 中的产品触发(这是我商店的礼品卡类别。将 15 更改为您使用的任何类别)。

        $instance 变量是 WooCommerce 用来将购物车/优惠券的详细信息传回函数的。

        我对编码很陌生,所以如果我的代码和我的解释不是很好,我很抱歉,但它似乎对我有用。我将其添加到functions.php

        【讨论】:

          猜你喜欢
          • 2017-12-26
          • 2016-06-25
          • 1970-01-01
          • 2018-01-27
          • 2019-10-09
          • 1970-01-01
          • 1970-01-01
          • 2016-09-04
          • 2018-09-08
          相关资源
          最近更新 更多