Added wc_stock_amount() core function and replaced intval

#4612
This commit is contained in:
Mike Jolley 2014-06-25 11:25:28 +01:00
parent 03de576bfb
commit 0f6f04e017
10 changed files with 25 additions and 16 deletions

View File

@ -136,7 +136,7 @@ class WC_Product {
* @return int
*/
public function get_stock_quantity() {
return $this->managing_stock() ? apply_filters( 'woocommerce_stock_amount', $this->stock ) : '';
return $this->managing_stock() ? wc_stock_amount( $this->stock ) : '';
}
/**

View File

@ -916,7 +916,7 @@ class WC_Admin_Post_Types {
if ( ! $product->is_type('grouped') ) {
if ( isset( $_REQUEST['_manage_stock'] ) ) {
update_post_meta( $post_id, '_manage_stock', 'yes' );
wc_update_product_stock( $post_id, intval( $_REQUEST['_stock'] ) );
wc_update_product_stock( $post_id, wc_stock_amount( $_REQUEST['_stock'] ) );
} else {
update_post_meta( $post_id, '_manage_stock', 'no' );
wc_update_product_stock( $post_id, 0 );
@ -1104,7 +1104,7 @@ class WC_Admin_Post_Types {
if ( ! empty( $_REQUEST['change_stock'] ) ) {
update_post_meta( $post_id, '_manage_stock', 'yes' );
wc_update_product_stock( $post_id, intval( $_REQUEST['_stock'] ) );
wc_update_product_stock( $post_id, wc_stock_amount( $_REQUEST['_stock'] ) );
}
if ( ! empty( $_REQUEST['_manage_stock'] ) ) {

View File

@ -132,7 +132,7 @@ class WC_Meta_Box_Order_Items {
);
if ( isset( $order_item_qty[ $item_id ] ) )
wc_update_order_item_meta( $item_id, '_qty', apply_filters( 'woocommerce_stock_amount', $order_item_qty[ $item_id ] ) );
wc_update_order_item_meta( $item_id, '_qty', wc_stock_amount( $order_item_qty[ $item_id ] ) );
if ( isset( $order_item_tax_class[ $item_id ] ) )
wc_update_order_item_meta( $item_id, '_tax_class', wc_clean( $order_item_tax_class[ $item_id ] ) );

View File

@ -1247,7 +1247,7 @@ class WC_Meta_Box_Product_Data {
}
if ( ! empty( $_POST['_manage_stock'] ) ) {
wc_update_product_stock( $post_id, intval( $_POST['_stock'] ) );
wc_update_product_stock( $post_id, wc_stock_amount( $_POST['_stock'] ) );
} else {
update_post_meta( $post_id, '_stock', '' );
}
@ -1459,7 +1459,7 @@ class WC_Meta_Box_Product_Data {
} else {
delete_post_meta( $variation_id, '_backorders' );
}
wc_update_product_stock( $variation_id, intval( $variable_stock[ $i ] ) );
wc_update_product_stock( $variation_id, wc_stock_amount( $variable_stock[ $i ] ) );
} else {
delete_post_meta( $variation_id, '_backorders' );
delete_post_meta( $variation_id, '_stock' );

View File

@ -270,7 +270,7 @@ class WC_AJAX {
*/
public static function add_to_cart() {
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
$quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
$quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) ) {

View File

@ -403,7 +403,7 @@ class WC_Form_Handler {
}
// Sanitize
$quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
$quantity = apply_filters( 'woocommerce_stock_amount_cart_item', wc_stock_amount( preg_replace( "/[^0-9\.]/", '', $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
if ( '' === $quantity || $quantity == $values['quantity'] )
continue;
@ -568,7 +568,7 @@ class WC_Form_Handler {
if ( 'variable' === $add_to_cart_handler ) {
$variation_id = empty( $_REQUEST['variation_id'] ) ? '' : absint( $_REQUEST['variation_id'] );
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_REQUEST['quantity'] );
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$all_variations_set = true;
$variations = array();
@ -683,7 +683,7 @@ class WC_Form_Handler {
// Simple Products
} else {
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_REQUEST['quantity'] );
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
// Add to cart validation
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

View File

@ -94,7 +94,7 @@ class WC_Order {
return false;
}
wc_add_order_item_meta( $item_id, '_qty', apply_filters( 'woocommerce_stock_amount', $qty ) );
wc_add_order_item_meta( $item_id, '_qty', wc_stock_amount( $qty ) );
wc_add_order_item_meta( $item_id, '_tax_class', $product->get_tax_class() );
wc_add_order_item_meta( $item_id, '_product_id', $product->id );
wc_add_order_item_meta( $item_id, '_variation_id', isset( $product->variation_id ) ? $product->variation_id : 0 );

View File

@ -64,7 +64,7 @@ class WC_Product_Grouped extends WC_Product {
$stock = get_post_meta( $child_id, '_stock', true );
if ( $stock != '' ) {
$this->total_stock += intval( $stock );
$this->total_stock += wc_stock_amount( $stock );
}
}
}
@ -73,7 +73,7 @@ class WC_Product_Grouped extends WC_Product {
}
}
return apply_filters( 'woocommerce_stock_amount', $this->total_stock );
return wc_stock_amount( $this->total_stock );
}
/**

View File

@ -54,20 +54,20 @@ class WC_Product_Variable extends WC_Product {
$transient_name = 'wc_product_total_stock_' . $this->id;
if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
$this->total_stock = max( 0, intval( $this->stock ) );
$this->total_stock = max( 0, wc_stock_amount( $this->stock ) );
if ( sizeof( $this->get_children() ) > 0 ) {
foreach ( $this->get_children() as $child_id ) {
if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) {
$stock = get_post_meta( $child_id, '_stock', true );
$this->total_stock += max( 0, intval( $stock ) );
$this->total_stock += max( 0, wc_stock_amount( $stock ) );
}
}
}
set_transient( $transient_name, $this->total_stock, YEAR_IN_SECONDS );
}
}
return apply_filters( 'woocommerce_stock_amount', $this->total_stock );
return wc_stock_amount( $this->total_stock );
}
/**

View File

@ -266,6 +266,15 @@ function wc_array_overlay( $a1, $a2 ) {
return $a1;
}
/**
* Formats a stock amount by running it through a filter
* @param int|float $amount
* @return int|float
*/
function wc_stock_amount( $amount ) {
return apply_filters( 'woocommerce_stock_amount', $amount );
}
/**
* Get the price format depending on the currency position
*