Add Weight & Change Orders CSV Output

New 11/2020: Total Order Weight. This snippet will give you the ability to modify the CSV download for your vendors. I did this for my site in order to enable my vendors to upload the CSV into the GoShippo shipping service. Since vendor-level integration with shipping services doesn’t seem to be possible, this at least enables vendors to upload the CSV in the necessary format to purchase and print paid shipping labels in bulk.

So, this snippet unsets various columns that were not necessary, including the column that displays the full shipping address in one line, since Shippo can’t recognize it. It then adds new columns for all the separate parts of the shipping address so that it works. I also take the opportunity to rename some things here as well.

As always, this is either added to your Child Theme’s functions.php file, or consider using the My Custom Functions Pro plugin to manage and organize all your snippets.

// ADD SAVING OF WEIGHT
add_action( 'woocommerce_checkout_update_order_meta', 'save_weight_order' );
function save_weight_order( $order_id ) {
    $weight = WC()->cart->get_cart_contents_weight();
    update_post_meta( $order_id, '_cart_weight', $weight );
}

// CSV HEADERS
add_filter( 'wcmp_vendor_order_generate_csv_headers', 'rename_column_headers' );
function rename_column_headers( $header ) {
    $new_header = array(
     'order' => __('Order #', 'dc-woocommerce-multi-vendor' ),
     'date_of_purchase' => __('Date', 'dc-woocommerce-multi-vendor' ),
     'product' => __('Items', 'dc-woocommerce-multi-vendor' ),
     'qty' => __('Quantities', 'dc-woocommerce-multi-vendor' ),
     'order_weight' => __('Total Weight (oz)', 'dc-woocommerce-multi-vendor'),
     'discount_used' => __('Discount', 'dc-woocommerce-multi-vendor' ),
     'tax' => __('Tax', 'dc-woocommerce-multi-vendor'),
     'shipping' => __('Shipping Charge', 'dc-woocommerce-multi-vendor' ),
     'buyer_contact' => __('Phone', 'dc-woocommerce-multi-vendor' ),
     'address_1' => __('Shipping Address 1', 'dc-woocommerce-multi-vendor'),
     'address_2' => __('Shipping Address 2', 'dc-woocommerce-multi-vendor'),
     'city' => __('Shipping City', 'dc-woocommerce-multi-vendor'),
     'state' => __('Shipping State', 'dc-woocommerce-multi-vendor'),
     'postcode' => __('Shipping Postcode', 'dc-woocommerce-multi-vendor'),
     'country' => __('Shipping Country', 'dc-woocommerce-multi-vendor'),
    );
return $new_header;
}

// CSV COLUMN CONTENT
add_filter( 'wcmp_vendor_order_generate_csv_data', 'add_shipping_column_data', 10, 3 );
function add_shipping_column_data( $data, $customer_order, $vendor ) {
    $order = wc_get_order($customer_order);
    $commission_id = get_post_meta( $customer_order, '_commission_id', true);
    $customer_phone = $order->get_billing_phone();
    $vendor_items = $vendor->get_vendor_items_from_order($customer_order, $vendor->term_id);
    $item_names = $item_qty = array();
    if(sizeof($vendor_items)>0){
        foreach ($vendor_items as $item) {
            $item_names[] = $item['name'];
            $item_qty[] = $item['quantity'];
        }
        $coupon_used = '';
        $coupons = $order->get_items('coupon');
        foreach ($coupons as $coupon_item_id => $item) {
            $coupon = new WC_Coupon(trim($item['name']));
            $coupon_post = get_post($coupon->get_id());
            $author_id = $coupon_post->post_author;
            if ($vendor->id == $author_id) {
                $coupon_used .= $item['name'] . ', ';
            }
        }
    }
    
    $order_id = wp_get_post_parent_id($customer_order);
    $weight = get_post_meta( $order_id, '_cart_weight', true );
    $new_data =array(
    'order' => '#' . $customer_order,
    'date_of_purchase' => date_i18n('Y-m-d', strtotime($order->get_date_created())),
    'product' => implode(', ', $item_names),
    'qty' => implode(', ', $item_qty),
    'order_weight'  => isset($weight) ? $weight : '',
    'discount_used' => apply_filters('wcmp_export_discount_used_in_order', $coupon_used),
    'tax' => get_post_meta($commission_id, '_tax', true),
    'shipping' => get_post_meta($commission_id, '_shipping', true),
    'buyer_contact' => $customer_phone,
    'address_1' => $order->get_shipping_address_1(),
    'address_2' => $order->get_shipping_address_2(),
    'city' => $order->get_shipping_city(),
    'state' => $order->get_shipping_state(),
    'postcode' => $order->get_shipping_postcode(),
    'country' => $order->get_shipping_country(),   
    );
    
return $new_data;
}

Tested and working on Woocommerce 4.6.1, WCMp 3.5.10

Is this no longer relevant or not working? Please let us know in the comments!

Comments on this post

Your email address will not be published. Required fields are marked *