#StackBounty: #php #wordpress #woocommerce Reduce and check stock of Variable Product

Bounty: 50

Using this code to reduce the stock from inventory by Attribute Value. It works to reduce the stock, but when you have like 100 g in stock and the customer buys 120 g. It doesn’t show Out of Stock.

So the following code was created:

<?php

add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 ); 
function filter_order_item_quantity( $quantity, $order, $item )  
{
    $product   = $item->get_product();
    $stock_quantity = $product->get_stock_quantity();
    $term_name = $product->get_attribute('pa_weight');

    // The 'pa_size' attribute value is "15 grams" And we keep only the numbers
    $quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);

    // Calculated new quantity
    if( is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
        $quantity *= $quantity_grams;

    if($quantity > $stock_quantity) {
        return $quantity;
    } else {
        echo '<p>Out of Stock</p>';
    } 

}

However, it isn’t working…

Is there a way to check the stock from $quantity?

    if($quantity > $stock_quantity) {
        return $quantity;
    } else {
        echo '<p>Out of Stock</p>';
    } 

EDIT:

Tried to reduce the stock with a hook to wc_reduce_stock_levels, without success

add_filter( 'wc_reduce_stock_levels', 'filter_reduce_stock_levels', 5, 1); 
function filter_reduce_stock_levels( $order_id ) { 
    if ( is_a( $order_id, 'WC_Order' ) ) { 
        $order = $order_id; 
        $order_id = $order->get_id(); 
    } else { 
        $order = wc_get_order( $order_id ); 
    } 
    if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && sizeof( $order->get_items() ) > 0 ) { 
        foreach ( $order->get_items() as $item ) { 
            if ( $item->is_type( 'line_item' ) && ( $product = $item->get_product() ) && $product->managing_stock() ) { 
                $term_name = $product->get_attribute('pa_weight');
                $qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item ); 
                $item_name = $product->get_formatted_name(); 
                $new_stock = wc_update_product_stock( $product, $qty, 'decrease' ); 
                $quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
                $qty *= $quantity_grams;
                if ( ! is_wp_error( $new_stock ) ) { 
                    /** translators: 1: item name 2: old stock quantity 3: new stock quantity */ 
                    $order->add_order_note( sprintf( __( '%1$s stock reduced from %2$s to %3$s.', 'woocommerce' ), $item_name, $new_stock + $qty, $new_stock ) ); 

                    // Get the latest product data. 
                    $product = wc_get_product( $product->get_id() ); 

                    if ( '' !== get_option( 'woocommerce_notify_no_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { 
                        do_action( 'woocommerce_no_stock', $product ); 
                    } elseif ( '' !== get_option( 'woocommerce_notify_low_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_low_stock_amount' ) ) { 
                        do_action( 'woocommerce_low_stock', $product ); 
                    } 

                    if ( $new_stock < 0 ) { 
                        do_action( 'woocommerce_product_on_backorder', array( 'product' => $product, 'order_id' => $order_id, 'quantity' => $qty ) ); 
                    } 
                } 
            } 
        } 

        // ensure stock is marked as "reduced" in case payment complete or other stock actions are called 
        $order->get_data_store()->set_stock_reduced( $order_id, true ); 

        do_action( 'woocommerce_reduce_order_stock', $order ); 
    } 
} 


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.