2

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!

  • try this $fixes[] = ['visibility' => ['from' => $product->getVisibility(), 'to' => $expectedVisibility],'taxClassId' => ['from' => $product->getTaxClassId(), 'to' => $expectedTaxClassId]]; $product->setStatus($expectedVisibility); $product->setTaxClassId($expectedTaxClassId); – fmsthird Mar 15 at 23:47
  • instead of doing two $fixes[] array – fmsthird Mar 15 at 23:47
  • Hey, like I said, the code is simplified. Sometimes visibility is fixed, sometimes it doesn't need to be. The issie here is not linked to the logs anyway. – JeremyC Mar 16 at 15:51

Your Answer

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

Browse other questions tagged or ask your own question.