Php 基于装运区域和产品类别的Woocommerce电子邮件

Php 基于装运区域和产品类别的Woocommerce电子邮件,php,wordpress,function,email,woocommerce,Php,Wordpress,Function,Email,Woocommerce,我已设法获得一些基于付款方式的代码(取自现有答案代码),即: add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 ); function add_order_email_instructions( $order, $sent_to_admin ) { if ( ! $sent_to_admin ) { if ( 'cod' == $order->

我已设法获得一些基于付款方式的代码(取自现有答案代码),即:

add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );

function add_order_email_instructions( $order, $sent_to_admin ) {

  if ( ! $sent_to_admin ) {

    if ( 'cod' == $order->payment_method ){
      // cash on delivery method
      echo '<p><strong>Please note: </strong>Your repair is currently pending acceptance from your local technician. A member of our team will contact you shortly.</p>';
  }
        elseif( $product_category == 'unlocking'){
        // unlocking email
        echo '<p>Your device should be unlocked within 48 hours, if your device requires an unlock code we will be in touch.</p>';
    }
      else {
      // other methods (ie credit card)
      echo '<p><strong>Instructions:</strong> Please ensure your device is packaged securely and send it to the following address:</p><br><br><p><strong>Please note; if using our Unlocking service, you do NOT need to send us your device.</strong></p>';
    }
  }
}
add_action('woocommerce_email_前订单_表','add_order_email_说明',10,2);
功能添加\订单\电子邮件\指示($order$发送\给\管理员){
如果(!$已发送给管理员){
如果('cod'=$order->付款方式){
//货到付款法
echo'请注意:您的维修目前正在等待您当地技术人员的验收。我们团队的一名成员将很快与您联系。

; } elseif($product_category==‘解锁’){ //解锁电子邮件 echo'您的设备应在48小时内解锁,如果您的设备需要解锁码,我们将与您联系。

; } 否则{ //其他方法(如信用卡) echo'说明:请确保您的设备包装安全,并将其发送至以下地址:



请注意;如果使用我们的解锁服务,您无需向我们发送您的设备。; } } }
我假设我使用的
$product\u category
不正确,需要一些参考。我尝试使用“”回答代码来添加装运区,但在我的案例中似乎不起作用


如果您有任何建议,我们将不胜感激。

要检查订单中的特定产品类别,您需要循环查看订单中的项目,并检查每个项目是否属于您想要的类别。这是您的函数,已修改,然后是第二个函数,用于检查是否存在特定的产品类别

add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );

function add_order_email_instructions( $order, $sent_to_admin ) {

  if ( ! $sent_to_admin ) {

    if ( 'cod' == $order->payment_method ){
      // cash on delivery method
      echo '<p><strong>Please note: </strong>Your repair is currently pending acceptance from your local technician. A member of our team will contact you shortly.</p>';
    }
    else if( is_category_in_order($order, 'unlocking') ){
        // unlocking email
        echo '<p>Your device should be unlocked within 48 hours, if your device requires an unlock code we will be in touch.</p>';
    }
    else {
      // other methods (ie credit card)
      echo '<p><strong>Instructions:</strong> Please ensure your device is packaged securely and send it to the following address:</p><br><br><p><strong>Please note; if using our Unlocking service, you do NOT need to send us your device.</strong></p>';
    }
  }
}

/* Check if order contains a product from a specific category */

function is_category_in_order($order, $target_category) {
      // initialize variable for if category exists in order
    $category_present = false;
      // get the items array from the order object
    $items = $order->get_items();

      // loop through the items in the order
    foreach ( $items as $item ) {
          // get the product ID of the current item
        $pid = $item->get_product_id();

          // check if product has target category
        if ( has_term( $target_category, 'product_cat', $pid ) ) {
              // if so, set our category exists in order variable to true
            $category_present = true;
            break;
        }
    }
    return $category_present;
}
add_action('woocommerce_email_前订单_表','add_order_email_说明',10,2);
功能添加\订单\电子邮件\指示($order$发送\给\管理员){
如果(!$已发送给管理员){
如果('cod'=$order->付款方式){
//货到付款法
echo'请注意:您的维修目前正在等待您当地技术人员的验收。我们团队的一名成员将很快与您联系。

; } else if(类别是否符合顺序($order,'unlocking')){ //解锁电子邮件 echo'您的设备应在48小时内解锁,如果您的设备需要解锁码,我们将与您联系。

; } 否则{ //其他方法(如信用卡) echo'说明:请确保您的设备包装安全,并将其发送至以下地址:



请注意;如果使用我们的解锁服务,您无需向我们发送您的设备。; } } } /*检查订单是否包含特定类别的产品*/ 函数是按顺序排列的类别($order,$target\u category){ //如果类别按顺序存在,则初始化变量 $category_present=假; //从order对象获取items数组 $items=$order->get_items(); //循环浏览顺序中的项目 foreach($items作为$item){ //获取当前项目的产品ID $pid=$item->get_product_id(); //检查产品是否有目标类别 if(有术语($target\U category,$pid)){ //如果是这样,请将我们的类别exists in order变量设置为true $category_present=真; 打破 } } 返回$category_present; }

我认为您忘记编写超链接以供参考。如果您没有对问题进行更改,请不要在问题中添加堆栈溢出中的现有代码…始终添加您在问题中使用的代码段链接(即使您进行了一些更改)。同时尝试澄清你的问题编辑它…令人惊讶的是,这已经解决了这个问题,解释当然帮助我在这里学习。