WordPress

woocommerce offtopic – wydruk szczegółów zamówienia i krótkiego opisu produktów

  • 9 marca, 2024
  • 3 min read
woocommerce offtopic – wydruk szczegółów zamówienia i krótkiego opisu produktów


Próbuję utworzyć przycisk wyświetlający szczegóły zamówienia. Nazwy produktów i krótki opis (na stronie wszystkie akcesoria są pokazane w formie tabeli w krótkim opisie) Problem polega na tym, że gdy próbuję dodać krótki opis każdego produktu, strona edycji zamówienia zawiesza się lub przycisk otwiera menu zamów ponownie adres URL To jest kod, który działa świetnie bez nieudanej próby dodania krótkiego opisu.

Muszę znaleźć sposób na dodanie linii do funkcji get_order_details, która bez problemu dołączy także krótki opis.

add_action('add_meta_boxes', 'add_meta_boxesws');
function add_meta_boxesws()
{
    add_meta_box('custom_order_meta_box', __('print order details'), 'custom_metabox_content', 'shop_order', 'normal', 'default');
}

function custom_metabox_content()
{
    $post_id = isset($_GET['post']) ? $_GET['post'] : false;
    if (!$post_id) return; // Exit

    $value = "abc";
    ?>
    <p><a href="?post=<?php echo $post_id; ?>&action=edit&abc=<?php echo $value; ?>" class="button" id="print-order"><?php _e('print'); ?></a></p>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var printButton = document.getElementById('print-order');
            if (printButton) {
                printButton.addEventListener('click', function (e) {
                    e.preventDefault();
                    printOrderDetails();
                });
            }

            function printOrderDetails() {
                var orderDetails="<?php echo get_order_details($post_id); ?>";
                if (orderDetails) {
                    var printWindow = window.open('', '_blank');
                    printWindow.document.write('<html><head><title>Order Details</title></head><body>');
                    printWindow.document.write('<h1>Order Details</h1>');
                    printWindow.document.write(orderDetails);
                    printWindow.document.write('</body></html>');
                    printWindow.document.close();
                    printWindow.print();
                } else {
                    alert('Error fetching order details.');
                }
            }
        });
    </script>
    <?php
}

function get_order_details($order_id)
{
    // Check if WooCommerce is active
    if (class_exists('WooCommerce')) {
        // Get the order object
        $order = wc_get_order($order_id);

        // Check if the order exists
        if ($order) {
            // Get the total cost of the order
            $total_cost = $order->get_total();

            // Get the list of products in the order
            $products = $order->get_items();

            // Prepare the order details string
            $order_details = "Order ID: " . $order_id . "<br>";
            $order_details .= "Total Cost: " . wc_price($total_cost) . "<br>";
            $order_details .= "Products:<br>";

            // Loop through each product and add its details to the string
            foreach ($products as $product) {
                $product_name = $product->get_name();
                $order_details .= "- " . $product_name . "<br>";
            }           
            $x = new WC_Order($order_id);

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

    // Return an empty string if WooCommerce is not active or the order is not found
    return '';
}


Źródło

Warto przeczytać!  Paginacja nie działa na statycznej stronie głównej