dsznndq4912405 2017-04-22 20:26
浏览 70
已采纳

将产品说明添加到WooCommerce电子邮件通知中

How to add product description/content of single product page (not the short description) to WooCommerce new order email notification?

I need to know specific written description of my products as most of them are almost same.

  • 写回答

1条回答 默认 最新

  • dsbj66959 2017-04-23 01:17
    关注

    As you are targeting a specific email notification, first we need to get the Email ID to target the "New Order" email notification. The only way is to get it before and to set the value in a global variable.

    Then in a custom function hooked in woocommerce_order_item_meta_end action hook, we display the product description exclusively for New Order email notification.

    Here is that code:

     ## Tested on WooCommerce 2.6.x and 3.0+
    
    // Setting the email_is as a global variable
    add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
    function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
        $GLOBALS['email_id_str'] = $email->id;
    }
    
    // Displaying product description in new email notifications
    add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 4 );
    function product_description_in_new_email_notification( $item_id, $item, $order = null, $plain_text = false ){
    
        // Getting the email ID global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_id = $refNameGlobalsVar['email_id_str'];
    
        // If empty email ID we exit
        if(empty($email_id)) return;
    
        // Only for "New Order email notification"
        if ( 'new_order' == $email_id ) {
    
            if( version_compare( WC_VERSION, '3.0', '<' ) ) { 
                $product_id = $item['product_id']; // Get The product ID (for simple products)
                $product = wc_get_product($item['product_id']); 
            } else {
                $product = $item->get_product();
    
                if( $product->is_type('variation') ) {
                    $product = wc_get_product( $item->get_product_id() );
                }
            }
    
            // Get the Product description (WC version compatibility)
            if ( method_exists( $item['product'], 'get_description' ) ) {
                $product_description = $product->get_description(); // for WC 3.0+ (new)
            } else {
                $product_description = $product->post->post_content; // for WC 2.6.x or older
            }
    
            // Display the product description
            echo '<div class="product-description"><p>' . $product_description . '</p></div>';
        }
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    Code Update and Error explanations in woocommerce_order_item_meta_end action hook:

    PHP Warning for woocommerce_order_item_meta_end (Mike Joley)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探