Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductToWebsiteChangeObserver.php
Go to the documentation of this file.
1 <?php
7 
16 
18 {
23 
27  protected $urlPersist;
28 
32  protected $productRepository;
33 
37  protected $request;
38 
45  public function __construct(
50  ) {
51  $this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
52  $this->urlPersist = $urlPersist;
53  $this->productRepository = $productRepository;
54  $this->request = $request;
55  }
56 
63  public function execute(\Magento\Framework\Event\Observer $observer)
64  {
65  foreach ($observer->getEvent()->getProducts() as $productId) {
66  $product = $this->productRepository->getById(
67  $productId,
68  false,
69  $this->request->getParam('store_id', Store::DEFAULT_STORE_ID)
70  );
71 
72  $this->urlPersist->deleteByData([
73  UrlRewrite::ENTITY_ID => $product->getId(),
75  ]);
76  if ($product->getVisibility() != Visibility::VISIBILITY_NOT_VISIBLE) {
77  $this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
78  }
79  }
80  }
81 }
__construct(ProductUrlRewriteGenerator $productUrlRewriteGenerator, UrlPersistInterface $urlPersist, ProductRepositoryInterface $productRepository, RequestInterface $request)