【问题标题】:Woocommerce Emails based on Shipping Zone and Product Category基于运输区域和产品类别的 Woocommerce 电子邮件
【发布时间】:2019-09-26 13:17:59
【问题描述】:

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

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

我假设我对$product_category 的使用不正确,需要一些参考。我尝试使用 "WooCommerce email based on shipping zone" 回答代码来添加运输区域,但它似乎不适用于我的情况。

任何建议将不胜感激。

【问题讨论】:

  • 我想你忘了写超链接以供参考。
  • 如果您不对其进行更改,请不要在您的问题中添加来自 Stack Overflow 的现有代码...始终添加您在问题中使用的代码 sn-ps 链接(即使您做了一些改动)。也试着澄清你的问题编辑它......

标签: php wordpress function email woocommerce


【解决方案1】:

要检查订单中的特定产品类别,您需要遍历订单中的商品并检查每个商品的所需类别。这是您的函数,已修改,然后是第二个函数,用于检查是否存在特定产品类别。

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

Source of 2nd function & more info

【讨论】:

  • 太棒了,这已经解决了问题,并且解释肯定有助于我在这里学习。
猜你喜欢
  • 2018-06-26
  • 1970-01-01
  • 2016-07-05
  • 2019-05-30
  • 2022-01-02
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多