Письма Woocommerce в зависимости от зоны доставки и категории продукта - PullRequest
0 голосов
/ 26 сентября 2019

Мне удалось получить код, основанный на методах оплаты, работающих (взят из существующего кода ответа) , что:

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 на основе зоны ответа " , чтобы добавить зону доставки, однако в моем случае это не сработало.

Любой совет будет принята с благодарностью.

1 Ответ

0 голосов
/ 27 сентября 2019

Чтобы проверить конкретную категорию товара в заказе, вам нужно будет пройтись по пунктам в заказе и проверить каждый товар на нужную категорию.Вот ваша функция, измененная, за которой следует вторая функция, которая выполняет проверку наличия определенной категории продукта.

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

Источник 2-й функции и дополнительная информация

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...