在WooCommerce电子邮件通知的文本中显示送货地址2

问题描述 投票:1回答:1

在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标记之间的字符串。

欢迎您的帮助。

php wordpress woocommerce orders email-notifications
1个回答
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文件中。它应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.