وردپرس

Add Sort by Featured | WordPress.org


Hello,

I’m adding a sort by featured in the products search results but I have a problem.

This is my code:

add_filter( ‘woocommerce_catalog_orderby’, ‘hugo_boss_catalog_orderby’ );
function hugo_boss_catalog_orderby( $orderby ) {
// Add a new sorting option called “Featured” to the dropdown
$orderby[‘featured’] = __(‘Featured’, ‘woocommerce’);

// Return the modified sorting options
return $orderby;

}

add_action( ‘woocommerce_product_query’, ‘obama_trump_product_query’ );
function obama_trump_product_query( $q ) {
// Check if not in admin panel and if the orderby parameter is set to “featured”
if ( ! is_admin() && isset($_GET[‘orderby’]) && ‘featured’ === esc_attr($_GET[‘orderby’]) ) {
// Get the existing tax queries
$tax_query = $q->get(‘tax_query’);

// Add a new tax query to filter by product visibility term “featured”
$tax_query[] = array(
‘taxonomy’ => ‘product_visibility’,
‘field’ => ‘name’,
‘terms’ => ‘featured’,
‘operator’ => ‘IN’, // Include featured products
);

// Merge all tax queries with ‘relation’ set to ‘OR’
$tax_query[‘relation’] = ‘OR’;

// Add another tax query to exclude products that are marked as “featured”
$tax_query[] = array(
‘taxonomy’ => ‘product_visibility’,
‘field’ => ‘name’,
‘terms’ => ‘featured’,
‘operator’ => ‘NOT IN’, // Exclude featured products
);

// Set the modified tax query
$q->set( ‘tax_query’, $tax_query );

// Set the order as DESC (or ASC if needed)
$q->set( ‘order’, ‘DESC’ );
}

}

unfortunately it only returns ONLY featured products, I tried adding logics to show both featured and non featured but it shows either unsorted products or no products at all.

Can you help me fix this function so that it shows featured first and non featured after?

Thank you

این خبر را در ایران وب سازان مرجع وب و فناوری دنبال کنید

مشاهده پاسخ های این مطلب
———————————————
این مطلب از سایت انجمن وردپرس گردآوری شده است و کلیه حقوق مطلق به انجمن وردپرس می باشد در صورت مغایرت و یا بروز مشکل اطلاع دهید تا حذف گردد

منبع: انجمن وردپرس

دکمه بازگشت به بالا