• I am facing difficulties regarding filtering orders on the shop order page. Is there anyone who can help me with resolving this issue?, and I have created a new menu to display the ‘Pending Concierge’,’Completed’ and ‘Cancel’ order table, intending to show orders with a pending status,completed and cancelled. The issue I am currently facing is that when I add a new product ID for filtering and displaying in the order table.

    The code before i change

    if (current_user_can('dashboard_concierge')) {
            $product_ids = array(25491);

    this the screen shoot if product id only just one

    and this screen shoot if i added new product id ini array

    function modify_subsubsub_actions($subsubsub) {
        if (current_user_can('dashboard_concierge')) {
            $product_ids = array(25491,1779);


    this the full code

    function modify_subsubsub_actions($subsubsub) {
        if (current_user_can('dashboard_concierge')) {
            $product_ids = array(25491,1779);
    
            // Generate placeholders for product IDs in the SQL query
            $placeholders = implode(', ', array_fill( 1, count($product_ids), '%d'));
    
            $completed_menu_label = sprintf(__('Completed Concierge (%d Orders)', 'textdomain'), 0);
            $completed_menu_url = add_query_arg(
                array(
                    'post_status' => 'wc-completed',
                    'post_type' => 'shop_order',
                    'product_ids' => implode(',', $product_ids),
                    'concierge_menu' => 'completed',
                ),
                admin_url('edit.php')
            );
    
    		$pending_menu_label = sprintf(__('Pending Concierge (%d Orders)', 'textdomain'), 0);
            $pending_menu_url = add_query_arg(
                array(
                    'post_status' => 'wc-pending',
                    'post_type' => 'shop_order',
                    'product_ids' => implode(',', $product_ids),
                    'concierge_menu' => 'pending',
                ),
                admin_url('edit.php')
            );
    
    		$cancel_concierge_menu_label = sprintf(__('Cancel Concierge (%d Orders)', 'textdomain'), 0);
            $cancel_concierge_menu_url = add_query_arg(
                array(
                    'post_status' => 'wc-cancelled',
                    'post_type' => 'shop_order',
                    'product_ids' => implode(',', $product_ids),
                    'concierge_menu' => 'cancelled',
                ),
                admin_url('edit.php')
            );
    		
           // Query for order (completed) with ID product
            $completed_orders_count = count(get_orders_ids_by_product_id_filter($product_ids, array('wc-completed')));
    
            $completed_concierge_menu_label = sprintf(__('Completed Concierge (%d Orders)', 'textdomain'), $completed_orders_count);
            $subsubsub = array_slice($subsubsub, 0, 2, true) +
                array('completed' => '<a href="#" data-url="' . esc_url($completed_menu_url) . '" class="concierge-link">' . $completed_menu_label . '</a>') +
                array_slice($subsubsub, 2, null, true);
    
    
            // Query for order (pending)with ID product
            $pending_orders_count = count(get_orders_ids_by_product_id_filter($product_ids, array('wc-pending')));
    
            $pending_menu_label = sprintf(__('Pending Concierge (%d Orders)', 'textdomain'), $pending_orders_count);
            $subsubsub = array_slice($subsubsub, 0, 2, true) +
                array('pending_payment' => '<a href="#" data-url="' . esc_url($pending_menu_url) . '" class="concierge-link">' . $pending_menu_label . '</a>') +
                array_slice($subsubsub, 2, null, true);
    		
    		 // Query for (cancel)ID product
            $cancelled_orders_count = count(get_orders_ids_by_product_id_filter($product_ids, array('wc-cancelled')));
    
            $cancel_concierge_menu_label = sprintf(__('Cancel Concierge (%d Orders)', 'textdomain'), $cancelled_orders_count);
            $subsubsub = array_slice($subsubsub, 0, 2, true) +
                array('cancel' => '<a href="#" data-url="' . esc_url($cancel_concierge_menu_url) . '" class="concierge-link">' .   $cancel_concierge_menu_label . '</a>') +
                array_slice($subsubsub, 2, null, true);
    
            // unset menu
            unset($subsubsub['all']);
            unset($subsubsub['wc-completed']);
            unset($subsubsub['wc-pending']);
        }
    
        return $subsubsub;
    }
    
    add_filter('views_edit-shop_order', 'modify_subsubsub_actions');
    
    // Function to get product by id
    function get_orders_ids_by_product_id_filter($product_ids, $order_status = array('wc-completed', 'wc-pending')) {
        global $wpdb;
    
        // Generate placeholders for product IDs in the SQL query
        $placeholders = implode(', ', array_fill(1, count($product_ids), '%d'));
    
        // Query ID for orders with multiple product IDs
        $results = $wpdb->get_col(
            $wpdb->prepare(
                "SELECT DISTINCT order_items.order_id
                FROM {$wpdb->prefix}woocommerce_order_items as order_items
                LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
                LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
                WHERE posts.post_type = 'shop_order'
                AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
                AND order_items.order_item_type = 'line_item'
                AND order_item_meta.meta_key = '_product_id'
                AND CAST(order_item_meta.meta_value AS SIGNED) IN ($placeholders)
                GROUP BY order_items.order_id
                HAVING COUNT(DISTINCT order_item_meta.meta_value) = " . count($product_ids),
    
            )
        );
    
        return $results;
    }
    
    // Filter for query
    function apply_product_filter_to_order_query($query) {
        global $pagenow, $post_type;
    
        if ($pagenow == 'edit.php' && $post_type == 'shop_order' && isset($_GET['product_ids'])) {
            $product_ids = array_map('intval', explode(',', $_GET['product_ids']));
            $order_ids = get_orders_ids_by_product_id_filter($product_ids);
    
            if (!empty($order_ids)) {
                $query->query_vars['post__in'] = $order_ids;
            }
        }
    }
    
    add_action('pre_get_posts', 'apply_product_filter_to_order_query');
    

    Saya membutuhan bantuan pada halaman berikut: [login untuk melihat tautan]

  • Topik ‘Costum view order on dashboard for speciffic user’ tertutup untuk balasan baru.