Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with products with more than 50 Skus #187

Open
bham1586 opened this issue Nov 1, 2016 · 3 comments
Open

Issue with products with more than 50 Skus #187

bham1586 opened this issue Nov 1, 2016 · 3 comments

Comments

@bham1586
Copy link

bham1586 commented Nov 1, 2016

When getting product SKUs, the bigcommerce API limits the response to 50 by default and uses pagination. In Resources/Product.php the function skus() needs to take pagination into account.

This is a quick solution I wrote, could probably be improved upon.

public function skus()
{
	$allSkus = [];
	$limit = 250;
	$page = 1;
	do {
		$filter = [
			'limit' => $limit,
			'page'  => $page,
		];
		$filter = Filter::create($filter);
		$skus = Client::getCollection($this->fields->skus->resource . $filter->toQuery(), 'Sku');
		$page++;
		$allSkus = array_merge($allSkus, $skus);
	} while(count($skus) >= $limit);
	return $allSkus;
}

for this to work you also need to add the following at the top of the file.

use Bigcommerce\Api\Filter;

@MagicTrixor
Copy link

$skus can be null, which will make the array_merge cause a warning

@bham1586
Copy link
Author

bham1586 commented Dec 13, 2016

@MagicTrixor Good point. As I said, my solution could be improved upon. This should fix it.

        public function skus()
	{
		$allSkus = [];
		$limit = 250;
		$page = 1;
		do {
			$filter = [
				'limit' => $limit,
				'page'  => $page,
			];
			$filter = Filter::create($filter);
			$skus = Client::getCollection($this->fields->skus->resource . $filter->toQuery(), 'Sku');
			$page++;
			if(!empty($skus)) {
				$allSkus = array_merge($allSkus, $skus);
			}
		} while(count($skus) >= $limit);
		return $allSkus;
	}

@Lewiscowles1986
Copy link
Contributor

Perhaps if $product->skus is null you can use the new PHP null coalesce features?

$skus = $product->skus ?? [];

Also for the do while loop, the condition should be $skuCount == $limit. It should never be able to return a greater number than the $limit unless there is a serious problem with the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants