• Resolved conducivedata

    (@conducivedata)


    Problem: When placing an order for multiple products that have attachments, the attachment(s) from the first product are repeated on the order page for each product in the order. Any other ordered products’ attachments are not displayed.

    Example: Create two products–Sprocket and Widget. Add a Sprocket Guide attachment to the Sprocket and a Widget Whitepaper to the Widget. Place an order containing one Sprocket and one Widget. The order received and order detail pages will show the Sprocket Guide twice and never the Widget Whitepaper (or vice versa, depending on the order of products in the cart).

    Cause: Function wcpoa_add_values_to_order_item_meta in class-woocommerce-product-attachment-public.php is responsible for gathering all the required attachments for an order when it is placed. The function is called once for each order item having an attachment.

    Every time the function is called, it loops through ALL order items and creates an order item meta record for each of them instead of just looking at the current order item.

    Using our example, the function would first be called for the Sprocket order item. It would then iterate through all order items and grab both the Sprocket Guide and Widget Whitepaper, creating order item meta entries for each. The function would then be called for the Widget order item and iterate through all items again.

    When these order item meta entries are then displayed on the order checkout and order details pages, only the first order item meta entry is retrieved for each order item. The Sprocket Guide was the first attachment recorded for both items, so it is shown twice.

    Solution: I have made some tweaks to wcpoa_add_values_to_order_item_meta in class-woocommerce-product-attachment-public.php and changed the add_action hook (due to deprecation) that calls it in class-woocommerce-product-attachment.php. This is compatible with Woocommerce 3.0+.

    The approach is simple: only create an order item meta entry for the order item being passed into the function instead of iterating through the entire order item list each time. My code is below.

    wcpoa_add_values_to_order_item_meta in class-woocommerce-product-attachment-public.php:

    public function wcpoa_add_values_to_order_item_meta($item_id, $item, $order_id) {
    
        $item_product = new WC_Order_Item_Product($item);
    
        $product_id = $item_product->get_product_id();
        $wcpoa_attachment_ids = get_post_meta($product_id, 'wcpoa_attachments_id', true);
        $wcpoa_attachment_name = get_post_meta($product_id, 'wcpoa_attachment_name', true);
        $wcpoa_attachment_description = get_post_meta($product_id, 'wcpoa_attachment_description', true);
        $wcpoa_attachment_url = get_post_meta($product_id, 'wcpoa_attachment_url', true);
        $wcpoa_variation = get_post_meta($product_id, 'wcpoa_variation', true);
        $wcpoa_order_status = get_post_meta($product_id, 'wcpoa_order_status', true);
        $wcpoa_expired_date = get_post_meta($product_id, 'wcpoa_expired_date', true);
    
        if (!empty($wcpoa_attachment_ids)) {
            $wcpoa_order_attachment_order_arr = array(
                'wcpoa_attachment_ids' => $wcpoa_attachment_ids,
                'wcpoa_attachment_name' => $wcpoa_attachment_name,
                'wcpoa_att_order_description' => $wcpoa_attachment_description,
                'wcpoa_attachment_url' => $wcpoa_attachment_url,
                'wcpoa_order_status' => $wcpoa_order_status,
                'wcpoa_order_product_variation' => $wcpoa_variation,
                'wcpoa_order_attachment_expired' => $wcpoa_expired_date
            );
            wc_add_order_item_meta($item_id, 'wcpoa_order_attachment_order_arr', $wcpoa_order_attachment_order_arr);
        }
    }

    In class-woocommerce-product-attachment.php, replace
    $this->loader->add_action('woocommerce_add_order_item_meta', $plugin_public, 'wcpoa_add_values_to_order_item_meta', 1, 2);
    with
    $this->loader->add_action('woocommerce_new_order_item', $plugin_public, 'wcpoa_add_values_to_order_item_meta', 1, 3);

    Please incorporate this fix into the next release, adjusting as you see fit for Woocommerce 2.x compatibility.

Viewing 1 replies (of 1 total)
  • Plugin Author Dotstore

    (@dots)

    Hi @conducivedata,
    Thanks for reporting the issue and your effort. We have released the new version and this issue has been resolved.
    we appreciate it.
    Thank You,
    Multidots.

Viewing 1 replies (of 1 total)

The topic ‘Duplicate / Missing Attachments on Order Page’ is closed to new replies.