datatables2ajax

Home » Snippets » datatables2ajax
0

Created with:

Visibility: 

public

Creator: mahdi

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
function wpturbo_get_product_list($args) {
    $categoryID = isset($args['category']) ? intval($args['category']) : 0;
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => $categoryID,
                'operator' => $categoryID ? 'IN' : 'EXISTS',
            ),
        ),
    );

    $query = new WP_Query($args);

    ob_start(); // Start output buffering
    ?>
    <table id="product-table" class="display">
        <thead>
            <tr>
                <th>Thumbnail</th>
                <th>Name</th>
                <th>Category</th>
                <th>ID</th>
                <th>SKU</th>
                <th>Rate</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($query->have_posts()) : $query->the_post(); ?>
                <?php
                $thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumbnail');
                $category = get_the_terms(get_the_ID(), 'product_cat');
                $id = get_the_ID();
                $sku = get_post_meta(get_the_ID(), '_sku', true);
                $rate = get_post_meta(get_the_ID(), '_rate', true);
                ?>
                <tr>
                    <td><?php echo $thumbnail; ?></td>
                    <td><?php the_title(); ?></td>
                    <td><?php echo isset($category[0]) ? $category[0]->name : ''; ?></td>
                    <td><?php echo $id; ?></td>
                    <td><?php echo $sku; ?></td>
                    <td><?php echo $rate; ?></td>
                </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
    <?php

    wp_reset_query();

    return ob_get_clean(); // Return buffered output
}
				

With this modification, the function now includes a select dropdown that allows the user to order the products by different options such as default, latest, low to high price, high to low price, best sell, average rating, and in stock. The selected option is determined based on the $orderby value. The query arguments are dynamically modified based on the selected option to perform the appropriate ordering.

To use the modified function and display the select dropdown, you can call it like this:

Replace 'your_category_id' with the ID of the desired category. The $orderby value will be retrieved from the URL query string using $_GET['orderby']. By default, if no orderby is provided in the URL, it will be set to "default".

This updated code allows the user to filter and order the product list in various ways, providing a more versatile and customizable user experience. Let me know if you have any more questions!

Register an account to save your snippets or go Pro to get more features.