在WooCommerce结帐字段shipping_address_2
中,我建立了一个自定义折扣优惠券。我需要的是在特定的电子邮件通知中获取此订单字段值。
这是我的代码尝试:
add_action( 'woocommerce_email_before_order_table', 'add_content_to_specific_email', 20, 4 );
function add_content_to_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_processing_order' ) {
echo '<h2 class="email-upsell-title">Get 20% off</h2><p class="email-upsell-p">Thank you for making this purchase! Come back and use the code "<strong> get_post_meta( $order_id, '_shipping_address_2', true ) </strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
}
它不起作用。我想将此字段值转换为<strong>
html标记之间的字符串。
欢迎您的帮助。
答案 0 :(得分:1)
您已经很接近实现您的期望了。正确的是:
echo '<h2 class="email-upsell-title">Get 20% off</h2><p class="email-upsell-p">Thank you for making this purchase! Come back and use the code "<strong>' . get_post_meta( $order->ger_id(), '_shipping_address_2', true ) . '</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
但是最好使用
WC_Order
get_shipping_address_2()
方法。
这是您重新访问的代码:
add_action( 'woocommerce_email_before_order_table', 'add_content_to_specific_email', 20, 4 );
function add_content_to_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
// For customer processing and completed orders notifications
if ( in_array($email->id, ['customer_processing_order', 'customer_completed_order']) ) {
if( $coupon_code = $order->get_shipping_address_2() ) {
echo '<h2 class="email-upsell-title">'.__("Get 20% off").'</h2>
<p class="email-upsell-p">';
printf(
__("Thank you for making this purchase! Come back and use the code %s to receive a 20%% discount on your next purchase! %s."),
'"<strong>'.$coupon_code.'</strong>"',
'<a href="'. get_permalink( wc_get_page_id( 'shop' ) ) .'">' . __("Click here to continue shopping") . '</a>'
);
echo '</p>';
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。