0

I want to increase the product variation limit in woocomerce.

In existing woocommerce code, The product variation limit is 100.

$limit = apply_filters( ‘woocommerce_rest_batch_items_limit’, 100, $this->get_normalized_rest_base() );

I changed the existing code product variation limit as 100 to 200.

$limit = apply_filters( ‘woocommerce_rest_batch_items_limit’, 200, $this->get_normalized_rest_base() );

It works but when I update my woocommerce plugin existing code changes are removed.

So I want that changes in function.php file. How to do it.

Base Code ( wordpress/plugin/woocommerce/includes/abstract/abstract-wc-rest-controller )

protected function check_batch_limit( $items ) {
    $limit = apply_filters( 'woocommerce_rest_batch_items_limit', 100, $this->get_normalized_rest_base() );
    $total = 0;

    if ( ! empty( $items['create'] ) ) {
        $total += count( $items['create'] );
    }

    if ( ! empty( $items['update'] ) ) {
        $total += count( $items['update'] );
    }

    if ( ! empty( $items['delete'] ) ) {
        $total += count( $items['delete'] );
    }

    if ( $total > $limit ) {
    /* translators: %s: items limit */
        return new WP_Error( 'woocommerce_rest_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), array( 'status' => 413 ) );
    }

    return true;
}
0

This is what the apply_filters() call is for. It's a hook that lets you modify the value.

In this case you want to use add_filter() with the woocommerce_rest_batch_items_limit hook name as the first argument, and then a callback function that returns a modified value:

function wpse_304237_rest_batch_items_limit( $limit ) {
    $limit = 200;

    return $limit;
}
add_filter( 'woocommerce_rest_batch_items_limit', 'wpse_304237_rest_batch_items_limit' );

Hooks are the main way themes and plugins integrate with WordPress and eachother, so if you want to start doing custom development on WordPress or WooCommerce I'd make learning them a priority.

  • I already did the code now understand what I did wrong add_filter( 'woocommerce_rest_batch_items_limit', 'product_variant_check_batch_limit', 200 ); function product_variant_check_batch_limit($limit){ return $limit; } – kirubanidhi May 23 '18 at 11:27
  • You’re not changing the limit anymore. You’re just returning the original limit. And you’re setting the priority to 200 for some reason. Please read the documentation in my first link. – Jacob Peattie May 23 '18 at 12:41
  • okay. Thanks, I am a newbie to WordPress. I applied your code in local. it works. I applied same code in themes/functions.php inside but not working. – kirubanidhi May 23 '18 at 13:00
  • In local, I am using one theme file but test server they used two themes, parent and child theme. I added code in child_theme/functions.php. The product variation limit is not affected so getting an error. {"code":"woocommerce_rest_request_entity_too_large","message":"Unable to accept more than 100 items for this request.","data":{"status":413}} – kirubanidhi May 23 '18 at 14:22
0

We resolve the problem. We created the new plugin named as product-variation-limit-plugin and after then create a new file name as product-variation-limit-plugin.php. Added below code inside of the PHP file.

http://woocommerce.com description: This plugin is used for increase the product variation limit while export product from odoo to woocommerce Version: 1.0 Author: Sodexis PVT Ltd Author URI: http://woocommerce.com License: GPL2 */

function wpse_rest_batch_items_limit( $limit ) {
    $limit = 200;

    return $limit;
}
add_filter( 'woocommerce_rest_batch_items_limit', 'wpse_rest_batch_items_limit' );

We checked on it. The problem was product variant is not synced from Odoo to woo commerce so we created this plugin. It is working properly.

-2

For all of those who want to use plugin instead, I created it: Increase Product Variation Limit For Woocommerce

  • Please post code to answer this question locally here on the site. Thanks! – Christine Cooper Jul 4 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.