0

i have multiple store view, and i create product programmatically with ProductInterfaceFactory, product successfully insert, but when i changing store view in front end, product page result not found, because table url_rewrite only has one row url store (store id 1). how to fix it ?

        $product = $this->productFactory->create();
        $product->setSku($data['sku']);
        //$product->setStoreIds(array("1,2")); not working
        //$product->setStoreId("2"); not working
        $product->setName($data['name']);
        $product->setDescription($data['description']);
        $product->setTypeId("simple");
        $product->setVisibility(4); //catalog & search
        $product->setPrice($data['price']);
        $product->setAttributeSetId(4);
        $product->setStatus(1);
        $product->setStockData(['qty' => 50, 'is_in_stock' => 1]);
        $product = $this->productRepository->save($product);

1 Answer 1

0

It seems a possible way forward is like below; mostly, the setCopyFromView is what seems to make this possible. I found this in the function \Magento\Catalog\Controller\Adminhtml\Product\Save::copyToStore. I admit I find this quite flawed as the name, description for instance are duplicated regardless if their value is different but hopefully that may push you to the next stage.

        $data['sku'] = 'sku123';
        $data['name'] = 'Product test 123';
        $data['description'] = 'Product description test 123';
        $data['price'] = 123;

        try {
            $product = $this->productRepository->get($data['sku'], true, Store::DEFAULT_STORE_ID);
        } catch (\Exception $e) {
            $product = $this->productFactory->create();
            $product->setSku($data['sku']);
        }

        //$product->setStoreIds(array("1,2")); not working
        //$product->setStoreId("2"); not working
        $product->setName($data['name']);
        $product->setDescription($data['description']);
        $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
        $product->setVisibility(Visibility::VISIBILITY_BOTH); //catalog & search
        $product->setPrice($data['price']);
        $product->setAttributeSetId(4);
        $product->setStatus(Status::STATUS_ENABLED);
        $product->setStockData(['qty' => 50, 'is_in_stock' => 1]);
        $product = $this->productRepository->save($product);

        $storeIds = [1, 2];
        foreach ($storeIds as $storeId) {
            $this->product->setStoreId(Store::DEFAULT_STORE_ID)
                ->load($product->getId())
                ->setStoreId($storeId)
                ->setCopyFromView(true)
                ->save();
        }
11
  • not working. url_rewrite only have store_id 1,
    – Egi
    Oct 4, 2020 at 15:14
  • You might want to reindex of course Oct 4, 2020 at 15:20
  • @HerveTribouilloy Could you please help me to get all dashboard's link inside top link also? I have posted question. Oct 4, 2020 at 15:26
  • I did read your question. you need to be more specific, is it sign out to go in my account sidebar you are after? Oct 4, 2020 at 15:45
  • @HerveTribouilloy still can't, finally im using $item->setStoreId($storeId); $this->urlPersist->replace( $this->productUrlRewriteGenerator->generate($item)); on my collection product id, but i don't know if this is right way
    – Egi
    Oct 4, 2020 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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