I’ve a inventory discount script that works completely. The one drawback is, when a purchase order is made and the inventory is decreased, the date_modified
meta is up to date. This causes issues for different scripts I’ve, because the publish is not truly up to date – simply the inventory.
How can I obtain the identical consequence with this code, with out it truly updating the date_modified
meta? (ie: solely replace date_modified
when the product itself is manually up to date).
// ---------------------------------------------------------------------------------------- //
// -------------------------- REDUCE WEIGHT BASED ON WEIGHT FIELD ----------------------- //
// ---------------------------------------------------------------------------------------- //
// cut back inventory based mostly on 'pa_weight' attribute
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
operate filter_order_item_quantity( $amount, $order, $merchandise )
{
$product = $item->get_product();
$term_name = $product->get_attribute('pa_weight');
// 'pa_weight' - preserve solely the numbers
$amount = preg_replace('/[^0-9.]+/', '', $term_name);
// new amount
if( is_numeric ( $amount ) && $amount != 0 )
$amount *= $amount;
return $amount;
}
/**
* Order change inventory discount repair.
*
* @param $forestall
* @param $merchandise
* @param $item_quantity
* @return bool|blended
*/
operate custom_stock_reduction_order_change( $forestall, $merchandise, $item_quantity ) {
$stock_reduced = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
$stock_reduction = filter_order_item_quantity( $item->get_quantity(), null, $merchandise );
if ( $stock_reduction == $stock_reduced ) {
$forestall = true;
}
return $forestall;
}
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', 'custom_stock_reduction_order_change', 10, 3 );