I'm trying to update some attributes of my product by a cron to avoid common mistakes. The code causing me trouble is executed in a loop:
foreach ($storesIds as $storeId) {
$this->storeEmulation->startEnvironmentEmulation($storeId);
$fixed = $this->doExecute($storeId);
$this->storeEmulation->stopEnvironmentEmulation();
}
The "doExecute method" code is simplified here, tests are done before to avoid to save a product
for nothing, here is how I'm saving a product:
$searchCriteria = $this->searchCriteriaBuilder->create();
$products = $this->productRepository->getList($searchCriteria)->getItems();
// [...]
foreach ($products as $product) {
// [...]
$fixes[] = ['visibility' => ['from' => $product->getVisibility(), 'to' => $expectedVisibility]];
$product->setStatus($expectedVisibility);
// [...]
$fixes[] = ['taxClassId' => ['from' => $product->getTaxClassId(), 'to' => $expectedTaxClassId]];
$product->setTaxClassId($expectedTaxClassId);
// [...]
try {
$this->productRepository->save($product);
$this->logger->notice('A product was fixed', [
'sku' => $product->getSku(),
'storeId' => $store->getId(),
'fixes' => $fixes
]);
} catch (\Exception $e) {
$this->logger->error('A product can\'t be fixed', [
'sku' => $product->getSku(),
'storeId' => $store->getId(),
'fixes' => $fixes
]);
}
}
I can see in the logs
that on the first fetch the visibility
is fixed. But the taxClassId
is not changing:
[2019-03-15 22:49:57] FixerLogger.NOTICE: A product was fixed {"sku":"107","storeId":"8","fixes":[{"taxClassId":{"from":"0","to":"2"}}]} []
[2019-03-15 22:49:58] FixerLogger.NOTICE: A product was fixed {"sku":"018-K","storeId":"8","fixes":[{"visibility":{"from":"2","to":1}},{"taxClassId":{"from":"8","to":"2"}}]} []
[2019-03-15 22:49:58] FixerLogger.NOTICE: A product was fixed {"sku":"109-K","storeId":"8","fixes":[{"taxClassId":{"from":"8","to":"2"}}]} []
[2019-03-15 22:50:27] FixerLogger.NOTICE: A product was fixed {"sku":"107","storeId":"8","fixes":[{"taxClassId":{"from":"0","to":"2"}}]} []
[2019-03-15 22:50:27] FixerLogger.NOTICE: A product was fixed {"sku":"018-K","storeId":"8","fixes":[{"taxClassId":{"from":"8","to":"2"}}]} []
[2019-03-15 22:50:28] FixerLogger.NOTICE: A product was fixed {"sku":"109-K","storeId":"8","fixes":[{"taxClassId":{"from":"8","to":"2"}}]} []
Any idea where am I wrong?
Thanks for your help!
$fixes[] = ['visibility' => ['from' => $product->getVisibility(), 'to' => $expectedVisibility],'taxClassId' => ['from' => $product->getTaxClassId(), 'to' => $expectedTaxClassId]]; $product->setStatus($expectedVisibility); $product->setTaxClassId($expectedTaxClassId);
$fixes[]
array