2017-10-30 12 views
0

私のWooCommerceウェブショップでは、私はシャンプーや石鹸のような天然製品を販売しています。現在私は2つの製品しか持っていません。WooCommerce電子メール通知の製品IDに基づいてカスタムリンクテキストを追加

私は混同されることから人々を停止するために、購入された商品についてのいくつかの背景情報へのハイパーリンクを持つテキストの特定の文字列を追加したいと思います:

  • 彼らはパーム油から作られたシャンプーを購入する場合、私がしたいです私たちがシャンプーなどを作る方法についての背景情報を持つハイパーリンクを送ってください。
  • もし彼らが石鹸を購入すれば、石鹸についても同じことをしたいと思います。

ように、製品Aは、ハイパーリンクAに等しく、製品Bは、ハイパーリンクB.に等しい

これは私がこれまで持っているものです:

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

foreach ($order->get_items() as $item_id => $item) { 
    $product_name = $item['Palm Oil Shampoo']; // product name 
    $product_id = $order->get_item_meta($item_id, '_POShamp_05', true); // product ID 
    $product_description = get_post($product_id)->post_content; // Product description 
} 

をしかし、それは動作しません。

お客様の電子メール通知で、購入した製品に基づいて特定のリンクを含むカスタムテキストを追加するにはどうすればよいですか?今、あなたはこのカスタムで定義し、製品IDに基づいて、いくつかの顧客の電子メール通知にあなたのリンクされたテキストを表示することができ

foreach ($order->get_items() as $item_id => $item) { 
     $product_name = $item->get_name(); // The product name 
     $product_id = $item->get_product_id(); // The product ID 
     $product = $item->get_product(); // The WC_Product Object 
     $product_description = $product->get_description(); // The Product description 
    } 

+1

あなたの現在のコードで発生し、他に何が必要とされているかを説明してください、 –

答えて

0

あなたのコードがWoocommerce 3+について少しだけ時代遅れである、それはのようなものでなければなりませんあなたの製品ID、リンク、テキスト、タイトルを機能させることができます。また、多くの異なる製品を注文することができます。 WooCommerce 3+と作品でテスト

// Add custom text hyperlinks to Order items 
add_action('woocommerce_email_after_order_table', 'add_product_custom_link', 9, 4); 
function add_product_custom_link($order, $sent_to_admin, $plain_text, $email) 
{ 
    // Only for customer "Processing" and "Completed" Order email notifications 
    if(! ($email->id == 'customer_processing_order' || $email->id == 'customer_completed_order')) return; 

    // HERE (Below) define your product IDs, links, texts, title … 
    $product_id1 = 37; 
    $product_id2 = 53; 
    $link1 = home_url('/some-path/product-info1/'); 
    $link2 = home_url('/some-path/product-info1/'); 
    $text1 = __('Your linked text1'); 
    $text2 = __('Your linked text1'); 
    $title = __("Product Documentation Link"); 
    $instroduction = __("Some introduction text paragraph here, blabla bla blabla blablabla bla blabla. …"); 


    // Iterating through order "line items" 
    foreach ($order->get_items() as $item_id => $item) { 
     $product_id = $item->get_product_id(); // The product ID 

     // Set all the product IDs in this order in an array 
     $product_ids[$product_id] = $product_id; 
    } 

    $has_product = false; 

    // CSS style 
    $styles = '<style> 
     table.product-info{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; 
      color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;} 
     table.product-info th, table.product-info td{text-align: left; border-top-width: 4px; 
      color: #737373; border: 1px solid #e4e4e4; padding: 12px;} 
     table.product-info td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;} 
     </style> 
     '; 

    // HTML (start) 
    $html_output = '<h2>'.$title.'</h2> 
     <p>'.$instroduction.'</p> 
     <table class="product-info" cellspacing="0" cellpadding="6">'; 

    // Iterating through the product ids (when multiple products are in the order) 
    foreach ($product_ids as $value_id) { 
     // ==> HERE Replace links path and linked text by yours (for each product) 
     if ($product_id == $product_id1) { 
      // PRODUCT 1 
      $html_output .= '<tr><td><a href="' . $link1 . '">' . $text1 . '</a></td></tr>'; // HTML 
      $has_product = true; 
     } elseif ($product_id == $product_id2) { 
      // PRODUCT 2 
      $html_output .= '<tr><td><a href="' . $link2 . '">' . $text2 . '</a></td></tr>'; // HTML 
      $has_product = true; 
     } 
    } 

    if($has_product){ 
     $html_output .= '</table><br>'; // HTML (end) 

     // Output CSS and HTML 
     echo $styles . $html_output; 
    } 
} 

:ここ

はコードです。あなたが好きなものを取得します:

enter image description here

関連する問題