检查 Woocommerce 中是否已选中“运送到不同地址”

Neo*_*yte 4 php wordpress orders email-notifications woocommerce

在 WwooCommerce 中,我试图将船舶添加到我的管理电子邮件中的不同地址信息。

如何检查是否选中了从结帐页面运送到不同地址的复选框?

我尝试使用:

$ship_to_different_address = get_option( 'woocommerce_ship_to_destination' ) === 'shipping' ? 1 : 0;

if($ship_to_different_address == 1):
 //the additional email text here
endif;
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用。有任何想法吗?

Loi*_*tec 8

最好的方法可能是模拟它,比较订单的帐单和送货地址。在大多数可用的相关电子邮件通知挂钩中,该$order对象作为参数包含在内。

这是一个将此函数挂在woocommerce_email_order_details动作钩子中的示例,它会显示不同的内容,具体取决于:

add_action( 'woocommerce_email_order_details', 'custom_content_email_order_details', 10, 4 );
function custom_content_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
    // Only for "New Order" and admin email notification
    if ( 'new_order' != $email->id && ! $sent_to_admin ) return;

    // Displaying something related
    if( $order->get_billing_address_1() != $order->get_shipping_address_1() ) {
        echo '<p style="color:red;">Different billing and shipping addresses<p>';
    } else {
        echo '<p style="color:green;">Same billing and shipping addresses<p>';
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已在 WooCommerce 3.1+ 中测试并有效

您还可以在此代码中使用(具有不同的优先级)以下任何挂钩:
- woocommerce_email_before_order_table
- woocommerce_email_after_order_table
- woocommerce_email_order_meta
-woocommerce_email_customer_details


Neo*_*yte 5

啊哈..我们可以检查是否$_POST['ship_to_different_address']设置了..