【问题标题】:Display Shipping address2 in a text on WooCommerce email notifications在 WooCommerce 电子邮件通知的文本中显示送货地址2
【发布时间】:2019-04-09 17:16:14
【问题描述】:

在 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>';
    }
}

它不起作用。我想将此字段值转换为&lt;strong&gt; html标签之间的字符串。

欢迎您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案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_Orderget_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 文件中。它应该可以工作。

    【讨论】:

    • 酷!非常感谢!!好多了 !!您能否也将其添加到已完成订单邮件中的相同函数“if ($email->id == 'customer_completed_order') {}”中?
    • @Carlos Claro!代码更新,以处理客户处理和完成订单通知
    • 早上好,我尝试了代码,我得到了这个错误:语法错误,第 5 行的意外 T_IF if ($ coupon_code = $ order-> get_shipping_address_2 ()) {
    • @CarlosGomez 更新了……抱歉,第一个 IF 语句(第 5 行)中缺少右圆括号
    • 谢谢!如果 shipping_address_2 字段不为空,我只需要在邮件中留下文本,您能帮帮我吗?
    猜你喜欢
    • 2019-08-23
    • 2016-12-20
    • 1970-01-01
    • 2021-11-06
    • 2021-05-05
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多