代码之家  ›  专栏  ›  技术社区  ›  Ramesh S

如何在WordPress Woo commerce中获得所有分页产品

  •  0
  • Ramesh S  · 技术社区  · 6 年前

    也许这个问题已经问过了,但对我的问题没有帮助。

    我正在创建一个 API 文字出版社 项目。所以我想把 美国石油学会 用于获取所有带分页的产品。

    android/所有产品.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();
    ?>
    

    我有所有的产品,但我想显示与分页的产品。

    注意:所有的API都写在android文件夹中。

    提前谢谢。

    2 回复  |  直到 6 年前
        1
  •  0
  •   raju_odi    6 年前

    如果您想创建API,那么只需使用 Woocommerce

    {{your_url}}/wp-json/wc/v2/products
    

    -&燃气轮机;启用rest API

    然后给上面的url添加它,以便在具有私钥和私钥的授权的应用程序中使用。

    请查看此链接 woocommerce API's https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product

        2
  •  0
  •   Ramesh S    6 年前

    最后,我创建了一个代码并使用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)
    ?>
    
        3
  •  0
  •   Islam Roshan    5 年前

    if(isset( $_GET['page_num'] ) ) {
            $page_number = $_GET['page_num'];
    } else {
        $page_number = 2;
    }
        $args = array (
            'limit' => 3,
            'page' => $page_number,
        );
        $products = wc_get_products( $args );
        foreach( $products as $product ) {`enter code here`
          //your data here
          // e.g $product_name = $product->get_name();
        }
        <a href="?page_num=<?php echo ++$page_number; ?>">Next</a>