Fixed includes/wc-stock-functions.php PHPCS violations

This commit is contained in:
Claudio Sanches 2018-03-08 16:44:52 -03:00
parent da23b0399e
commit cf9ce1add8
1 changed files with 32 additions and 18 deletions

View File

@ -4,14 +4,11 @@
*
* Functions used to manage product stock levels.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @package WooCommerce/Functions
* @version 3.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;
/**
* Update a product's stock amount.
@ -20,16 +17,19 @@ if ( ! defined( 'ABSPATH' ) ) {
*
* @since 3.0.0 this supports set, increase and decrease.
*
* @param int|WC_Product $product
* @param int|null $stock_quantity
* @param string $operation set, increase and decrease.
* @param int|WC_Product $product Product ID or product instance.
* @param int|null $stock_quantity Stock quantity.
* @param string $operation Type of opertion, allows 'set', 'increase' and 'decrease'.
*
* @return bool|int|null
*/
function wc_update_product_stock( $product, $stock_quantity = null, $operation = 'set' ) {
if ( ! $product = wc_get_product( $product ) ) {
$product = wc_get_product( $product );
if ( ! $product ) {
return false;
}
if ( ! is_null( $stock_quantity ) && $product->managing_stock() ) {
// Some products (variations) can have their stock managed by their parent. Get the correct ID to reduce here.
$product_id_with_stock = $product->get_stock_managed_by_id();
@ -56,8 +56,8 @@ function wc_update_product_stock( $product, $stock_quantity = null, $operation =
/**
* Update a product's stock status.
*
* @param int $product_id
* @param int $status
* @param int $product_id Product ID.
* @param int $status Status.
*/
function wc_update_product_stock_status( $product_id, $status ) {
$product = wc_get_product( $product_id );
@ -69,8 +69,9 @@ function wc_update_product_stock_status( $product_id, $status ) {
/**
* When a payment is complete, we can reduce stock levels for items within an order.
*
* @since 3.0.0
* @param int $order_id
* @param int $order_id Order ID.
*/
function wc_maybe_reduce_stock_levels( $order_id ) {
$order = wc_get_order( $order_id );
@ -82,8 +83,9 @@ add_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' );
/**
* Reduce stock levels for items within an order.
*
* @since 3.0.0
* @param int|WC_Order $order_id
* @param int|WC_Order $order_id Order ID or order instance.
*/
function wc_reduce_stock_levels( $order_id ) {
if ( is_a( $order_id, 'WC_Order' ) ) {
@ -92,9 +94,15 @@ function wc_reduce_stock_levels( $order_id ) {
} else {
$order = wc_get_order( $order_id );
}
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && sizeof( $order->get_items() ) > 0 ) {
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && count( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( $item->is_type( 'line_item' ) && ( $product = $item->get_product() ) && $product->managing_stock() ) {
if ( ! $item->is_type( 'line_item' ) ) {
continue;
}
$product = $item->get_product();
if ( $product->managing_stock() ) {
$qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
$item_name = $product->get_formatted_name();
$new_stock = wc_update_product_stock( $product, $qty, 'decrease' );
@ -113,13 +121,19 @@ function wc_reduce_stock_levels( $order_id ) {
}
if ( $new_stock < 0 ) {
do_action( 'woocommerce_product_on_backorder', array( 'product' => $product, 'order_id' => $order_id, 'quantity' => $qty ) );
do_action(
'woocommerce_product_on_backorder', array(
'product' => $product,
'order_id' => $order_id,
'quantity' => $qty,
)
);
}
}
}
}
// ensure stock is marked as "reduced" in case payment complete or other stock actions are called
// Ensure stock is marked as "reduced" in case payment complete or other stock actions are called.
$order->get_data_store()->set_stock_reduced( $order_id, true );
do_action( 'woocommerce_reduce_order_stock', $order );