Skip to content

Instantly share code, notes, and snippets.

@ovidiumght
Created August 19, 2021 07:26
Show Gist options
  • Select an option

  • Save ovidiumght/10a344e120453c6eb0f15b2db5c75859 to your computer and use it in GitHub Desktop.

Select an option

Save ovidiumght/10a344e120453c6eb0f15b2db5c75859 to your computer and use it in GitHub Desktop.
<?php
function wpcb_should_override_backorder( $product ) {
//Set the product categories in the array
$categories = array('cat-a', 'cat-b');
return has_term( $categories, 'product_cat' ) && $product->get_attribute('pa_attr-a');
}
function __enable_backorders( $value, $product ) {
// Allow Backorder For All Products In Specific Categories WITH Specific Attribute
if( wpcb_should_override_backorder($product) ){
// Enables Manage Stock - Use "yes" or "notify", which is the equivalent of "Allow, but notify customer"
return $product->get_manage_stock('edit') ? $value : 'notify';
}
}
function woocommerce_product_get_stock_status( $stock_status, $product ) {
if(wpcb_should_override_backorder($product)){
return 'onbackorder';
}
}
add_filter( 'woocommerce_product_get_backorders', '__enable_backorders', 0, 2);
add_filter( 'woocommerce_product_get_stock_status', 'woocommerce_product_get_stock_status', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment