php - 如何在 WooCommerce 中创建有效的自定义产品查询?

标签 php wordpress woocommerce wordpress-theming

我在创建自己的自定义 WooCommerce 产品循环时遇到困难。

在此过程中,我遇到了一些问题,如果能够得到解答,这些问题将非常有帮助。

我目前正在处理这段代码:

ARCHIVE-PRODUCT.PHP(移入子主题 -> woocommerce/archive-product.php):

<?php
/**
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.4.0
 */

defined( 'ABSPATH' ) || exit;

get_header();

$display_count = $atts['per_page'];
echo $display_count; // NO value - How do I access this?
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo $page; // returns 1 - CORRECT;
$offset = ( $page - 1 ) * $display_count;
echo $offset; // returns 0 - CORRECT;
$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'posts_per_page'        => $atts['per_page'],
    'page'                  => $page,
    'offset'                => $offset,
    'orderby'               => $atts['orderby'],
    'order'                 => $atts['order']
); ?>

<?php
$loop = new WC_Product_Query( $args );
if ( $loop->have_posts() ) :

    // do_action( 'woocommerce_before_shop_loop' );

    woocommerce_product_loop_start();
?>

    <div id="product-list">

        <?php
        while ( $loop->have_posts() ) :

            $loop->the_post();

            wc_get_template_part( 'content', 'product' );

        endwhile;
        ?>

    </div>

    <?php
    woocommerce_product_loop_end();

    do_action( 'woocommerce_after_shop_loop' );

else :

    do_action( 'woocommerce_no_products_found' );

endif;

wp_reset_postdata();

do_action( 'woocommerce_after_main_content' );

do_action( 'woocommerce_sidebar' );

get_footer(); ?>

也许我的错误很明显。

在浏览我的代码时,我会突出显示我遇到但找不到答案的问题。

从一行开始:

$display_count = $atts['per_page'];
echo $display_count; // NO value - How do I access this?

据我了解,这些变量 ($atts['per_page'], $atts['order_by'], $atts['order' ]) 包含在此处:/includes/class-wc-shortcodes.php

但是,除非我弄错了,否则我只能看到为单个产品页面设置的这个属性?

我可以使用 $atts[] 中设置的变量吗?这也适用于在 $args 数组页面下方使用这些变量

如何设置它们以便使用它们?

这些值在哪里存储和更改?

在将自定义 AJAX 分页脚本写入 functions.php 文件时,这些变量在这里也很容易获得吗?如果没有,我该如何设置?

$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo $page; // returns 1 - CORRECT;

设置页面变量以帮助我跟踪我所在的页面。

$offset = ( $page - 1 ) * $display_count;
echo $offset; // returns 0 - CORRECT;

创建偏移量变量以放入 args 中,这样就可以知道有多少产品将被偏移,这在分页中会非常方便。

$loop = new WC_Product_Query( $args );

构建与提供的 $args 关联的产品循环。

这是最好的查询处理程序吗?我见过很多其他的,例如:

$loop = new WP_Query( $args );
$loop = wc_get_products( $args );

代码的尾端,我只是尝试使用尽可能多的 WooCommerce 基本模板结构来输出找到的产品。

编辑:理想情况下,它还会向我展示如何使用 WooCommerce 循环,因为我已经使用了一个附加到 WooCommerce 插件的插件来帮助我重新订购产品定制订单。我希望通过这个定制订购的产品列表。

最佳答案

遍历 WooCommerce 产品的基本查询

<ul class="products">
<?php
$args = array(
    'post_type'   => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1,  // -1 will get all the product. Specify positive integer value to get the number given number of product
    );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) : $the_query->the_post();

        // Get default product template
        wc_get_template_part( 'content', 'product' );

    endwhile;
} else {
    echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul>

关于php - 如何在 WooCommerce 中创建有效的自定义产品查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59954260/

相关文章:

php - wp.​​template for front end templating in WordPress how to write for each loop

mysql - 具有多个自定义帖子类型和多个日期列名称的复杂 meta_query 和 orderby

php - 将几个 WordPress 类别的内容加入到新的虚拟类别中

php - 如何在 WooCommerce 2.2+ 上手动安装翻译文件?

php - 如何在 WooCommerce 上的特定位置添加自定义结账字段

php - 使用 GD 调整大小和裁剪图像,同时保持纵横比

php - 使用 PHP 生成 NACHA 文件?

php - 显示评论表mysql中的多个值?

php - 从多个表中检索内容..可以吗?

php - 在 Woocommerce 单个产品页面中显示产品价格上方的自定义字段