Created
June 6, 2023 15:05
-
-
Save hamidrezayazdani/6030b239c15be0fd6b957fe65ff6f683 to your computer and use it in GitHub Desktop.
Avoid purchasing certain products without them becoming out-of-stock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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