在商店页面上显示所有产品和变体,同时保持分页 [英] Display all products and variations on the shop page, while maintaining pagination

查看:102
本文介绍了在商店页面上显示所有产品和变体,同时保持分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示每个产品,如果它是可变产品(例如多种颜色),则在商店页面上显示每个变体.

I need to display every product, and in case it's a variable product (e.g. multiple colors), each variation on the shop page.

首先,我编写了自己的查询来检索所有包含产品的变体:

At first, I've wrote my own query to retrieve all variations if the product had them:

$product = get_product();
global $variable_id;
global $color;
if ($product->product_type == "variable") {
    $variations = $product->get_available_variations();
    foreach ($variations as $variation) {
        $color = $variation["attributes"]["attribute_pa_colour"];
        $variable_id = $variation['variation_id'];
        wc_get_template_part('content', 'product');
    }
} else {
    $variable_id = null;
    $color = null;
    wc_get_template_part('content', 'product');
}

我为变体设置了全局参数,然后成功地在另一个模板文件中获得了正确的图像.

I set the global parameter for the variation and got the right image afterwards in another template file with success.

此解决方案的问题是分页被破坏(1个产品出现多次).

The problem with this solution is that the pagination is broken (1 product appears multiple times).

因此,我需要实现的是操纵主产品查询以选择所有产品,具有变化的产品必须针对每个具有正确图像链接和变化值的变化出现一次(我也需要主产品的信息链接到正确的页面). 这可以通过基于post_parent的同一产品表上的Left Join来实现.

So, what I need to achieve is manipulating the main product query to select all products, where those who have variations must appear once for each variation with the correct image link and variation value (I need the info of the main product aswell to link to the correct page). This could be achieved by a Left Join on the same products table, based on post_parent.

这里的关键是保持默认的woocommerce行为.这是用于检索产品的woocommerce功能:

The key here is to keep the default woocommerce behavior. This is the woocommerce function to retrieve the products:

private function query_products( $args ) {
    // Set base query arguments
    $query_args = array(
        'fields'      => 'ids',
        'post_type'   => 'product',
        'post_status' => 'publish',
        'meta_query'  => array(),
    );
    if ( ! empty( $args['type'] ) ) {
        $types = explode( ',', $args['type'] );
        $query_args['tax_query'] = array(
            array(
                'taxonomy' => 'product_type',
                'field'    => 'slug',
                'terms'    => $types,
            ),
        );
        unset( $args['type'] );
    }

    // Filter products by category
    if ( ! empty( $args['category'] ) ) {
        $query_args['product_cat'] = $args['category'];
    }

    // Filter by specific sku
    if ( ! empty( $args['sku'] ) ) {
        if ( ! is_array( $query_args['meta_query'] ) ) {
            $query_args['meta_query'] = array();
        }
        $query_args['meta_query'][] = array(
            'key'     => '_sku',
            'value'   => $args['sku'],
            'compare' => '='
        );
        $query_args['post_type'] = array( 'product', 'product_variation' );
    }
    $query_args = $this->merge_query_args( $query_args, $args );
    return new WP_Query( $query_args );
}

因此,基本上,我需要找到一种方法来覆盖此函数,以便返回一个WP_Query对象,其中包含所有简单"乘积以及所有变量"乘积的每个变体的乘积,同时所有参数都随$args继续工作.

So basically, I need to find a way to override this function, so that a WP_Query object is returned containing all 'simple' products and a product for each variation of all 'variable' products, while all parameters passed with $args keep working.

推荐答案

我找到了一个解决方案:"Woocommerce Show Single Variations"插件: https://iconicwp.com/products/woocommerce-show-single-variations/

I have found a solution: "Woocommerce Show Single Variations" plugin: https://iconicwp.com/products/woocommerce-show-single-variations/

备注:如果您已经有类别,则必须删除并重新添加它们.而且,它仍然不适用于简码,尽管作者正在研究它.

Remark: If you already have categories, you have to remove and re-add them. Also, it still does not work for shortcodes, the author is working on it though.

已更新:更改了指向插件的链接

Updated: Changed the link to the plugin

这篇关于在商店页面上显示所有产品和变体,同时保持分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆