Stock fixes

This commit is contained in:
Mike Jolley 2011-08-21 17:47:49 +01:00
parent 23cbdbdbc4
commit da6d4b9da9
4 changed files with 45 additions and 12 deletions

View File

@ -203,6 +203,7 @@ function woocommerce_order_items_meta_box($post) {
<td class="center">
<input type="hidden" name="item_id[<?php echo $loop; ?>]" value="<?php echo $item['id']; ?>" />
<input type="hidden" name="item_name[<?php echo $loop; ?>]" value="<?php echo $item['name']; ?>" />
<input type="hidden" name="item_variation[<?php echo $loop; ?>]" value="<?php echo $item['variation_id']; ?>" />
<button type="button" class="remove_row button">&times;</button>
</td>
</tr>

View File

@ -140,13 +140,14 @@ class woocommerce_product {
*/
function reduce_stock( $by = 1 ) {
if ($this->managing_stock()) :
$reduce_to = $this->stock - $by;
update_post_meta($this->id, 'stock', $reduce_to);
$this->stock = $this->stock - $by;
$this->total_stock = $this->total_stock - $by;
update_post_meta($this->id, 'stock', $this->stock);
// Out of stock attribute
if (!$this->is_in_stock()) update_post_meta($this->id, 'stock_status', 'outofstock');
return $reduce_to;
return $this->stock;
endif;
}
@ -157,13 +158,14 @@ class woocommerce_product {
*/
function increase_stock( $by = 1 ) {
if ($this->managing_stock()) :
$increase_to = $this->stock + $by;
update_post_meta($this->id, 'stock', $increase_to);
$this->stock = $this->stock + $by;
$this->total_stock = $this->total_stock + $by;
update_post_meta($this->id, 'stock', $this->stock);
// Out of stock attribute
if ($this->is_in_stock()) update_post_meta($this->id, 'stock_status', 'instock');
return $increase_to;
return $this->stock;
endif;
}

View File

@ -156,9 +156,32 @@ class woocommerce_product_variation extends woocommerce_product {
function reduce_stock( $by = 1 ) {
if ($this->variation_has_stock) :
if ($this->managing_stock()) :
$reduce_to = $this->stock - $by;
update_post_meta($this->variation_id, 'stock', $reduce_to);
return $reduce_to;
$this->stock = $this->stock - $by;
$this->total_stock = $this->total_stock - $by;
update_post_meta($this->variation_id, 'stock', $this->stock);
// Parents out of stock attribute
if (!$this->is_in_stock()) :
// Check parent
$parent_product = &new woocommerce_product( $this->id );
if ($parent_product->managing_stock()) :
if (!$parent_product->backorders_allowed()) :
if ($parent_product->total_stock==0 || $parent_product->total_stock<0) :
update_post_meta($this->id, 'stock_status', 'outofstock');
endif;
endif;
else :
if ($parent_product->total_stock==0 || $parent_product->total_stock<0) :
update_post_meta($this->id, 'stock_status', 'outofstock');
endif;
endif;
endif;
return $this->stock;
endif;
else :
return parent::reduce_stock( $by );
@ -173,9 +196,15 @@ class woocommerce_product_variation extends woocommerce_product {
function increase_stock( $by = 1 ) {
if ($this->variation_has_stock) :
if ($this->managing_stock()) :
$increase_to = $this->stock + $by;
update_post_meta($this->variation_id, 'stock', $increase_to);
return $increase_to;
$this->stock = $this->stock + $by;
$this->total_stock = $this->total_stock + $by;
update_post_meta($this->variation_id, 'stock', $this->stock);
// Parents out of stock attribute
if ($this->is_in_stock()) update_post_meta($this->id, 'stock_status', 'instock');
return $this->stock;
endif;
else :
return parent::increase_stock( $by );

View File

@ -223,6 +223,7 @@ function woocommerce_add_order_item() {
<td class="center">
<input type="hidden" name="item_id[<?php echo $index; ?>]" value="<?php echo $_product->id; ?>" />
<input type="hidden" name="item_name[<?php echo $index; ?>]" value="<?php echo $_product->get_title(); ?>" />
<input type="hidden" name="item_variation[<?php echo $loop; ?>]" value="<?php if (isset($_product->variation_id)) echo $_product->variation_id; ?>" />
<button type="button" class="remove_row button">&times;</button>
</td>
</tr>