PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to get all products with pagination in WordPress Woo commerce

 May 09, 2022     pagination, product, wordpress     No comments   

Issue

Maybe this questions already asked but that not helpful for my question.

Am creating a API for my WordPress project. So i want to send API for to get all products with pagination.

I get products list using this code:

android/all_products.php

<?php
require_once('../wp-load.php');
$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 10
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
    global $product;
    echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;

wp_reset_query();
?>

I got all products, but i want to show products with pagination.

Note: all the API's are written in inside android folder.

Thanks in advance.


Solution

Finally i creating a code and send the date using API.

 <?php
    require_once('../wp-load.php');
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1
    );

$brand_product_list = new WP_Query( $args);
    $pagination_count = 1;       
    while($brand_product_list->have_posts()) : $brand_product_list->the_post(); 

        $pagination_count++;

    endwhile; wp_reset_query();
//echo'<pre>';print_r($pagination_count/12); exit;
wp_reset_query();   
?>

<?php
    $pagination = round($pagination_count/12);
    for($i=1;$i<=$pagination;$i++)
    {
        $pagination_no[] = $i;
    ?>
    <!-- <a class="product-category-view-all" href="?pagination=<?php echo $i; ?>"><?php echo $i; ?></a> -->
    <?php } ?>

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : $_GET['pagination'];

    $args = array(
        'post_type' => 'product',
        'paged' => $paged,
        'posts_per_page' => 12,
    );
    $wp_query = new WP_Query($args);

    while($wp_query->have_posts()) : $wp_query->the_post(); 

        $product_data = wc_get_product( $post->ID );
        if(!empty(get_the_post_thumbnail_url($product_data->get_id())))
        {
            $img = get_the_post_thumbnail_url($product_data->get_id());
        }
        else
        {
            $img = "";
        }

        $product_list[] = array(
            'product_id' => $product_data->get_id(),
            'product_name' => $product_data->get_title(),
            'product_regular_price' => $product_data->get_regular_price(),
            'product_sale_price' => $product_data->get_sale_price(),
            'product_price' => $product_data->get_price(),
            'img' => $img,
            'rating' => $product_data->get_average_rating(),
            'stock_quantity' => $product_data->get_stock_quantity(),
            'stock' => $product_data->is_in_stock(),
        );
    endwhile; wp_reset_query();
    $data[] = array(
        'pagination' => $pagination_no,
        'product_list' => $product_list
    );

    //echo json_encode($peoduct_list, $pagination)
    echo json_encode($data)
?>


Answered By - Ramesh S
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing