使用属性Woocommerce对产品列表进行排序

时间:2020-02-06 09:11:11

标签: php wordpress woocommerce

请帮助我对商店页面中的产品列表进行排序。我确实尝试使用此代码,但始终返回“找不到符合您选择的产品”。

add_filter('woocommerce_get_catalog_ordering_args', 'wh_catalog_ordering_args');
function wh_catalog_ordering_args($args) {
global $wp_query;
if (isset($_GET['orderby'])) {
	switch ($_GET['orderby']) {
		//for attribute/taxonomy=pa_color
		case 'pa_product-name' :
			$args['order'] = 'ASC';
			$args['meta_key'] = '_product_attributes';
			$args['orderby'] = 'meta_value';
			break;
	}
}
return $args;
}

add_filter('woocommerce_catalog_orderby', 'wh_catalog_orderby');

function wh_catalog_orderby($sortby) {
$sortby['pa_product-name'] = 'Sort by Product Name: A - Z';
return $sortby;
}


add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
function save_woocommerce_attr_to_meta( $post_id ) {
	// Get the attribute_names .. For each element get the index and the name of the attribute
	// Then use the index to get the corresponding submitted value from the attribute_values array.
foreach( $_REQUEST['attribute_names'] as $index => $value ) {
	update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] );
}
}

请帮助我

0 个答案:

没有答案
相关问题