• Resolved BASit Bulbulia

    (@basitbulbulia)


    I have stock items with variations all of which have stock management; The problem is when I do sell a stock variation item the parent item stock count does not reduce !

    Is there a plug in or code which will allow me to do this ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’m not a bonafide expert, but pretty well informed.

    My approach is to have a SKU for parent item (eg Paint-Red) then assign individual SKUs to variants (eg Paint-Red-5kg, Paint-Red-10kg, paint-Red-20kg)

    I’m not really using stock control TBH, but I believe this is the correct approach – especially as the order invoice should be accurate with the variant chosen.

    Simple enough to test this approach to see if stock numbers drop accordingly.

    Good luck.

    Thread Starter BASit Bulbulia

    (@basitbulbulia)

    Thanks Mike ! I tried what you suggested and it does not work. The variants do change stock but the parent stock remain the same . . .

    But, isn’t that what’s supposed to happen?

    The parent SKU is really just a ‘ghost’ product – and umbrella for the variants.

    In my example – you cannot buy ‘red paint’ (that’s just the shelf where the various different tins are stored) – but you can buy a 5kg tin of red paint, and this purchase would reduce the count of 5kgs tins by 1.

    The parent product cannot be affected, as it is not associated with an actual product.

    Thread Starter BASit Bulbulia

    (@basitbulbulia)

    The solution I require is that the ghost product as you call it (Parent item) stock must also change.

    Thread Starter BASit Bulbulia

    (@basitbulbulia)

    I tested this code and it works . . .

    Code goes to functions.php of your theme or child theme.

    add_action( 'woocommerce_reduce_order_stock', 'lets_reduce_parent' );
    
    function lets_reduce_parent( $order_id ) {
      $order = wc_get_order( $order_id );
      foreach( $order->get_items() as $item_id => $item ){
          $parent_id = $item->get_product_id();
          $child_id = $item->get_variation_id();
          $quantity = $item->get_quantity();
    
          if($child_id == 0) return;
          else
          {
              $parent_product = wc_get_product( $parent_id );
              $stock = $parent_product->get_stock_quantity();
              $new_stock = $stock - $quantity;
              wc_update_product_stock( $parent_product, $quantity, 'decrease' );
              $order->add_order_note( "Parent Product ID: $parent_id - Stock Reduced: $stock -> $new_stock");
          }
      }
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Stock of parent sku must be equal to stock of all variations.’ is closed to new replies.