Skip to content

Instantly share code, notes, and snippets.

@grola
Last active February 21, 2018 19:55
Show Gist options
  • Save grola/56d9dcfc8a4ca61afba3864401f54856 to your computer and use it in GitHub Desktop.
Save grola/56d9dcfc8a4ca61afba3864401f54856 to your computer and use it in GitHub Desktop.
Add/remove category to product dependent on stock quantity
<?php
class studiowp_product_category_on_stock_change {
protected $category_id;
protected $stock_level;
protected $variable_product_id = 0;
protected $variable_product_add_category = false;
public function __construct( $category_id, $stock_level ) {
$this->category_id = $category_id;
$this->stock_level = $stock_level;
add_action( 'woocommerce_product_set_stock', array( $this, 'woocommerce_product_set_stock' ) );
add_action( 'woocommerce_variation_set_stock', array( $this, 'woocommerce_variation_set_stock' ) );
add_action( 'woocommerce_process_product_meta_variable', array( $this, 'woocommerce_process_product_meta_variable' ) );
}
public function woocommerce_process_product_meta_variable( $product_id ) {
/** @var WC_Product_Variable $product */
$product = wc_get_product( $product_id );
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$managing_stock = true;
$stock_quantity = 0;
if ( !$product->managing_stock() ) {
$available_variations = $product->get_available_variations();
foreach ( $available_variations as $available_variation ) {
$variation = wc_get_product( $available_variation['variation_id'] );
if ( $variation->managing_stock() ) {
$stock_quantity += $variation->get_stock_quantity();
}
else {
$managing_stock = false;
}
}
}
if ( $managing_stock ) {
$category_ids = $product->get_category_ids();
if ( $stock_quantity <= $this->stock_level ) {
if ( !in_array( $this->category_id, $category_ids ) ) {
$category_ids[] = $this->category_id;
$product->set_category_ids( $category_ids );
$product->save();
}
}
else {
if ( in_array( $this->category_id, $category_ids ) ) {
unset( $category_ids[array_search($this->category_id, $category_ids)] );
$product->set_category_ids( $category_ids );
$product->save();
}
}
}
}
}
/**
* @param WC_Product_Variation $variation
*/
public function woocommerce_variation_set_stock( $variation ) {
/** @var WC_Product_Variable $product */
$product = wc_get_product( $variation->get_parent_id() );
$stock_quantity = 0;
$available_variations = $product->get_available_variations();
foreach ( $available_variations as $available_variation ) {
$variation = wc_get_product( $available_variation['variation_id'] );
if ( $variation->managing_stock() ) {
$stock_quantity += $variation->get_stock_quantity();
}
}
$category_ids = $product->get_category_ids();
if ( $stock_quantity <= $this->stock_level ) {
if ( !in_array( $this->category_id, $category_ids ) ) {
$category_ids[] = $this->category_id;
$product->set_category_ids( $category_ids );
$product->save();
}
}
else {
if ( in_array( $this->category_id, $category_ids ) ) {
unset( $category_ids[array_search($this->category_id, $category_ids)] );
$product->set_category_ids( $category_ids );
$product->save();
}
}
}
/**
* @param WC_Product $product
*/
public function woocommerce_product_set_stock( $product ) {
$stock_quantity = $product->get_stock_quantity();
$category_ids = $product->get_category_ids();
if ( $stock_quantity <= $this->stock_level ) {
if ( !in_array( $this->category_id, $category_ids ) ) {
$category_ids[] = $this->category_id;
$product->set_category_ids( $category_ids );
$product->save();
}
}
else {
if ( in_array( $this->category_id, $category_ids ) ) {
unset( $category_ids[array_search($this->category_id, $category_ids)] );
$product->set_category_ids( $category_ids );
$product->save();
}
}
}
}
/**
* Example
* when stock quantity equals or is less than 10 add product to category with id 38
* when stock is grater than 10 remove product from category with id 38
*/
new studiowp_product_category_on_stock_change( 38, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment