Tuesday, 2 May 2023

Checking the pincode of shipping and adding in assigning to fulfilment-partner

 // Checking the pincode of shipping and adding in assigning to fulfilment-partner
function before_checkout_create_order($order, $data)
{
    $args1 = array('role__in' => array('fulfilment_partner') );    
    $shipping_pincode = $order->shipping_postcode;
    $users = get_users( $args1 );        

    // Array of WP_User objects.
    foreach ( $users as $user ) {
        $stpax = explode(',',trim($user->stokist_pincode_allocated));
        if (in_array($shipping_pincode,$stpax)) {             
            $store_manager_id = $user->id ;            
        }        
    }         
    $order->update_meta_data('_store_manager_id', $store_manager_id);    
}
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);

//Showing the order according to fulfilment partner wise.
function custom_admin_shop_manager_orders($query)
{
    global $pagenow;
    $qv = &$query->query_vars;
    $currentUserRoles = wp_get_current_user()->roles;
    $user_id = get_current_user_id();    
    //if (in_array('shop_manager',$currentUserRoles)) {        
    if($currentUserRoles[0] == 'fulfilment_partner')
    {
    
        if ( $pagenow == 'edit.php' &&     isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {
            // I use the meta key from step 1 as a second parameter here
            $query->set('meta_key', '_store_manager_id');
            // The value we want to find is the $user_id defined above
            $query->set('meta_value', $user_id);
        }
    }

    return $query;
}
add_filter('pre_get_posts', 'custom_admin_shop_manager_orders');

No comments:

Post a Comment