1. The problem:
I want to sell perishable items which are weighed (and thus never exactly match in weight what the customer actually ordered) and sometimes if a specific product is no more available, a similar product will be shipped.
e.g.:
ordered: 1.000 kg apple (variety A) shipped: 0.975 kg apple (variety B)
I want the customers to be able to see this in their invoice:
Product | kg ordered | kg shipped __________________|____________|___________ apple (variety A) | 1.000 kg | 0.000 kg apple (variety B) | 0.000 kg | 0.975 kg
- This means WooCommerce should be "convinced" to handle quantities in both Qty columns/variables (old and custom one) as
float
instead ofinteger
. - Therefore I also need WooCommerce to stop deleting products with zero quantity (
product_qty
)(when updating an order in the backoffice).
Partial temporary solution:
\woocommerce\includes\admin\wc-admin-functions.php
Line284:
$item->delete();
// $item->delete();
- I also want the new custom column
product_quantity_ordered
in the table{prefix}wc_order_product_lookup
(which I created already)(if not 'good practice' - where&how should I add this column?) to get the values from columnproduct_qty
(in the same table{prefix}wc_order_product_lookup
) the moment the customer makes his order.
Partial temporary solution:
a SQL query in phpMyAdmin:
UPDATE
L16_wc_order_product_lookup
SET product_qty_ordered
= product_qty
WHERE date_created
BETWEEN '2020-06-24' AND '2020/07/14';
- A plus would be to display both quantity columns next to each other (first quantity ordered, then quantity shipped) in both admin edit order page and in the invoice.
2. What I’ve tried:
- I added the new custom column
product_quantity_ordered
in the table{prefix}wc_order_product_lookup
(again, if this is not "best practice"... where should I add this new column?)
see image: - I used the code snippet from Show product meta in order items table in Order Details (which displays the new column column and populates it with values from
product_qty
). However, as soon as I edit the product quantity in the edit order page - of course - WooCommerce updates both quantity columns.
see image:
3. My code:
Only this snippet (from Show product meta in order items table in Order Details ) for the time being:
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
// set the column name
$column_name = 'Qty ordered';
echo '<th>' . $column_name . '</th>';
}
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
$value = $item->get_quantity( 'edit' ); // this line of code stems from Line28 of \woocommerce\packages\woocommerce-admin\src\Overrides\OrderTraits.php
echo '<td>' . $value . '</td>';
}
$item->delete(); → replace with: // $item->delete();
– Yoda - user264474 Jul 18 '20 at 22:04