php – Custom reduce stock code affecting date_modified – How to bypass?
Question
I have a stock reduction script that works perfectly. The only problem is, when a purchase is made and the stock is reduced, the date_modified
meta is updated. This causes problems for other scripts I have, as the post isn’t actually updated – just the stock.
How can I achieve the same result with this code, without it actually updating the date_modified
meta? (ie: only update date_modified
when the product itself is manually updated).
// ---------------------------------------------------------------------------------------- //
// -------------------------- REDUCE WEIGHT BASED ON WEIGHT FIELD ----------------------- //
// ---------------------------------------------------------------------------------------- //
// reduce stock based on 'pa_weight' attribute
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
function filter_order_item_quantity( $quantity, $order, $item )
{
$product = $item->get_product();
$term_name = $product->get_attribute('pa_weight');
// 'pa_weight' - keep only the numbers
$quantity = preg_replace('/[^0-9.]+/', '', $term_name);
// new quantity
if( is_numeric ( $quantity ) && $quantity != 0 )
$quantity *= $quantity;
return $quantity;
}
/**
* Order change stock reduction fix.
*
* @param $prevent
* @param $item
* @param $item_quantity
* @return bool|mixed
*/
function custom_stock_reduction_order_change( $prevent, $item, $item_quantity ) {
$stock_reduced = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
$stock_reduction = filter_order_item_quantity( $item->get_quantity(), null, $item );
if ( $stock_reduction == $stock_reduced ) {
$prevent = true;
}
return $prevent;
}
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', 'custom_stock_reduction_order_change', 10, 3 );
0
3 weeks
2022-06-17T16:53:55-05:00
2022-06-17T16:53:55-05:00 0 Answers
0 views
0
Leave an answer