Example #1
0
 /**
  * @param Mage_Catalog_Model_Resource_Product_Collection $products
  * @return bool
  */
 protected function _process($products)
 {
     $processor = $this->_htmlProcessor;
     $helper = $this->_getHelper();
     foreach ($products as $product) {
         $product->setData('url', $product->getProductUrl());
     }
     $items = $processor->query($helper->getCssSelector('related_product_link'));
     if (count($items) === 0) {
         return false;
     }
     $positions = array();
     foreach ($items as $i => $item) {
         /** @var $item DOMElement */
         $url = $item->getAttribute('href');
         $product = $products->getItemByColumnValue('url', $url);
         if (!$product) {
             continue;
         }
         if ($helper->isEnabledForProduct($product)) {
             $positions[] = $i;
         }
     }
     $processor->replace($helper->getCssSelector('related_product_price'), $this->_getHelper()->prepareReplacement(), $positions);
 }
 /**
  * Update product links for the given type of links - up sell, related, cross sell
  * @param  Mage_Catalog_Model_Product $product     Product to add links to
  * @param  array                      $linkUpdates Product links data
  * @param  string                     $linkType    Type of product links to create
  * @return array                                   Any links that could not be added
  */
 protected function _linkProducts(Mage_Catalog_Model_Product $product, $linkUpdates, $linkType)
 {
     $opFilter = function ($operation) {
         return function ($el) use($operation) {
             return strtolower($el['operation_type']) === $operation;
         };
     };
     $skuMap = function ($link) {
         return $link['link_to_unique_id'];
     };
     // get all currently linked products of this link type
     $linkedProductsGetter = $this->_linkTypes[$linkType]['linked_products_getter_method'];
     $linkedProducts = $product->{$linkedProductsGetter}();
     // remove any links that are supposed to be getting deleted
     $deleteSkus = array_map($skuMap, array_filter($linkUpdates, $opFilter('delete')));
     $linkedProducts = array_filter($linkedProducts, function ($prod) use($deleteSkus) {
         return !in_array($prod->getSku(), $deleteSkus);
     });
     $addSkus = array_map($skuMap, array_filter($linkUpdates, $opFilter('add')));
     // Go through the skus to add and look up the product id for each one.
     // If any are missing, add the product link for that sku to a list of links that
     // cannot be resolved yet.
     $missingLinks = array();
     $idsToAdd = array();
     foreach ($addSkus as $sku) {
         $linkProduct = $this->_products->getItemByColumnValue('sku', $sku);
         $id = $linkProduct ? $linkProduct->getId() : null;
         if ($id) {
             $idsToAdd[] = $id;
         } else {
             $missingLinks[] = $this->_buildProductLinkForSku($sku, $linkType, 'Add');
         }
     }
     // get a list of all products that should be linked
     $linkIds = array_filter(array_unique(array_merge(array_map(function ($prod) {
         return $prod->getId();
     }, $linkedProducts), $idsToAdd)));
     $linkData = array();
     foreach ($linkIds as $id) {
         $linkData[$id] = array('position' => '');
     }
     // add the updated links to the product
     $product->setData($this->_linkTypes[$linkType]['data_attribute'], $linkData);
     // return links that were not resolved
     return $missingLinks;
 }
 /**
  * given a Mage_Catalog_Model_Resource_Product_Collection object and a list of SKUs
  * if the SKUs in the list of sku is not in the collection then add error confirmation node
  * to feed file about sku was not imported
  * @param  Mage_Catalog_Model_Resource_Product_Collection $collection
  * @param  array list of SKUs that were suppose to be imported
  * @param  string the file the sku was found on
  * @param  string the event type
  * @return self
  */
 protected function addImportErrors(Mage_Catalog_Model_Resource_Product_Collection $collection, array $skus, $fileName, $type)
 {
     foreach ($skus as $sku) {
         $product = $collection->getItemByColumnValue('sku', $sku);
         if (is_null($product)) {
             $this->appendError(self::SKU_NOT_IMPORTED, '', $type, $fileName, $sku);
         }
     }
     return $this;
 }