Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hamidrezayazdani/6030b239c15be0fd6b957fe65ff6f683 to your computer and use it in GitHub Desktop.
Save hamidrezayazdani/6030b239c15be0fd6b957fe65ff6f683 to your computer and use it in GitHub Desktop.
Avoid purchasing certain products without them becoming out-of-stock
<?php
/**
* @param $query
* @param $product_id
* @param $exclude_order_id
*
* @return mixed|string
*/
function ywp_prevent_purchase_products( $query, $product_id, $exclude_order_id ) {
$bad_products = array( 5544 );
if ( in_array( $product_id, $bad_products ) ) {
$stock = (int) get_post_meta( $product_id, '_stock', true );
return "SELECT $stock;";
}
return $query;
}
add_filter ( 'woocommerce_query_for_reserved_stock', 'ywp_prevent_purchase_products', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment