Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
2.2 kB
4
Indexable
Never

// Modify the query arguments and add custom b2b prices
function eg_add_postmeta_ordering_args($args_sort_cw){
    $cw_orderby_value = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) :
        apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));
    switch ($cw_orderby_value) {
        case 'b2bking_regular_product_price_group_308_asc':
            $args_sort_cw['orderby'] = 'meta_value_num';
            $args_sort_cw['order'] = 'asc';
            $args_sort_cw['meta_key'] = 'b2bking_regular_product_price_group_308';
            $args_sort_cw['meta_type'] = 'NUMERIC'; 
            break;
        case 'b2bking_regular_product_price_group_308_desc':
            $args_sort_cw['orderby'] = 'meta_value_num';
            $args_sort_cw['order'] = 'desc';
            $args_sort_cw['meta_key'] = 'b2bking_regular_product_price_group_308';
            $args_sort_cw['meta_type'] = 'NUMERIC';
            break;
    }

    return $args_sort_cw;
}

function eg_add_new_postmeta_orderby($sortby){
    // Get the current user's ID
    $current_user_id = get_current_user_id();

    // Check if we have a user (user is logged in)
    if ($current_user_id) {
        // Get the meta value for 'user_group_id' for the current user
        $user_group_id = get_user_meta($current_user_id, 'b2bking_customergroup', true);

        // Check if the meta value is equal to current logged user 
        if ($user_group_id == '308') {
            $sortby['b2bking_regular_product_price_group_308_asc'] = __('Order by price (Ascending)', 'woocommerce');
            $sortby['b2bking_regular_product_price_group_308_desc'] = __('Order by price (Descending)', 'woocommerce');
        }
    }

    return $sortby;
}

add_filter('woocommerce_get_catalog_ordering_args', 'eg_add_postmeta_ordering_args');
add_filter('woocommerce_default_catalog_orderby_options', 'eg_add_new_postmeta_orderby');
add_filter('woocommerce_catalog_orderby', 'eg_add_new_postmeta_orderby');

function rsd_catalog_orderby( $orderby ) {
    unset($orderby["price"]);
    unset($orderby["price-desc"]);
	unset($orderby["popularity"]);
    return $orderby;
}

add_filter( "woocommerce_catalog_orderby", "rsd_catalog_orderby", 20 );
Leave a Comment