So. I'm trying to create a new order status with PHP, I need that new status to increase the stock of the product that are list in that order, when I do it, it doesn't work, never change anything, even if I attach it to an increase stock hook of WooCommerce.
I tried different things:
Option 1
function wc_maybe_increase_stock_levels1() {
if (isset($_GET['action']) && $_GET['action'] === 'mark_compra_proveedor' && isset($_GET['order_id'])) {
$order_id = intval($_GET['order_id']);
$order = wc_get_order($order_id);
if (!$order) {
return;
}
$stock_reduced = $order->get_data_store()->get_stock_reduced($order_id);
$trigger_increase = (bool) $stock_reduced;
// Only continue if we're increasing stock.
if (!$trigger_increase) {
return;
}
wc_increase_stock_levels($order);
// Ensure stock is not marked as "reduced" anymore.
$order->get_data_store()->set_stock_reduced($order_id, false);
}
}
add_action('woocommerce_order_status_mark_compra_proveedor', 'wc_maybe_increase_stock_levels1');`
Option 2
/**
* Función para sumar la cantidad de productos de un pedido al inventario cuando se guarda con el estado "wc-compra-proveedor".
*
* @param int $order_id El ID del pedido que se guarda.
*/
function sumar_al_inventario_al_guardar_pedido($order_id) {
// Obtenemos el objeto del pedido
$order = wc_get_order($order_id);
// Verificamos si el pedido tiene el estado "wc-compra-proveedor"
if ($order && $order->get_status() === 'wc-compra-proveedor') {
// Recorremos los productos en el pedido y ajustamos el inventario
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
$quantity = $item->get_quantity();
// Obtén la cantidad actual en inventario del producto
$current_stock = get_post_meta($product_id, '_stock', true);
// Calcula la nueva cantidad en inventario sumando la cantidad del pedido
$new_stock = $current_stock + $quantity;
// Actualiza la cantidad en inventario del producto
update_post_meta($product_id, '_stock', max(0, $new_stock)); // Aseguramos que no sea negativo
update_post_meta($product_id, '_manage_stock', 'yes'); // Habilitamos la gestión de inventario
}
}
}
// Hook para ejecutar la función al guardar un pedido
add_action('woocommerce_new_order', 'sumar_al_inventario_al_guardar_pedido');`
Any help is appreciated.
Try the following instead, that will increase related product(s) stock levels when the order is set to "compra-proveedor" status:
add_action('woocommerce_order_status_compra-proveedor', 'purchase_supplier_order_increase_stock_levels');
function purchase_supplier_order_increase_stock_levels( $order_id, $order ) {
// Loop through order items
foreach ( $order->get_items() as $item ) {
$item_quantity = $item->get_quantity(); // Order item quantity
$product = $item->get_product(); // Product object
$stock_quantity = $product->get_stock_quantity(); // product stock quantity
if ( ! $product->get_manage_stock() ) {
$product->set_manage_stock( true ); // Enable stock management if not set
}
$product->set_stock_quantity( $stock_quantity + $item_quantity ); // updated stock quantity
$product->set_stock_status(); // update stock status to 'instock'
$product->save(); // save product changes
$order->set_order_stock_reduced(true); // Mark order as stock reduced (trick)
$order->save(); // save order changes (increase stock)
}
}
It should work.