Sell unique product with woocommerce - simplified

by Geme   Last Updated November 15, 2017 06:08 AM

One way to sell a unique product as discussed here is to mamage_stock and set stock_quantity to 1.

However, this requires the user to set_manage_stock = 'yes', set_stock_status = 'instock', set_stock_quantity = 1, set_backorders = 'no', set_sold_individually = 'true').

All these except set_stock_quantity and set_stock_status can be set with a class that extends WC_Product like so:

class WC_Product_Unique extends WC_Product {
    public function __construct( $product = 0) {
        $this->supports[]   = 'ajax_add_to_cart';
        parent::__construct( $product );
    }

    public function __construct( $product = 0) {
        $this->supports[]   = 'ajax_add_to_cart';
        parent::__construct( $product );
    }

    public function get_type() {
        return 'unique';
    }

    public function set_manage_stock( $manage_stock ) {
        $this->set_prop( 'manage_stock', 'yes' );
    }

    //same as parent - doesn't have to be copied
    public function set_stock_quantity( $quantity ) {
        $this->set_prop( 'stock_quantity', '' !== $quantity ? wc_stock_amount( $quantity ) : null );
    }

    //same as parent - doesn't have to be copied
    public function set_stock_status( $stock_status = '' ) {
        $this->set_prop( 'stock_status', 'outofstock' === $status ? 'outofstock' : 'instock' );
    }

    public function set_backorders( $backorders ) {
        $this->set_prop( 'backorders', 'no' );
    }

    public function set_sold_individually( $sold_individually ) {
        $this->set_prop( 'sold_individually', true );
    }
}

And that all works, but as mentioned before, the user still needs to set status and quantity. I would like to simplify that.

I see two options:

1) javascript/jQuery - when unique product type is selected make sure that

a. when stock_quantity = 0, stock_status = outofstock

b. when 'instock' == stock_status selected, stock_quantity = 1

2) a. set_stock_quantity(1);

b. when product is sold, set_stock_status( 'outofstock' );

Option 2 looks like a nicer approach to me but what hook can be used to set_stock_status( 'outofstock' ) when a product is sold? What normally happens is that stock_quantity is reduced by 1 every time a product is sold and when that reaches 0, stock status is set to outofstock. That seems to happen in wc_update_product_stock() so I thought this function might work but it doesn't

add_action( 'woocommerce_product_set_stock', 'update_unique_stock_status_after_purchase' );
function update_unique_stock_status_after_purchase( $product_with_stock ){
    $product_with_stock->set_stock_status( 'outofstock' );
}

Any ideas?



Related Questions


WPB Woo Product Slider cuts image border

Updated April 24, 2015 02:03 AM

CUSTOM PRODUCT TYPE PLUGIN IN WOOCOMMERCE

Updated February 19, 2017 09:08 AM

Wordpress WooCommerce variative issue

Updated March 01, 2017 11:08 AM

Overwrite WooCommerce template files via plugin

Updated March 05, 2017 20:08 PM