From aea2b83153eee81c103810d471c3b44a8665f5f9 Mon Sep 17 00:00:00 2001 From: Justin Stern Date: Mon, 18 Mar 2013 01:36:59 -0400 Subject: [PATCH 1/2] Product Variation Order Again Bug Fix and Improvement * Fixed a bug introduced with the new order item meta data structure of WC 2.0: product variations could not be "ordered again" * Improved the order again support for products with custom item meta data --- woocommerce-functions.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/woocommerce-functions.php b/woocommerce-functions.php index 89885555a1c..948d28078ab 100644 --- a/woocommerce-functions.php +++ b/woocommerce-functions.php @@ -853,15 +853,17 @@ function woocommerce_order_again() { $quantity = (int) $item['qty']; $variation_id = (int) $item['variation_id']; $variations = array(); - foreach ( $item['item_meta'] as $meta ) { - if ( ! substr( $meta['meta_name'], 0, 3) === 'pa_' ) continue; - $variations[$meta['meta_name']] = $meta['meta_value']; + $cart_item_data = apply_filters( 'woocommerce_order_again_cart_item_data', array(), $item, $order ); + + foreach ( $item['item_meta'] as $meta_name => $meta_value ) { + if ( 'pa_' === substr( $meta_name, 0, 3 ) ) + $variations[ $meta_name ] = $meta_value[0]; } // Add to cart validation - if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations ) ) continue; + if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) continue; - $woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ); + $woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations, $cart_item_data ); } do_action( 'woocommerce_ordered_again', $order->id ); @@ -1687,4 +1689,4 @@ function woocommerce_save_address() { } } -add_action( 'template_redirect', 'woocommerce_save_address' ); \ No newline at end of file +add_action( 'template_redirect', 'woocommerce_save_address' ); From f4076b888f823918b4de90d801a38380b807c4d5 Mon Sep 17 00:00:00 2001 From: Justin Stern Date: Mon, 18 Mar 2013 01:38:22 -0400 Subject: [PATCH 2/2] Product Stock Filters Added a number of filters related to product stock to allow for improved control over the stock handling and display --- classes/class-wc-checkout.php | 2 +- classes/class-wc-order.php | 6 ++++-- templates/cart/mini-cart.php | 4 ++-- templates/checkout/review-order.php | 2 +- templates/order/order-details.php | 5 +++-- woocommerce-ajax.php | 6 ++++-- woocommerce-functions.php | 4 ++-- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/classes/class-wc-checkout.php b/classes/class-wc-checkout.php index 106b547231e..6898a1f4ceb 100644 --- a/classes/class-wc-checkout.php +++ b/classes/class-wc-checkout.php @@ -275,7 +275,7 @@ class WC_Checkout { // Add line item meta for backorder status if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $values['quantity'] ) ) - woocommerce_add_order_item_meta( $item_id, __( 'Backordered', 'woocommerce' ), $values['quantity'] - max( 0, $_product->get_total_stock() ) ); + woocommerce_add_order_item_meta( $item_id, apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ), $cart_item_key, $order_id ), $values['quantity'] - max( 0, $_product->get_total_stock() ) ); //allow plugins to add order item meta do_action( 'woocommerce_add_order_item_meta', $item_id, $values ); diff --git a/classes/class-wc-order.php b/classes/class-wc-order.php index 9b9b16d057d..728375f028a 100644 --- a/classes/class-wc-order.php +++ b/classes/class-wc-order.php @@ -1457,7 +1457,9 @@ class WC_Order { $old_stock = $_product->stock; - $new_quantity = $_product->reduce_stock( $item['qty'] ); + $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item ); + + $new_quantity = $_product->reduce_stock( $qty ); $this->add_order_note( sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) ); @@ -1540,4 +1542,4 @@ class WC_Order { } -} \ No newline at end of file +} diff --git a/templates/cart/mini-cart.php b/templates/cart/mini-cart.php index 1a101caf54c..a1a57dda40a 100644 --- a/templates/cart/mini-cart.php +++ b/templates/cart/mini-cart.php @@ -45,7 +45,7 @@ global $woocommerce; cart->get_item_data( $cart_item ); ?> - + ' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '', $cart_item, $cart_item_key ); ?> @@ -71,4 +71,4 @@ global $woocommerce; - \ No newline at end of file + diff --git a/templates/checkout/review-order.php b/templates/checkout/review-order.php index 6fa79c5f353..75ece30a429 100644 --- a/templates/checkout/review-order.php +++ b/templates/checkout/review-order.php @@ -168,7 +168,7 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods(); if ($_product->exists() && $values['quantity']>0) : echo ' - ' . $_product->get_title() . ' × ' . $values['quantity'] . '' . $woocommerce->cart->get_item_data( $values ) . ' + ' . apply_filters( 'woocommerce_checkout_item_name', $_product->get_title() . ' × ' . $values['quantity'] . '' . $woocommerce->cart->get_item_data( $values ), $values, $item_id ) . ' ' . apply_filters( 'woocommerce_checkout_item_subtotal', $woocommerce->cart->get_product_subtotal( $_product, $values['quantity'] ), $values, $item_id ) . ' '; endif; diff --git a/templates/order/order-details.php b/templates/order/order-details.php index 67fc256aa62..4dd63ad171c 100755 --- a/templates/order/order-details.php +++ b/templates/order/order-details.php @@ -45,7 +45,8 @@ $order = new WC_Order( $order_id ); '; - echo '' . $item['name'] . ' × ' . $item['qty'] . ''; + $product_name = '' . $item['name'] . ' × ' . $item['qty'] . ''; + echo apply_filters( 'woocommerce_order_table_item_name', $product_name, $item ); $item_meta = new WC_Order_Item_Meta( $item['item_meta'] ); $item_meta->display(); @@ -139,4 +140,4 @@ $order = new WC_Order( $order_id ); -
\ No newline at end of file +
diff --git a/woocommerce-ajax.php b/woocommerce-ajax.php index 880f1c6aa49..afaab410060 100644 --- a/woocommerce-ajax.php +++ b/woocommerce-ajax.php @@ -1157,7 +1157,8 @@ function woocommerce_ajax_reduce_order_item_stock() { if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) { $old_stock = $_product->stock; - $new_quantity = $_product->reduce_stock( $order_item_qty[ $item_id ] ); + $stock_change = apply_filters( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[ $item_id ], $item_id ); + $new_quantity = $_product->reduce_stock( $stock_change ); $return[] = sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_quantity ); $order->add_order_note( sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_quantity) ); @@ -1209,7 +1210,8 @@ function woocommerce_ajax_increase_order_item_stock() { if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) { $old_stock = $_product->stock; - $new_quantity = $_product->increase_stock( $order_item_qty[ $item_id ] ); + $stock_change = apply_filters( 'woocommerce_restore_order_stock_quantity', $order_item_qty[ $item_id ], $item_id ); + $new_quantity = $_product->increase_stock( $stock_change ); $return[] = sprintf( __( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_quantity ); $order->add_order_note( sprintf( __( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_quantity ) ); diff --git a/woocommerce-functions.php b/woocommerce-functions.php index 948d28078ab..71264dcc13e 100644 --- a/woocommerce-functions.php +++ b/woocommerce-functions.php @@ -244,9 +244,9 @@ function woocommerce_update_cart_action() { continue; // Sanitize - $quantity = preg_replace( "/[^0-9\.]/", "", $cart_totals[ $cart_item_key ]['qty'] ); + $quantity = apply_filters( 'woocommerce_stock_amount_cart_item', preg_replace( "/[^0-9\.]/", "", $cart_totals[ $cart_item_key ]['qty'] ), $cart_item_key ); - if ( $quantity == "" ) + if ( "" === $quantity ) continue; // Update cart validation