WooCommerce – Fixed quantity of a product [closed]

I am trying to accomplish a thing on WooCommerce that I can not find any information about. It is like the following scenario:

I sell screws and have 1500 in stock. I would like the customer to choose between buying 10, 30, 50 and 100 screws in a drop-down list. The price for each screw is lower when you buy 100 compared to 10, 30 etc.

This works just the way I want by using product attributes and product variations. BUT the stock is not effected the way I want since the system think that it is one package of 100 screws and therefore reduce the stock by 1.

I am thinking of modifying the class-wc-product-variation.php file that contains the following code:

function reduce_stock( $by=1 ) {
    global $woocommerce;
    if ( $this->variation_has_stock ) {
        if ( $this->managing_stock() ) {
            $this->stock        = $this->stock - $by;
            $this->total_stock  = $this->total_stock - $by;
            update_post_meta( $this->variation_id, '_stock', $this->stock );
            $woocommerce->clear_product_transients( $this->id ); // Clear transient
            // Check parents out of stock attribute
            if ( ! $this->is_in_stock() ) {
                // Check parent
                $parent_product = new WC_Product( $this->id );
                // Only continue if the parent has backorders off
                if ( ! $parent_product->backorders_allowed() && $parent_product->get_total_stock() <= 0 ) {
                    update_post_meta( $this->id, '_stock_status', 'outofstock' );
                }
            }
            return $this->stock;
        }
    } else {
        return parent::reduce_stock( $by );
    }
}

I am trying to set $by to the name of the product variation (10, 30, 50, 100) but I am not that good at PHP. I have tried to collect some relevant code (at least what I think) and putted it together to the following function:

function new_reduce_by () {
    //Get the name of the variation
    $this->variation_data[$name] = $attrvalue[0];
    //Extract only the numbers (some attributes may end with "grams" etc.)
    preg_match_all('!\d+!', $attrvalue[0], $modified_attrvalue);
    //Convert the array to a variable
    $custom_reduce_value = implode(' ', $modified_attrvalue[0]);
    //Return the name (value) of the variation
    return $custom_reduce_value;
}

I have tried to call this function in different ways but without success, I guess the code above is not correct.

Should the code above work? If so, how do I call the function in the best way? I know that you cant call a function by changing $by=1, you have to pass a value or variable.

Or should I solve this some other way?

Many thanks!

3 Answers
3

You might want to reconsider your approach here, at first glance it seems to me that you could greatly simplify this but using a hook after the order is processed.

If all of your variations are really only quantities then it stands to reason that when an order is completed you’d update the quantities for the parent and each of the variations so that they all reflect the same inventory count.

So when processing the order you have parent ID and the variation IDs within the $product and you can simply update the quantity values for each within the loop.

I don’t know the right hook off the top of my head but you can find the WooCommerce docs at http://wcdocs.woothemes.com/apidocs/index.html and http://wcdocs.woothemes.com/codex/extending/hooks/

Leave a Comment