Pridanie stĺpca v zozname objednávok

Pridanie stĺpca s názvom spôsob dopravy

// add columns sposob dopravy
function add_shipping_to_orders_table($columns) {
    $columns['shipping_method'] = 'Spôsob dopravy';
    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'add_shipping_to_orders_table', 11 );

Nastavenie hodnoty stĺpca

function get_shipping_columns_values($column) {
    global $post;
    
    // získame názov spôsob dopravy
    $order_facory = new WC_Order_Factory();
    $order = $order_facory->get_order( $post->ID );

    if ( $column == 'shipping_method' ) {   
        if ($order->get_shipping_method() == 'Zvýraznený stav') {
            echo '<strong style="color:red">Vlastný názov<strong>';
        }
        else{
            echo $order->get_shipping_method();
        }
    }


}

add_action( 'manage_shop_order_posts_custom_column', 'get_shipping_columns_values', 2 );

Pridanie zoradenia podľa stĺpca

function customize_sort_function_custom_values( $columns ) {
	global $wpdb;

    $custom = array(
        'shipping_method' => 'shipping_method'
    );

    return wp_parse_args( $custom, $columns );
}

add_filter( "manage_edit-shop_order_sortable_columns", 'customize_sort_function_custom_values' );



function order_by_shipping_method_order_join($query) {
    global $wpdb;

    if ( is_admin() && $_GET['post_type'] === 'shop_order' && $_GET['orderby'] === 'shipping_method' ) {
    	$first_item_in_order = "SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = $wpdb->posts.ID AND order_item_type = 'shipping' LIMIT 0, 1";
        $query .= "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS items ON $wpdb->posts.ID = items.order_id AND items.order_item_id = ($first_item_in_order) ";
        $query .= "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id AND (itemmeta.meta_key = 'method_id') ";
    }

    return $query;
}

add_filter('posts_join', 'order_by_shipping_method_order_join');

Doplnenie funkcie v prípade zlého filtrovania


function order_by_shipping_method_where($where) {
	global $wpdb;

	if( is_admin() && $_GET['post_type'] === 'shop_order' && $_GET['orderby'] === 'shipping_method' ) {
		if(strpos($where, 'shop_webhook')) {
			return " AND $wpdb->posts.post_type = 'shop_order' AND itemmeta.meta_key = 'method_id'";
		}
	}

	return $where;
}

add_filter('posts_where', 'order_by_shipping_method_where');


function generate_replace_part($shipping_methods, $replace) {
	$key = array_shift(array_keys($shipping_methods));
	$value = array_shift($shipping_methods);

	$query_part = "REPLACE($replace, '$key', '$value')";

	if(!empty($shipping_methods)) {
		return generate_replace_part($shipping_methods, $query_part);
	}

	return $query_part;
}

function order_by_shipping_method_order($order_by) {
	global $wpdb;

	$shipping_methods = array(
		'local_delivery' => 'Doručenie kurierom',
		'local_pickup'   => 'Osobný odber',
		'pick_pack_pont' => 'Pick Pack Pont'
	);

	if( is_admin() && $_GET['post_type'] === 'shop_order' && $_GET['orderby'] === 'shipping_method' ) {
		return generate_replace_part($shipping_methods, 'itemmeta.meta_value') . " " . strtoupper($_GET['order']);
	}
	
	return $order_by;
}

add_filter('posts_orderby', 'order_by_shipping_method_order');

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená. Vyžadované polia sú označené *