php WooCommerce - 按属性列出产品(多个值),例如:[woo_products_by_attributes attribute =“color”values =“red,black”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WooCommerce - 按属性列出产品(多个值),例如:[woo_products_by_attributes attribute =“color”values =“red,black”相关的知识,希望对你有一定的参考价值。
<?php
/**
* Plugin Name: WooCommerce - List Products by Attributes
* Plugin URI: http://www.remicorson.com/list-woocommerce-products-by-attributes/
* Description: List WooCommerce products by attributes using a shortcode, ex: [woo_products_by_attributes attribute="colour" values="red,black" per_page="5"]
* Version: 1.0
* Author: Remi Corson
* Author URI: http://remicorson.com
* Requires at least: 3.5
* Tested up to: 3.5
*
* Text Domain: -
* Domain Path: -
*
*/
/*
* List WooCommerce Products by attributes
*
* ex: [woo_products_by_attributes attribute="colour" values="red,black" per_page="5"]
*/
function woo_products_by_attributes_shortcode( $atts, $content = null ) {
global $woocommerce, $woocommerce_loop;
// Get attribuets
extract(shortcode_atts(array(
'attribute' => '',
'values' => '',
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'desc',
), $atts));
if ( ! $attribute ) return;
// Default ordering args
$ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderby, $order );
// Define Query Arguments
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => $per_page,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'pa_' . $attribute,
'terms' => explode(",",$values),
'field' => 'slug',
'operator' => 'IN'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
add_shortcode("woo_products_by_attributes", "woo_products_by_attributes_shortcode");
以上是关于php WooCommerce - 按属性列出产品(多个值),例如:[woo_products_by_attributes attribute =“color”values =“red,black”的主要内容,如果未能解决你的问题,请参考以下文章
php WooCommerce |按名称添加按商店归档排序
Woocommerce 按属性过滤产品添加复选框
php 按类别筛选搜索 - woocommerce产品
php WooCommerce搜索产品按类别下拉列表
php WooCommerce |按名称(asc)添加到商店设置
php facetwp重新添加woocommerce post子句按价格排序