Im trying to add an additional sorting method by an attribute 'brand'
Its adding to the front end but I have no idea what the meta key would be for an attribute.
function theme_catalog_orderby( $catalog_orderby_options ) {
$catalog_orderby_options['brands'] = __( 'Sort by brands', 'textdomain' );
return $catalog_orderby_options;
}
add_filter( 'woocommerce_catalog_orderby', 'theme_catalog_orderby' );
/**
* Returns an array of arguments for ordering products based on the selected values.
*
* @uses woocommerce_get_catalog_ordering_args hook
* @return array
*/
function theme_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sale' == $orderby_value ) {
$args['meta_key'] = '_brands';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
}
return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'theme_get_catalog_ordering_args' );
Postman result
"attributes": [
{
"id": 2,
"name": "brands",
"position": 0,
"visible": true,
"variation": false,
"options": [
"Rice"
]
}
Tried
"attributes": [{"name": "brands"}]
Need to know how to target the attribute name. Thanks alot
Product attributes are saved in a serialized array under the post_meta table with meta_key _product_attributes. The problem is that your brands attribute is contained there with a prefix like pa_brands. This makes filtering directly with an SQL query somewhat hard.