Okay
  Public Ticket #3840238
Show image product order in email
Closed

Comments

  •  3
    Hung started the conversation

    Please display the data for me about the product in each message, including the name, price, and quantity.
    add_filter('b2bking_quote_message_before_items', function($messagecart, $items, $user_id){    ob_start();    display_customer_order_history($user_id);    $messagecart .= ob_get_clean();    return $messagecart;
    }, 10, 3);
    function display_customer_order_history($items) {
        //Start building the table    echo "<div class='productorder'><table class='productorder__table'>";    echo '<thead><tr><th>STT</th><th>ẢNH SẢN PHẨM</th><th>TÊN SẢN PHẨM</th><th>SỐ LƯỢNG</th><th>ĐƠN GIÁ (VNĐ) </th><th>THÀNH TIỀN (VNĐ) </th></tr></thead><tbody>';    $number= 0;    foreach($items as $item => $values) {        $number++;        $product =  wc_get_product( $values['data']->get_id());
            $image = wp_get_attachment_image_src( get_post_thumbnail_id($product->get_id()), 'single-post-thumbnail' );        $title = $product->get_name(); // Product name        $price = $product->get_price(); // Product price        $quantity = $values['quantity']; // Quantity ordered
            echo '<tr>            <td>'.$number.'</td>
    <td><img src='.$image[0].' ></td>
    <td><a href='.get_permalink($product->get_id()).'>'.$title.'</a></td>
    <td>'.$price.'</td>
    <td>'.$quantity.' VNĐ</td>            <td>'.$price * $quantity.' VNĐ</td>  </tr>';
        }
        echo '</tbody></table></div>';
    }
    

    Attached files:  Screenshot.png

  •  2,361
    WebWizards replied

    Hi Hung,

    I've looked at your code and I notice you're trying to add product details including images to the quote messages. While your code structure is generally good, there are a couple of issues I can see:

    1. The filter hook parameters seem incorrect - `$items` should be passed to the display function but you're passing `$user_id` instead
    2. The HTML formatting needs some adjustment to display properly in emails

     

    I tried to improve the code, please try this version:

    add_filter('b2bking_quote_message_before_items', function($messagecart, $items, $user_id){
        ob_start();
        display_customer_order_history($items);
        $messagecart .= ob_get_clean();
        return $messagecart;
    }, 10, 3);
    
    function display_customer_order_history($items) {
        $output = '<div style="margin: 20px 0;"><table style="width: 100%; border-collapse: collapse;">';
        $output .= '<thead><tr style="background: #f8f8f8;"><th>No.</th><th>Image</th><th>Product</th><th>Price</th><th>Quantity</th><th>Total</th></tr></thead><tbody>';
        
        $number = 0;
        foreach($items as $item => $values) {
            $number++;
            $product = wc_get_product($values['data']->get_id());
            $image = wp_get_attachment_image_src(get_post_thumbnail_id($product->get_id()), 'thumbnail');
            
            $output .= '<tr style="border-bottom: 1px solid #eee;">';
            $output .= '<td>' . $number . '</td>';
            $output .= '<td><img src="' . $image[0] . '" style="max-width: 50px;"></td>';
            $output .= '<td>' . $product->get_name() . '</td>';
            $output .= '<td>' . wc_price($product->get_price()) . '</td>';
            $output .= '<td>' . $values['quantity'] . '</td>';
            $output .= '<td>' . wc_price($product->get_price() * $values['quantity']) . '</td>';
            $output .= '</tr>';
        }
        
        $output .= '</tbody></table></div>';
        echo $output;
    }

     

    Kind regards,

    Stefan