WooCommerce 从产品名称或 SKU 搜索客户订单

问题描述 投票:0回答:2

我正在尝试在 WordPress 中创建一个功能以部署在 WooCommerce 中,以便客户可以按 SKU 或名称查找他们购买的文章。 问题:无法正常工作,因为无论查找哪个 SKU,但都会显示相同的结果。除此之外,结果购买的文章数量始终为 1。

function search_orders_by_sku_or_product_name_form() {
    ob_start();
    ?>
    <form method="post" action="">
        <input type="text" name="order_search" placeholder="enter SKU o name">
        <input type="submit" value="Search">
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('order_search_form', 'search_orders_by_sku_or_product_name_form');

function search_orders_by_sku_or_product_name_result() {
    if (isset($_POST['order_search']) && !empty($_POST['order_search'])) {
        $search_term = sanitize_text_field($_POST['order_search']);
        $user_id = get_current_user_id();

        $orders = wc_get_orders(array(
            'customer' => $user_id,
            'limit' => -1,
        ));

        $found_orders = array();

        foreach ($orders as $order) {
            $order_items = $order->get_items();

            foreach ($order_items as $item) {
                $product = $item->get_product();
                if (!$product) {
                    continue;
                }

                $sku = $product->get_sku();
                $name = $product->get_name();

                if ($sku && $name && (strpos($sku, $search_term) === 0 || strpos($name, $search_term) === 0)) {
                    $found_orders[] = $order;
                    break;
                }
            }
        }

        if (!empty($found_orders)) {
            echo '<table>';
            echo '<tr><th>Order</th><th>Status</th><th>SKU</th><th>Name</th><th>Quantity</th><th>Date</th></tr>';
            foreach ($found_orders as $order) {
                $order_data = $order->get_data();
                $order_date = $order->get_date_created();
                $formatted_date = $order_date->format('d/m/Y H:i');
                $order_status = wc_get_order_status_name($order_data['status']);

                echo '<tr>';
                echo '<td>#' . $order_data['id'] . '</td>';
                echo '<td>' . $order_status . '</td>';
                echo '<td>' . $sku . '</td>';
                echo '<td>' . $name . '</td>';
                echo '<td>' . $item->get_quantity() . '</td>';
                echo '<td>' . $formatted_date . '&nbsp;Hs.</td>';
                echo '</tr>';
            }
            echo '</table>';
        } else {
            echo 'Sorry no results.';
        }
    }
}

add_shortcode('order_search_result', 'search_orders_by_sku_or_product_name_result');

关于如何使其发挥作用有任何提示吗?

php wordpress woocommerce shortcode orders
2个回答
0
投票

尝试使用此代码并检查:

function search_orders_by_sku_or_product_name_form() {
    ob_start();
    ?>
    <form method="post" action="">
        <input type="text" name="order_search" placeholder="enter SKU o name">
        <input type="submit" value="Search">
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('order_search_form', 'search_orders_by_sku_or_product_name_form');

function search_orders_by_sku_or_product_name_result() {
    if (isset($_POST['order_search']) && !empty($_POST['order_search'])) {
        $search_term = sanitize_text_field($_POST['order_search']);
        $user_id = get_current_user_id();

        $orders = wc_get_orders(array(
            'customer' => $user_id,
            'limit' => -1,
        ));

        $found_orders = array();

        foreach ($orders as $order) {
            $order_items = $order->get_items();

            foreach ($order_items as $item) {
                $product = $item->get_product();
                if (!$product) {
                    continue;
                }

                $sku = $product->get_sku();
                $name = $product->get_name();
                if ( $sku && $name && (strpos($sku, $search_term) === 0 || strpos($name, $search_term) === 0) ) {
                    $found_orders[] = $order;
                    break;
                }
            }
        }

        if (!empty($found_orders)) {
            echo '<table>';
            echo '<tr><th>Order</th><th>Status</th><th>SKU</th><th>Name</th><th>Quantity</th><th>Date</th></tr>';
            foreach ($found_orders as $order) {
                $order_data = $order->get_data();
                $order_items = $order->get_items();
                $order_date = $order->get_date_created();
                $formatted_date = $order_date->format('d/m/Y H:i');
                $order_status = wc_get_order_status_name($order_data['status']);
                foreach ($order_items as $item) {
                    $product = $item->get_product();
                    echo '<tr>';
                    echo '<td>#' . $order_data['id'] . '</td>';
                    echo '<td>' . $order_status . '</td>';
                    echo '<td>' . $product->get_sku() . '</td>';
                    echo '<td>' . $product->get_name() . '</td>';
                    echo '<td>' . $item->get_quantity() . '</td>';
                    echo '<td>' . $formatted_date . '&nbsp;Hs.</td>';
                    echo '</tr>';
                }
            }
            echo '</table>';
        } else {
            echo 'Sorry no results.';
        }
    }
}

add_shortcode('order_search_result', 'search_orders_by_sku_or_product_name_result');

0
投票

尝试将您的第二个功能替换为以下功能,这将使您能够获得具有正确数量,产品名称和SKU的客户订单(您的第一个功能保持不变)

function search_orders_by_sku_or_product_name_result() {
    if (isset($_POST['order_search']) && !empty($_POST['order_search'])) {
        $search_term = sanitize_text_field($_POST['order_search']);

        $orders = wc_get_orders( array(
            'limit'    => -1,
            'customer' => get_current_user_id(),
        ) );

        $found_orders = array();

        if( $orders ) {
            foreach ( $orders as $order) {
                foreach ( $order->get_items() as $item ) {
                    $product = $item->get_product();
                    $sku     = $product->get_sku();
                    $name    = $product->get_name();
                    if ( $sku && $name && ( strpos($sku, $search_term) !== false || strpos($name, $search_term) !== false ) ) {
                        $found_orders[] = (object) array(
                            'number' => $order->get_order_number(),
                            'status' => wc_get_order_status_name($order->get_status()),
                            'sku'    => $sku,
                            'name'   => $name,
                            'qty'    => $item->get_quantity(),
                            'date'   => $order->get_date_created()->format('d/m/Y H:i'),
                        );
                        break;
                    }
                }
            }
        }

        if ( ! empty($found_orders) ) {
            echo '<table><tr><th>Order</th><th>Status</th><th>SKU</th><th>Name</th><th>Quantity</th><th>Date</th></tr>';

            foreach ( $found_orders as $order_data ) {
                echo '<tr>
                <td>#' . $order_data->number . '</td>
                <td>'  . $order_data->status . '</td>
                <td>'  . $order_data->sku . '</td>
                <td>'  . $order_data->name . '</td>
                <td>'  . $order_data->qty . '</td>
                <td>'  . $order_data->date . '&nbsp;Hs.</td>
                </tr>';
            }
            echo '</table>';
        } else {
            echo 'Sorry no results.';
        }
    }
}

add_shortcode('order_search_result', 'search_orders_by_sku_or_product_name_result');

代码位于子主题的functions.php 文件中(或插件中)。经过测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.