• Resolved polarone

    (@polarone)


    A few experiments. Might be useful to someone:
    I set out to display related products in a letter along with the ordered product. In short – we determine the ordered goods and their crossels and display no more than three and bring them to the hook. Still some work to do on the styles…

    More hooks are needed.

    function display_cross_sells_in_email($order) {
        echo '<p><strong>Crossels for goods in the order:</strong></p>';
        
        foreach ($order->get_items() as $item_id => $item) {
            $product_id = $item->get_product_id();
            $product = wc_get_product($product_id);
            
            // Getting crossels for the current product
            $cross_sells = $product->get_cross_sell_ids();
    
            if (!empty($cross_sells)) {
                // We limit the number of crossels to three
                $cross_sells = array_slice($cross_sells, 0, 3);
                
                echo '<table style="width: 100%; max-width: 600px;"><tr>';
                
                foreach ($cross_sells as $cross_sell_id) {
                    $cross_sell_product = wc_get_product($cross_sell_id);
                    $cross_sell_image = $cross_sell_product->get_image();
                    $cross_sell_name = $cross_sell_product->get_name();
                    $cross_sell_stock_status = $cross_sell_product->get_stock_status();
                    $cross_sell_price = wc_price($cross_sell_product->get_price());
                    $cross_sell_buy_button = sprintf(
                        '<a href="%s" class="button">%s</a>',
                        esc_url(add_query_arg('add-to-cart', $cross_sell_id, $site_url)), // We use the current site address and add the add-to-cart parameter
                        esc_html__('Buy', 'woocommerce')
                    );
                    
                    echo '<td style="width: 33.33%; text-align: center; padding: 10px; box-sizing: border-box;">';
                    echo $cross_sell_image;
                    echo '<p><strong>' . $cross_sell_name . '</strong></p>';
                    echo '<p><strong>Availability:</strong> ' . $cross_sell_stock_status . '</p>';
                    echo '<p><strong>Price:</strong> ' . $cross_sell_price . '</p>';
                    echo $cross_sell_buy_button;
                    echo '</td>';
                }
                
                echo '</tr></table>';
            }
        }
    }
    
    add_action('woocommerce_email_after_order_table', 'display_cross_sells_in_email', 10, 1);
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Crossels in the letter with the order’ is closed to new replies.