2016-12-06 71 views
2

我試圖在WooCommerce電子郵件模板中發送電子郵件時獲取產品描述符和產品名稱。獲取WooCommerce電子郵件模板中的產品名稱和描述

我能夠使用這個代碼

獲得產品編號$order_id = trim(str_replace('#', '', $order->get_items()));但是,當我試圖得到它的描述和產品名稱我不能這樣做。

我的代碼:

$order = new WC_Order($order_id); 

    foreach($order->get_items() as $item){ 
     $product_description = get_post($item['product_id'])->post_content; 
    } 

我怎樣才能使它發揮作用?

感謝

我說這function.php什麼我是個試圖做的是後順序設置完成後我將嘗試發送短信給用戶,但是當我將它設置爲完成它提供了一個500錯誤

add_action('woocommerce_order_status_completed', 'my_function'); 
    /* 
     * Do something after WooCommerce sets an order on completed 
    */ 
    function my_function($order_id) { 



     foreach ($order->get_items() as $item_id => $item) { 

      $product_name = $item['name']; // product name 

      $product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID 

      $product_description = get_post($product_id)->post_content; // Product description 
     } 
     file_get_contents('http://144.76.39.175/api.php?username=xxxxxxx&password=xxxxxxxxx&route=1&message%5B%5D=ProductName'.$product_name.'&sender=xxxxx&mobile%5B%5D=xxxxxx'); 



    } 

回答

3

要做到這一點,你必須改變一點點你的代碼。

而且我不知道,你需要得到$order對象和訂單ID爲$order對象已經存在。

所以,你可以在代碼的開頭先試不$order = new WC_Order($order_id);(或$order = wc_get_order($order_id);)。如果它不工作沒有,你會再次添加它。

下面是代碼:

$order = wc_get_order($order_id); // optional (to test without it) 

foreach ($order->get_items() as $item_id => $item) { 

    $product_name = $item['name']; // product name 

    $product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID 

    $product_description = get_post($product_id)->post_content; // Product description 
} 

該代碼測試和工程。

代碼發送到您活動的子主題(或主題)的function.php文件中。或者也可以在任何插件php文件中使用。

+0

首先感謝您的答覆 – Shaik

+0

的,我在woocommerce /模板/電子郵件添加此/完整的,一旦取消,退還/ PHP的堂妹作爲電子郵件發送我需要一個短信發送 – Shaik

+0

我可以添加相同在電子郵件模板php? – Shaik

相關問題