Php 如果使用了特定优惠券,则在“收到的商业订单”页面上显示自定义文本
如果在结帐期间使用了三个特定优惠券代码中的一个,我将尝试在Woocommerce order received页面上显示一条自定义感谢消息 我们的Woocommerce版本是2.6.11 我尝试了以下代码的一些变体,但无法使其工作,我是否做了一些错误的事情Php 如果使用了特定优惠券,则在“收到的商业订单”页面上显示自定义文本,php,wordpress,woocommerce,orders,coupon,Php,Wordpress,Woocommerce,Orders,Coupon,如果在结帐期间使用了三个特定优惠券代码中的一个,我将尝试在Woocommerce order received页面上显示一条自定义感谢消息 我们的Woocommerce版本是2.6.11 我尝试了以下代码的一些变体,但无法使其工作,我是否做了一些错误的事情 //show custom coupon thankyou function coupon_thankyou($order_id) { $coupon_id = '1635'; $order = wc_get_order($o
//show custom coupon thankyou
function coupon_thankyou($order_id) {
$coupon_id = '1635';
$order = wc_get_order($order_id);
foreach( $order->get_items('coupon') as $coupon_item ){
if( $coupon_item->get_code() = $coupon_id ){
echo '<p>This is an custom thank you.</p>';
}
}
}
add_action('woocommerce_thankyou','coupon_thankyou');
//显示定制优惠券谢谢
功能优惠券谢谢($order\u id){
$优惠券id='1635';
$order=wc\u get\u order($order\u id);
foreach($order->get_items('优惠券')作为$优惠券_item){
如果($优惠券\项目->获取\代码()=$优惠券\ id){
回声'这是一个定制的谢谢。';
}
}
}
添加行动(“感谢您”、“优惠券”);
您的IF语句条件中有一个错误,=
必须替换为=
或=
。同样对于优惠券,您需要使用优惠券代码slug(但不是post ID)
要在订单接收页面上显示消息,最好使用woocommerce\u thankyou\u Order\u received\u text
filter hook,这种方式(适用于woocommerce 3+):
// On "Order received" page (add a message)
add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_applied_coupon_message', 10, 2 );
function thankyou_applied_coupon_message( $text, $order ) {
$coupon_code = '1635'; // coupon code name
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon->get_code() === $coupon_code ){
$text .= '<p>'.__("This is an custom thank you.").'</p>';
}
}
return $text;
}
//在“订单已收到”页面上(添加消息)
添加过滤器(“woocommerce\u Thankyu\u order\u received\u text”、“Thankyu\u applied\u优惠券\u message”,10,2);
功能感谢您申请优惠券信息($text,$order){
$优惠券代码='1635';//优惠券代码名称
foreach($order->get_items('优惠券')作为$优惠券){
如果($优惠券->获取优惠券代码()==$优惠券代码){
$text.=''。uuu(“这是一个定制的谢谢。”)。;
}
}
返回$text;
}
代码进入活动子主题(或活动主题)的function.php文件。现在应该可以了
已更新 对于Woocommerce 3.0之前的版本,您应该使用以下内容:
// On "Order received" page (add a message)
add_action( 'woocommerce_thankyou', 'thankyou_applied_coupon_message', 10, 1 );
function thankyou_applied_coupon_message( $order_id ) {
$coupon_code = '1635'; // coupon code name
$order = wc_get_order( $order_id );
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon['name'] === $coupon_code ){
echo '<p>'.__("This is an custom thank you.").'</p>';
}
}
}
//在“订单已收到”页面上(添加消息)
添加行动('woocommerce\u Thankyu'、'Thankyu\u applied\u优惠券\u message',10,1);
功能感谢您申请优惠券信息($order\u id){
$优惠券代码='1635';//优惠券代码名称
$order=wc\u get\u order($order\u id);
foreach($order->get_items('优惠券')作为$优惠券){
如果($优惠券['name']==$优惠券\u代码){
回显“”。(“这是一个定制的谢谢。”)。;
}
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试并运行。您的IF语句条件中有一个错误,
=
必须替换为=
或==
。同样对于优惠券,您需要使用优惠券代码slug(但不是post ID)
要在订单接收页面上显示消息,最好使用woocommerce\u thankyou\u Order\u received\u text
filter hook,这种方式(适用于woocommerce 3+):
// On "Order received" page (add a message)
add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_applied_coupon_message', 10, 2 );
function thankyou_applied_coupon_message( $text, $order ) {
$coupon_code = '1635'; // coupon code name
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon->get_code() === $coupon_code ){
$text .= '<p>'.__("This is an custom thank you.").'</p>';
}
}
return $text;
}
//在“订单已收到”页面上(添加消息)
添加过滤器(“woocommerce\u Thankyu\u order\u received\u text”、“Thankyu\u applied\u优惠券\u message”,10,2);
功能感谢您申请优惠券信息($text,$order){
$优惠券代码='1635';//优惠券代码名称
foreach($order->get_items('优惠券')作为$优惠券){
如果($优惠券->获取优惠券代码()==$优惠券代码){
$text.=''。uuu(“这是一个定制的谢谢。”)。;
}
}
返回$text;
}
代码进入活动子主题(或活动主题)的function.php文件。现在应该可以了
已更新 对于Woocommerce 3.0之前的版本,您应该使用以下内容:
// On "Order received" page (add a message)
add_action( 'woocommerce_thankyou', 'thankyou_applied_coupon_message', 10, 1 );
function thankyou_applied_coupon_message( $order_id ) {
$coupon_code = '1635'; // coupon code name
$order = wc_get_order( $order_id );
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon['name'] === $coupon_code ){
echo '<p>'.__("This is an custom thank you.").'</p>';
}
}
}
//在“订单已收到”页面上(添加消息)
添加行动('woocommerce\u Thankyu'、'Thankyu\u applied\u优惠券\u message',10,1);
功能感谢您申请优惠券信息($order\u id){
$优惠券代码='1635';//优惠券代码名称
$order=wc\u get\u order($order\u id);
foreach($order->get_items('优惠券')作为$优惠券){
如果($优惠券['name']==$优惠券\u代码){
回显“”。(“这是一个定制的谢谢。”)。;
}
}
}
代码进入活动子主题(或活动主题)的function.php文件。经过测试,效果良好。非常感谢您对Loic的回复。当您提到需要使用的优惠券代码“slug”时,您是指实际的优惠券代码本身吗?我查看了优惠券代码设置,似乎没有任何与优惠券代码相关的slug。我已经测试了您提供的代码的2.6版本,但使用
$coupon\u code
变量的代码本身的优惠券代码ID无法使其工作。@Connor优惠券代码是优惠券名称(或slug)。第一个代码在我测试过的时候就可以运行了…现在我不记得WC 2.6是如何实现的…我将尝试在WC 2.5或2.6下捕获一个测试服务器,看看需要什么。感谢Loic的澄清。非常感谢您对Loic的回复。当您提到需要使用的优惠券代码“slug”时,您是指实际的优惠券代码本身吗?我查看了优惠券代码设置,似乎没有任何与优惠券代码相关的slug。我已经测试了您提供的代码的2.6版本,但使用$coupon\u code
变量的代码本身的优惠券代码ID无法使其工作。@Connor优惠券代码是优惠券名称(或slug)。第一个代码在我测试它时工作…现在我不记得WC 2.6如何做到这一点…我将尝试捕获WC 2.5或2.6下的测试服务器,看看需要什么。感谢Loic的澄清。