Handle stock for self and children, as well as syncing status with the children
This commit is contained in:
parent
d65cb855bc
commit
21d37e86c2
|
@ -22,9 +22,8 @@ class WC_Product_Variable extends WC_Product {
|
|||
public $total_stock;
|
||||
|
||||
/**
|
||||
* __construct function.
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $product
|
||||
*/
|
||||
public function __construct( $product ) {
|
||||
|
@ -51,24 +50,20 @@ class WC_Product_Variable extends WC_Product {
|
|||
* @return int
|
||||
*/
|
||||
public function get_total_stock() {
|
||||
|
||||
if ( empty( $this->total_stock ) ) {
|
||||
|
||||
$transient_name = 'wc_product_total_stock_' . $this->id;
|
||||
|
||||
if ( false === ( $this->total_stock = get_transient( $transient_name ) ) ) {
|
||||
$this->total_stock = $this->stock;
|
||||
$this->total_stock = max( 0, intval( $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 );
|
||||
|
||||
if ( $stock != '' ) {
|
||||
$this->total_stock += intval( $stock );
|
||||
$this->total_stock += max( 0, intval( $stock ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
set_transient( $transient_name, $this->total_stock, YEAR_IN_SECONDS );
|
||||
}
|
||||
}
|
||||
|
@ -82,14 +77,50 @@ class WC_Product_Variable extends WC_Product {
|
|||
* @param string $mode can be set, add, or subtract
|
||||
* @return int Stock
|
||||
*/
|
||||
function set_stock( $amount = null, $mode = 'set' ) {
|
||||
// Empty total stock so its refreshed
|
||||
public function set_stock( $amount = null, $mode = 'set' ) {
|
||||
$this->total_stock = '';
|
||||
|
||||
// Call parent set_stock
|
||||
delete_transient( 'wc_product_total_stock_' . $this->id );
|
||||
return parent::set_stock( $amount, $mode );
|
||||
}
|
||||
|
||||
/**
|
||||
* Performed after a stock level change at product level
|
||||
*/
|
||||
protected function check_stock_status() {
|
||||
$set_child_stock_status = '';
|
||||
|
||||
if ( ! $this->backorders_allowed() && $this->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
|
||||
$set_child_stock_status = 'outofstock';
|
||||
} elseif ( $this->backorders_allowed() || $this->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' ) ) {
|
||||
$set_child_stock_status = 'instock';
|
||||
}
|
||||
|
||||
if ( $set_child_stock_status ) {
|
||||
foreach ( $this->get_children() as $child_id ) {
|
||||
if ( 'yes' !== get_post_meta( $child_id, '_manage_stock', true ) ) {
|
||||
wc_update_product_stock_status( $child_id, $set_child_stock_status );
|
||||
}
|
||||
}
|
||||
|
||||
// Children statuses changed, so sync self
|
||||
self::sync_stock_status( $this->id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set_stock_status function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function set_stock_status( $status ) {
|
||||
$status = ( 'outofstock' === $status ) ? 'outofstock' : 'instock';
|
||||
|
||||
if ( update_post_meta( $this->id, '_stock_status', $status ) ) {
|
||||
do_action( 'woocommerce_product_set_stock_status', $this->id, $status );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the products children posts.
|
||||
*
|
||||
|
@ -97,11 +128,9 @@ class WC_Product_Variable extends WC_Product {
|
|||
* @return array of children ids
|
||||
*/
|
||||
public function get_children( $visible_only = false ) {
|
||||
$variable = $visible_only ? 'visible_children' : 'children';
|
||||
|
||||
if ( ! is_array( $this->$variable ) ) {
|
||||
$this->$variable = array();
|
||||
$transient_name = 'wc_product_' . $variable . '_ids_' . $this->id;
|
||||
if ( ! is_array( $this->children ) ) {
|
||||
$this->children = array();
|
||||
$transient_name = 'wc_product_children_ids_' . $this->id;
|
||||
|
||||
if ( false === ( $this->children = get_transient( $transient_name ) ) ) {
|
||||
$args = array(
|
||||
|
@ -114,23 +143,28 @@ class WC_Product_Variable extends WC_Product {
|
|||
'numberposts' => -1
|
||||
);
|
||||
|
||||
if ( $visible_only && 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
|
||||
$args['meta_query'] = array(
|
||||
array(
|
||||
'key' => '_stock_status',
|
||||
'value' => 'instock',
|
||||
'compare' => '='
|
||||
)
|
||||
);
|
||||
}
|
||||
$this->children = get_posts( $args );
|
||||
|
||||
$this->$variable = get_posts( $args );
|
||||
|
||||
set_transient( $transient_name, $this->$variable, YEAR_IN_SECONDS );
|
||||
set_transient( $transient_name, $this->children, YEAR_IN_SECONDS );
|
||||
}
|
||||
}
|
||||
|
||||
return $this->$variable;
|
||||
if ( $visible_only ) {
|
||||
$children = array();
|
||||
foreach( $this->children as $child_id ) {
|
||||
if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) {
|
||||
if ( 'instock' === get_post_meta( $child_id, '_stock_status', true ) ) {
|
||||
$children[] = $child_id;
|
||||
}
|
||||
} elseif ( $this->is_in_stock() ) {
|
||||
$children[] = $child_id;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$children = $this->children;
|
||||
}
|
||||
|
||||
return $children;
|
||||
}
|
||||
|
||||
|
||||
|
@ -396,7 +430,10 @@ class WC_Product_Variable extends WC_Product {
|
|||
|
||||
$variation = $this->get_child( $child_id );
|
||||
|
||||
if ( ! empty( $variation->variation_id ) ) {
|
||||
if ( empty( $variation->variation_id ) || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$variation_attributes = $variation->get_variation_attributes();
|
||||
$availability = $variation->get_availability();
|
||||
$availability_html = empty( $availability['availability'] ) ? '' : apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">'. wp_kses_post( $availability['availability'] ).'</p>', wp_kses_post( $availability['availability'] ) );
|
||||
|
@ -439,7 +476,6 @@ class WC_Product_Variable extends WC_Product {
|
|||
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
|
||||
), $this, $variation );
|
||||
}
|
||||
}
|
||||
|
||||
return $available_variations;
|
||||
}
|
||||
|
@ -469,6 +505,33 @@ class WC_Product_Variable extends WC_Product {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync variable product stock status with children
|
||||
* @param int $product_id
|
||||
*/
|
||||
public static function sync_stock_status( $product_id ) {
|
||||
$children = get_posts( array(
|
||||
'post_parent' => $product_id,
|
||||
'posts_per_page'=> -1,
|
||||
'post_type' => 'product_variation',
|
||||
'fields' => 'ids',
|
||||
'post_status' => 'publish'
|
||||
) );
|
||||
|
||||
$stock_status = 'outofstock';
|
||||
|
||||
foreach ( $children as $child_id ) {
|
||||
$child_stock_status = get_post_meta( $child_id, '_stock_status', true );
|
||||
$child_stock_status = $child_stock_status ? $child_stock_status : 'instock';
|
||||
if ( 'instock' === $child_stock_status ) {
|
||||
$stock_status = 'instock';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wc_update_product_stock_status( $product_id, $stock_status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync the variable product with it's children
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue