5

What's the function of $editMode and $forceReload parameter in ProductRepository?

If I set $editMode=true or $forceReload=true, what will happen?

When should I set the parameter is true when using the ProductRepository?


Below is the codes in ProductRepository:

public function get($sku, $editMode = false, $storeId = null, $forceReload = false)
{
    $cacheKey = $this->getCacheKey([$editMode, $storeId]);
    if (!isset($this->instances[$sku][$cacheKey]) || $forceReload) {
        $product = $this->productFactory->create();

        $productId = $this->resourceModel->getIdBySku($sku);
        if (!$productId) {
            throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
        }
        if ($editMode) {
            $product->setData('_edit_mode', true);
        }
        if ($storeId !== null) {
            $product->setData('store_id', $storeId);
        }
        $product->load($productId);
        $this->cacheProduct($cacheKey, $product);
    }
    if (!isset($this->instances[$sku])) {
        $sku = trim($sku);
    }
    return $this->instances[$sku][$cacheKey];
}
1

As far as I can see, if you set either the editMode or the forceReload true, you will receive a fresh loaded model from the DB. Otherwise, you'll just receive a model from the cache.

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.