• Resolved renamer

    (@renamer)


    Just a feature I would want, because clicking show items is a bit tedious and you can easily misclick the text and end up in the order screen. So having an action in the Bulk actions menu to activate the Show Items column would be nice!

    Anyway thanks for a great plugin.

Viewing 6 replies - 1 through 6 (of 6 total)
  • missodessa

    (@missodessa)

    I’ve actually tried to add something like this into the code myself and got it to work because I had the same issue with accidentally opening the order instead! I can send it to you to test if you would like.

    Thread Starter renamer

    (@renamer)

    That would be very helpful! If you can just paste it here, that would be nice.

    Plugin Author pipdig

    (@pipdig)

    Hi guys, we’ve just pushed update 1.9.0 which will hopefully help with this. We added a [Show All](https://imgur.com/a/KnLRIH7) button to the column header. When clicked, it will the items for all orders on the page.

    Thread Starter renamer

    (@renamer)

    That’s nice, but you forgot to add it in the HPOS filter woocommerce_shop_order_list_table_columns.

    Plugin Author pipdig

    (@pipdig)

    Version 1.9.1 fixes that now!

    ??

    missodessa

    (@missodessa)

    so from the bulk actions drop down select “show purchased items” then select the ones you want to show and hit apply. The page will refresh and then you will see each item load. I am still learning coding myself so hopefully this was close to what you were asking for.


    You can paste this into the purchased-items-column-woocommerce.php file (its the entire code)



    add_action('before_woocommerce_init', function() {
    if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
    \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
    }
    });

    // Add Purchased Items Column
    add_filter('manage_edit-shop_order_columns', function($columns) {
    $new_array = [];
    foreach ($columns as $key => $title) {
    if ($key == 'billing_address') {
    $new_array['order_items'] = __('Purchased', 'woocommerce');
    }
    $new_array[$key] = $title;
    }
    return $new_array;
    });

    add_action('manage_shop_order_posts_custom_column', function($column) {
    if ($column == 'order_items') {
    $order_id = (int) get_the_ID();
    echo '<a href="#" class="show_order_items" data-wc-order="'.$order_id.'">'.__('Show items', 'purchased-items-column-woocommerce').'</a>';
    echo '<div id="show_order_items_'.$order_id.'"></div>';
    }
    }, 10, 2);

    // Add JS to handle showing items on click
    add_action('admin_footer', function() {
    if ((isset($_GET['page']) && $_GET['page'] == 'wc-orders') || (isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order')) {
    ?>
    <script>
    jQuery(document).ready(function($) {
    // Handle manual click on "Show Items" button
    $(".show_order_items").click(function(e) {
    e.preventDefault(); // Prevent default anchor behavior
    let thisBtn = $(this);
    let order_id = thisBtn.data('wc-order');
    thisBtn.hide(); // Hide button after click
    $('#show_order_items_'+order_id).html('loading...');
    let data = {
    'action': 'pipdig_wc_find_products_ajax',
    'sec': '<?php echo wp_create_nonce('pipdig_wc_find_products_nonce'); ?>',
    'order_id': order_id
    };
    $.post(ajaxurl, data, function(response) {
    $('#show_order_items_'+order_id).html(response);
    });
    });

    // Automatically click show items for bulk action orders
    let bulkOrderIds = <?php echo json_encode(get_transient('bulk_show_items_order_ids')); ?>;
    if (bulkOrderIds) {
    bulkOrderIds.forEach(function(order_id) {
    $(".show_order_items[data-wc-order='" + order_id + "']").trigger("click");
    });
    }
    });
    </script>
    <?php
    // Clear the transient after it's used
    delete_transient('bulk_show_items_order_ids');
    }
    }, 999999);

    // AJAX action to fetch order items
    add_action('wp_ajax_pipdig_wc_find_products_ajax', function() {
    check_ajax_referer('pipdig_wc_find_products_nonce', 'sec');

    if (!function_exists('wc_get_order')) return;

    $output = '';
    $order_id = (int) $_POST['order_id'];
    $order = wc_get_order($order_id);

    if (!$order) wp_die();

    // Exclude these meta keys from showing in the column
    $excluded_meta_keys = array('_reduced_stock', '_restock_refunded_items');

    foreach ($order->get_items() as $item) {
    $product = $item->get_product();
    $sku_info = '';
    $meta_markup = '';

    if ($product) {
    $sku = $product->get_sku();
    if ($sku) $sku_info = ' ('.esc_html($sku).')';

    // Loop through product meta data and exclude unwanted attributes
    foreach ($item->get_meta_data() as $meta_data) {
    $meta_data_as_array = $meta_data->get_data();

    // Skip excluded meta attributes
    if (in_array($meta_data_as_array['key'], $excluded_meta_keys, true)) {
    continue;
    }

    $value = $meta_data_as_array['value'];
    $attribute = $meta_data_as_array['key'];
    $attribute_name = wc_attribute_label($attribute, $product);
    $name = $value;

    $term = get_term_by('slug', $value, $attribute);
    if ($term) {
    $name = $term->name;
    }

    $meta_markup .= '<br>'.esc_html($attribute_name).': '.esc_html($name);
    }
    }

    $quantity = (int) $item['quantity'];
    $product_name = esc_html($item['name']);

    $output .= $quantity.' &times; '.$product_name.$sku_info.$meta_markup.'<br /><br />';
    }

    echo $output;
    wp_die();
    });

    // Add Bulk Action for Purchased Items
    add_filter('bulk_actions-edit-shop_order', function($bulk_actions) {
    $bulk_actions['show_purchased_items'] = __('Show Purchased Items', 'purchased-items-column-woocommerce');
    return $bulk_actions;
    });

    // Handle Bulk Action to auto-expand purchased items
    add_filter('handle_bulk_actions-edit-shop_order', function($redirect_to, $action, $order_ids) {
    if ($action === 'show_purchased_items') {
    // Store selected order IDs in transient for retrieval after page load
    set_transient('bulk_show_items_order_ids', $order_ids, 60); // 60 seconds expiration
    $redirect_to = add_query_arg('bulk_show_purchased_items', count($order_ids), $redirect_to);
    }
    return $redirect_to;
    }, 10, 3);

    // Admin Notice after bulk action
    add_action('admin_notices', function() {
    if (!empty($_REQUEST['bulk_show_purchased_items'])) {
    $purchased_items_count = intval($_REQUEST['bulk_show_purchased_items']);
    printf('<div id="message" class="updated notice is-dismissible"><p>' .
    _n('%s order processed.', '%s orders processed.', $purchased_items_count, 'purchased-items-column-woocommerce') . '</p></div>', $purchased_items_count);
    }
    });


Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.