Php 如何在woocommerce中隐藏特定消息?
我想在不修改基本woocommerce插件的情况下隐藏/删除woocommerce中的特定消息。 有几种类型的消息与优惠券相关Php 如何在woocommerce中隐藏特定消息?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想在不修改基本woocommerce插件的情况下隐藏/删除woocommerce中的特定消息。 有几种类型的消息与优惠券相关 优惠券代码已应用 对不起!优惠券12345已应用于您的购物车。(这里我主要想隐藏优惠券代码) 和其他一些类似的优惠券代码 我只想隐藏这些类型的优惠券/购物车消息,其他消息很好,如“Product successfully added!”或任何其他错误消息 基本上,目的是显示所有其他消息(错误和成功消息),但不想在这些消息中显示优惠券消息和优惠券代码 那么,有没有办法通过
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>
因此,您的问题的关键是-我如何定制WooCommerce优惠券信息
我有半个答案——我已经使用“woocommerce\u优惠券消息”过滤器定制了优惠券消息(绿色方框)。但是我还没有能够使用“woocommerce\u优惠券错误”过滤器获得优惠券错误消息(红色框)
我尝试过基于类中几个不同方法的条件语句,但没有成功。在打印错误消息之前,我似乎无法“截取”(然后自定义)错误消息。如果有人有解决优惠券错误的办法,我很乐意听到
无论如何。。。下面的函数连接到“购物车之前的woocommerce\u”和“结账之前的woocommerce\u表单”操作中,因此该函数适用于任一页面
显然,它可以无限定制,但我的示例基本上测试有效优惠券,然后您可以更改消息或不返回任何内容。您还可以测试其他条件以抛出各种自定义通知!比修改模板文件要好得多!:-)
我知道这是一个古老的线程,但我想我刚刚解决了如何处理优惠券错误消息 在我的商店里,我使用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 );
有关特定错误消息的$err\u code
可在文件woocommerce/includes/class wc-tup.php
中找到
在我的例子中,我想编辑错误代码114的错误消息(E_WC_优惠券_排除的_类别)。我还希望我的自定义消息仅在购物车中的礼品卡触发114错误时出现,而不是针对每个114错误。为了解决这个问题,我添加了if(在数组('15',$categories)中)
。这样做的目的是检查错误消息是否由类别15中的产品触发(这是my store的礼品卡类别。请将15更改为您使用的任何类别)
$instance
变量是WooCommerce用于将购物车/优惠券的详细信息传递回函数的变量
我对编码非常陌生,所以如果我的代码和解释不是很好,我很抱歉,但它显然适合我。我将它添加到functions.php
中
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' );
}
}
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 );