php Woocommerce如何按产品分类展示产品

rur96b6h  于 2023-11-16  发布在  PHP
关注(0)|答案(6)|浏览(153)

我的一个客户想显示所有的woocommerce产品SKU
通常我使用以下代码显示产品。

$postArg = array('post_type'=>'product',
                            'post_status'=>'publish',
                            'posts_per_page'=>-1,
                            'orderby'=>'data',
                            'order'=>'DESC',

                    );

            $queryGetFiles = get_posts($postArg);

字符串
但现在我的客户想显示所有产品的产品SKU在前面。
1041-14、1041-12、1041-16、1041、2001、3501
所有产品都有不同的SKU值和显示,没有"-"字符
有人知道我该怎么做吗?

toiithl6

toiithl61#

试试这个

$postArg = array(
       'post_type'      => 'product',
       'post_status'    => 'publish',
       'posts_per_page' => -1,
       'meta_key'       => '_sku',
       'orderby'        => 'meta_value' // meta_value_num if ordered by intergers
       'order'          => 'DESC',
);

$queryGetFiles = get_posts($postArg);

字符串
Answered with help of this post

li9yvcax

li9yvcax2#

尝试将代码放在function.php中

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
    function am_woocommerce_catalog_orderby( $args ) {
        $args['meta_key'] = '_sku';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'desc'; 
        return $args;
    }

字符串

rkue9o1l

rkue9o1l3#

请试试这个。让我知道这是否完美...

/**
 * Adds the ability to sort products in the shop based on the SKU
 * Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
 */

function sv_add_sku_sorting( $args ) {

	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

	if ( 'sku' == $orderby_value ) {
		$args['orderby'] = 'meta_value';
    		$args['order'] = 'asc'; 
    		// ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
		$args['meta_key'] = '_sku';
	}

	return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );


function sv_sku_sorting_orderby( $sortby ) {
	$sortby['sku'] = 'Sort by SKU';
	// Change text above as desired; this shows in the sorting dropdown
	return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );

字符串

k2fxgqgv

k2fxgqgv4#

谢谢大家的支持。
我自己用下面的方法解决了这个问题。

$postArg = array('post_type'=>'product',
                            'post_status'=>'publish',
                            'posts_per_page'=>-1,
                            'orderby'=>'date',
                            'order'=>'DESC',
                            'meta_query' => array(
                                    array(
                                            'key' => '_sku',
                                            'value' => '-',
                                            'compare' => 'NOT LIKE'
                                    )
                                ),
                    );

字符串

8dtrkrch

8dtrkrch5#

似乎后来的woo-commerce版本在产品页面上增加了一个手动拖放排序,这覆盖了我们用来尝试和按顺序排序的任何功能。

add_action( 'woocommerce_product_query', 'custom_sort_products_by_sku' );

function custom_sort_products_by_sku( $query ) {
// Check if on a product category archive page
if ( ! is_admin() && is_product_category() && $query->is_main_query() ) {
    // Set the orderby parameter to 'meta_value' for SKU
    $query->set( 'orderby', 'meta_value' );
    // Set the meta key to '_sku' which is the SKU
    $query->set( 'meta_key', '_sku' );
    // Set the order parameter to 'ASC' for ascending order
    $query->set( 'order', 'ASC' );
}
}

// Add the 'Sort by SKU' option to the 
sorting dropdown
add_filter( 'woocommerce_catalog_orderby', 
'add_sort_by_sku_to_dropdown' );
function add_sort_by_sku_to_dropdown( $options ) {
// Add 'Sort by SKU' option
$options['sort_by_sku'] = __('Sort by SKU', 'woocommerce');
return $options;
}

// Handle the sorting logic for 'Sort by SKU'
add_action( 'woocommerce_product_query', 'sort_products_by_sku' );
function sort_products_by_sku( $query ) {
// Check if 'orderby' is set to 'sort_by_sku'
if ( isset( $_GET['orderby'] ) && 'sort_by_sku' === $_GET['orderby'] ) {
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'meta_key', '_sku' );
    $query->set( 'order', 'ASC' );
}
}

// Ensure the 'Sort by SKU' option works correctly with WooCommerce's session 
handling
add_filter( 'woocommerce_default_catalog_orderby_options', 'add_sort_by_sku_to_dropdown' );
add_filter( 'woocommerce_catalog_orderby', 'add_sort_by_sku_to_dropdown' );

字符串

wwwo4jvm

wwwo4jvm6#

我刚注册了回复。
我遇到了同样的问题,一个客户希望她的产品按SKU排序。上面的脚本按一般WordPress ID而不是产品SKU排序产品。下面是按SKU排序产品的内容:

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
    $args['meta_key'] = '_sku';
    $args['orderby'] = '_sku';
    $args['order'] = 'asc'; 
    return $args;
}

字符串

相关问题