【问题标题】:How to prevent hook running in WooCommerce email notifications如何防止在 WooCommerce 电子邮件通知中运行挂钩
【发布时间】:2022-01-08 04:45:08
【问题描述】:

我已经编写了这个函数来在 WooCommerce 的订单接收页面上显示类别和类别 ID。

但是,我希望禁止在 WooCommerce 订单电子邮件中显示此内容。有什么建议吗?

// Display order items product categories and ids
add_action( 'woocommerce_order_item_meta_end', 'display_custom_data_in_emails', 10, 4 );
function display_custom_data_in_emails( $item_id, $item, $order, $bool ) {
    // Get the product categories for this item
    $terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
  $term_ids = wp_get_post_terms( $item->get_product_id(), 'product_cat', array('fields' => 'ids') );
    
    // Output a coma separated string of product category names
    echo "<br><small>" . implode(', ', $terms) . "</small>";
      echo "<br><small>" . implode(', ', $term_ids) . "</small>";
    
}

}

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce email-notifications


    【解决方案1】:

    可以使用! is_wc_endpoint_url()判断是否是邮件通知

    所以你得到:

    function action_woocommerce_order_item_meta_end( $item_id, $item, $order, $plain_text ) {   
        // NOT on emails notifications
        if ( ! is_wc_endpoint_url() ) return;
        
        // Get the product categories for this item
        $terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
        $term_ids = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'ids' ) );
        
        // Output a coma separated string of product category names
        echo "<br><small>" . implode( ', ', $terms ) . "</small>";
        echo "<br><small>" . implode( ', ', $term_ids ) . "</small>";
    }
    add_action( 'woocommerce_order_item_meta_end', 'action_woocommerce_order_item_meta_end', 10, 4 );
    

    只定位收到的订单(谢谢)页面:

    if ( is_wc_endpoint_url( 'order-received' ) ) {
        // do something
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2017-01-05
      • 2019-08-15
      • 2020-09-07
      相关资源
      最近更新 更多