Woocommerce: sort on a property

Lukket Opslået 3 år siden Betalt ved levering
Lukket

I have a woocommerce store. I want to sort results on default by a attribute I made: Levertijd

I already added this to [login to view URL]:

/************* Add sorting by attributes **************/

/**

* Defines the criteria for sorting with options defined in the method below

*/

add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');

function custom_woocommerce_get_catalog_ordering_args($args)

{

global $wp_query;

// Changed the $_SESSION to $_GET

if (isset($_GET['orderby'])) {

switch ($_GET['orderby']):

case 'pa_levertijd-asc':

$args['order'] = 'ASC';

$args['meta_key'] = 'pa_levertijd';

$args['orderby'] = 'meta_value';

break;

endswitch;

}

return $args;

}

/**

* Adds the sorting options to dropdown list .. The logic/criteria is in the method above

*/

add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');

add_filter('woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby');

function custom_woocommerce_catalog_orderby($sortby)

{

$sortby['pa_levertijd-asc'] = 'Sorteer op levertijd';

return $sortby;

}

/**

* Save custom attributes as post's meta data as well so that we can use in sorting and searching

*/

// add_action( 'save_post', 'save_woocommerce_attr_to_meta', 10, 3 );

function save_woocommerce_attr_to_meta($post_id, $post, $update)

{

if ($post->post_type != 'product') {return;}

// 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]);

}

}

/************ End of Sorting ***************************/

It sort of works: see image on right top (it says Levertijd). However, the results still arent filtered on levertijd. It should be filtered on attribute levertijd alphabetical. So Direct leverbaar first, Levertijd 1-5 werkdagen sevond, levertijd 2-3 weken third etc.

1} How soon can you do this?

2 } Can you do it within 5$?

PHP WordPress WooCommerce MySQL Software Arkitektur

Projekt ID: #29439614

Om projektet

1 bud Remote projekt Aktiv 3 år siden

1 freelancer byder i gennemsnit $4 på dette job

netfair4business

Hi we can look to work for your project if you are intrested pls reply, we will connect and forward the query to our developers and they will provide the solution

$4 CAD / time
(0 bedømmelser)
0.0