woocommerce / wc-smooth-generator

Smooth product, customer and order generation for WooCommerce
315 stars 49 forks source link

[Enhancement]: Add count parameter to the wc_smooth_generate_object hook #76

Closed ovidiul closed 2 years ago

ovidiul commented 2 years ago

Describe the solution you'd like

On VIP, we can make use of its scalable cron infrastructure to offload multiple generate events.

By adding a count parameter to the wc_smooth_generate_object here, we can basically schedule events that could run on a constant basis to generate the imports as:

seq 0 10 | xargs -P 2 -I {} wp cron event schedule wc_smooth_generate_object now every_minute --type=order --count=10 --nr={}
seq 0 10 | xargs -P 2 -I {} wp cron event schedule wc_smooth_generate_object now every_minute --type=product --count=10 --nr={}

which would allow creating 10 events each generating 10 orders running each minute. The cron infrastructure would scale itself based on the number of events needed to process.

The code I am using currently looks like this:

function wc_smooth_generate_object( $type, $count = 1) {

        // Check what generation task to perform
        $i = 0;
        while($i++ < $count) {
            switch ( $type ) {
                    case 'order':
                            Generator\Order::generate();
                            break;
                    case 'product':
                            Generator\Product::generate();
                            break;
                    case 'customer':
                            Generator\Customer::generate();
                            break;
                    case 'coupon':
                            Generator\Coupon::generate();
                            break;
                    default:
                            return false;
            }
        }

        return false;
}

add_action( 'wc_smooth_generate_object', 'wc_smooth_generate_object' , 10, 2);

Happy to add a PR if this can be considered. It should have no impact on the existing functionality.

Describe alternatives you've considered

No response

Additional context

No response