/** * extracts visibility from node list, returning an expected integer or translating an expected string * into an expected integer, returning null for unexpected values * @param DOMNodeList $nodes * @return int|null */ public function extractVisibilityValue(DOMNodeList $nodes, Mage_Catalog_Model_Product $product) { $visibilityValues = ['Not Visible Individually' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE, 'Catalog' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, 'Search' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH, 'Catalog, Search' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH]; $visibility = Mage::helper('eb2ccore')->extractNodeVal($nodes); if (in_array($visibility, $visibilityValues)) { // If the value is an expected integer value, return that value return $visibility; } elseif (isset($visibilityValues[$visibility])) { // If the value is an expected string value, return the associated integer return $visibilityValues[$visibility]; } else { // If the value is unexpected, return the current value return $product->getVisibility(); } }
/** * Test if visibility properly saved after import * * @magentoDataFixture Mage/Catalog/_files/multiple_products.php */ public function testSaveProductsVisibility() { $existingProductIds = array(10, 11, 12); $productsBeforeImport = array(); foreach ($existingProductIds as $productId) { $product = new Mage_Catalog_Model_Product(); $product->load($productId); $productsBeforeImport[] = $product; } $source = new Mage_ImportExport_Model_Import_Adapter_Csv(__DIR__ . '/_files/products_to_import.csv'); $this->_model->setParameters(array('behavior' => Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE, 'entity' => 'catalog_product'))->setSource($source)->isDataValid(); $this->_model->importData(); /** @var $productBeforeImport Mage_Catalog_Model_Product */ foreach ($productsBeforeImport as $productBeforeImport) { /** @var $productAfterImport Mage_Catalog_Model_Product */ $productAfterImport = new Mage_Catalog_Model_Product(); $productAfterImport->load($productBeforeImport->getId()); $this->assertEquals($productBeforeImport->getVisibility(), $productAfterImport->getVisibility()); unset($productAfterImport); } unset($productsBeforeImport, $product); }
/** * set visibility */ public function setVisibility() { $level = null; $visibility = new Shopgate_Model_Catalog_Visibility(); switch ($this->item->getVisibility()) { case Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH: $level = Shopgate_Model_Catalog_Visibility::DEFAULT_VISIBILITY_CATALOG_AND_SEARCH; break; case Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG: $level = Shopgate_Model_Catalog_Visibility::DEFAULT_VISIBILITY_CATALOG; break; case Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH: $level = Shopgate_Model_Catalog_Visibility::DEFAULT_VISIBILITY_SEARCH; break; case Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE: $level = Shopgate_Model_Catalog_Visibility::DEFAULT_VISIBILITY_NOT_VISIBLE; break; } $visibility->setLevel($level); $visibility->setMarketplace(true); parent::setVisibility($visibility); }
protected function _isObjectIndexable(Mage_Catalog_Model_Product $object) { if ($object->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) { return false; } if ($object->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG && $object->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) { return false; } return true; }
public function getObject(Mage_Catalog_Model_Product $product) { $type = $this->config->getMappedProductType($product->getTypeId()); $this->logger->start('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($product->storeId)); $this->logger->log('Product type (' . $product->getTypeId() . ', mapped to: ' . $type . ')'); $defaultData = array(); $transport = new Varien_Object($defaultData); Mage::dispatchEvent('algolia_product_index_before', array('product' => $product, 'custom_data' => $transport)); $defaultData = $transport->getData(); $defaultData = is_array($defaultData) ? $defaultData : explode('|', $defaultData); $visibility = (int) $product->getVisibility(); /** @var Mage_Catalog_Model_Product_Visibility $catalogProductVisibility */ $catalogProductVisibility = Mage::getSingleton('catalog/product_visibility'); $visibleInCatalog = $catalogProductVisibility->getVisibleInCatalogIds(); $visibleInSearch = $catalogProductVisibility->getVisibleInSearchIds(); $customData = array('objectID' => $product->getId(), 'name' => $product->getName(), 'url' => $product->getProductUrl(), 'visibility_search' => (int) in_array($visibility, $visibleInSearch), 'visibility_catalog' => (int) in_array($visibility, $visibleInCatalog)); $additionalAttributes = $this->config->getProductAdditionalAttributes($product->getStoreId()); $groups = null; if ($this->isAttributeEnabled($additionalAttributes, 'description')) { $customData['description'] = $product->getDescription(); } $categories = array(); $categories_with_path = array(); $_categoryIds = $product->getCategoryIds(); if (is_array($_categoryIds) && count($_categoryIds) > 0) { $categoryCollection = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name')->addAttributeToFilter('entity_id', $_categoryIds)->addFieldToFilter('level', array('gt' => 1))->addIsActiveFilter(); if ($this->config->showCatsNotIncludedInNavigation($product->getStoreId()) == false) { $categoryCollection->addAttributeToFilter('include_in_menu', '1'); } $rootCat = Mage::app()->getStore($product->getStoreId())->getRootCategoryId(); /** @var Mage_Catalog_Model_Category $category */ foreach ($categoryCollection as $category) { // Check and skip all categories that is not // in the path of the current store. $path = $category->getPath(); $path_parts = explode('/', $path); if (isset($path_parts[1]) && $path_parts[1] != $rootCat) { continue; } $categoryName = $category->getName(); if ($categoryName) { $categories[] = $categoryName; } $category->getUrlInstance()->setStore($product->getStoreId()); $path = array(); foreach ($category->getPathIds() as $treeCategoryId) { $name = $this->getCategoryName($treeCategoryId, $product->getStoreId()); if ($name) { $path[] = $name; } } $categories_with_path[] = $path; } } if ($this->config->indexWholeCategoryTree($product->getStoreId())) { foreach ($categories_with_path as $result) { for ($i = count($result) - 1; $i > 0; $i--) { $categories_with_path[] = array_slice($result, 0, $i); } } } $categories_with_path = array_intersect_key($categories_with_path, array_unique(array_map('serialize', $categories_with_path))); $categories_hierarchical = array(); $mainCategories = array(); $level_name = 'level'; /** @var array $category */ foreach ($categories_with_path as $category) { $categoriesCount = count($category); for ($i = 0; $i < $categoriesCount; $i++) { if (isset($categories_hierarchical[$level_name . $i]) === false) { $categories_hierarchical[$level_name . $i] = array(); } $mainCategories[$level_name . $i][] = $category[$i]; if ($this->config->indexWholeCategoryTree($product->getStoreId())) { $categories_hierarchical[$level_name . $i][] = implode(' /// ', array_slice($category, 0, $i + 1)); } else { if ($i === $categoriesCount - 1) { $categories_hierarchical[$level_name . $i][] = implode(' /// ', $category); } } } } foreach ($categories_hierarchical as &$level) { $level = array_values(array_unique($level)); } foreach ($mainCategories as &$level) { $level = array_values(array_unique($level)); } foreach ($categories_with_path as &$category) { $category = implode(' /// ', $category); } $customData['categories'] = $categories_hierarchical; $customData['categories_without_path'] = $categories; if ($this->isAttributeEnabled($additionalAttributes, 'main_categories')) { $customData['main_categories'] = $mainCategories; } /** @var Algolia_Algoliasearch_Helper_Image $imageHelper */ $imageHelper = Mage::helper('algoliasearch/image'); if (false === isset($defaultData['thumbnail_url'])) { /** @var Algolia_Algoliasearch_Helper_Image $thumb */ $thumb = $imageHelper->init($product, 'thumbnail')->resize(75, 75); try { $customData['thumbnail_url'] = $thumb->toString(); } catch (\Exception $e) { $this->logger->log($e->getMessage()); $this->logger->log($e->getTraceAsString()); $placeholderUrl = Mage::getDesign()->getSkinUrl($thumb->getPlaceholder()); $customData['thumbnail_url'] = $imageHelper->removeProtocol($placeholderUrl); } } if (false === isset($defaultData['image_url'])) { /** @var Algolia_Algoliasearch_Helper_Image $image */ $image = $imageHelper->init($product, $this->config->getImageType())->resize($this->config->getImageWidth(), $this->config->getImageHeight()); try { $customData['image_url'] = $image->toString(); } catch (\Exception $e) { $this->logger->log($e->getMessage()); $this->logger->log($e->getTraceAsString()); $placeholderUrl = Mage::getDesign()->getSkinUrl($image->getPlaceholder()); $customData['image_url'] = $imageHelper->removeProtocol($placeholderUrl); } if ($this->isAttributeEnabled($additionalAttributes, 'media_gallery')) { $product->load('media_gallery'); $customData['media_gallery'] = array(); foreach ($product->getMediaGalleryImages() as $image) { $customData['media_gallery'][] = $imageHelper->removeProtocol($image->getUrl()); } } } $sub_products = null; $ids = null; if ($type == 'configurable' || $type == 'grouped' || $type == 'bundle') { if ($type == 'bundle') { $ids = array(); $selection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product); foreach ($selection as $option) { $ids[] = $option->product_id; } } if ($type == 'configurable' || $type == 'grouped') { $ids = $product->getTypeInstance(true)->getChildrenIds($product->getId()); $ids = call_user_func_array('array_merge', $ids); } if (count($ids)) { $collection = $this->getProductCollectionQuery($product->getStoreId(), $ids, false); $sub_products = $collection->load(); } else { $sub_products = array(); } } if (false === isset($defaultData['in_stock'])) { $stockItem = $product->getStockItem(); $customData['in_stock'] = (int) $stockItem->getIsInStock(); } // skip default calculation if we have provided these attributes via the observer in $defaultData if (false === isset($defaultData['ordered_qty']) && $this->isAttributeEnabled($additionalAttributes, 'ordered_qty')) { $customData['ordered_qty'] = (int) $product->getOrderedQty(); } if (false === isset($defaultData['total_ordered']) && $this->isAttributeEnabled($additionalAttributes, 'total_ordered')) { $customData['total_ordered'] = (int) $product->getTotalOrdered(); } if (false === isset($defaultData['stock_qty']) && $this->isAttributeEnabled($additionalAttributes, 'stock_qty')) { $customData['stock_qty'] = (int) $product->getStockQty(); } if (Mage::helper('core')->isModuleEnabled('Mage_Review')) { if ($this->isAttributeEnabled($additionalAttributes, 'rating_summary')) { $customData['rating_summary'] = (int) $product->getRatingSummary(); } } $this->setNoAttributes($additionalAttributes); foreach ($additionalAttributes as $attribute) { $attribute_name = $attribute['attribute']; if (isset($customData[$attribute_name])) { continue; } $value = $product->getData($attribute_name); /** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute_resource */ $attribute_resource = $product->getResource()->getAttribute($attribute_name); if ($attribute_resource) { $attribute_resource->setStoreId($product->getStoreId()); /** * if $value is missing or if the attribute is SKU, * use values from child products. */ if (($value === null || 'sku' == $attribute_name) && ($type == 'configurable' || $type == 'grouped' || $type == 'bundle')) { if ($value === null) { $values = array(); } else { $values = array($this->getValueOrValueText($product, $attribute_name, $attribute_resource)); } $all_sub_products_out_of_stock = true; if ($type !== 'bundle' || in_array($attribute_name, $this->excludedAttrsFromBundledProducts, true) === false) { foreach ($sub_products as $sub_product) { $isInStock = (int) $sub_product->getStockItem()->getIsInStock(); if ($isInStock == false && $this->config->indexOutOfStockOptions($product->getStoreId()) == false) { continue; } $all_sub_products_out_of_stock = false; $value = $sub_product->getData($attribute_name); if ($value) { $values[] = $this->getValueOrValueText($sub_product, $attribute_name, $attribute_resource); } } } if (is_array($values) && count($values) > 0) { $customData[$attribute_name] = array_values(array_unique($values, SORT_REGULAR)); } // Set main product out of stock if all // sub-products are out of stock. if ($customData['in_stock'] && $all_sub_products_out_of_stock) { $customData['in_stock'] = 0; } } elseif (!is_array($value)) { $value = $this->getValueOrValueText($product, $attribute_name, $attribute_resource); } if ($value && !isset($customData[$attribute_name])) { $customData[$attribute_name] = $value; } } } $msrpEnabled = method_exists(Mage::helper('catalog'), 'canApplyMsrp') ? (bool) Mage::helper('catalog')->canApplyMsrp($product) : false; if (false === $msrpEnabled) { $this->handlePrice($product, $sub_products, $customData); } else { unset($customData['price']); } $transport = new Varien_Object($customData); Mage::dispatchEvent('algolia_subproducts_index', array('custom_data' => $transport, 'sub_products' => $sub_products)); $customData = $transport->getData(); $customData = array_merge($customData, $defaultData); $customData['type_id'] = $type; $this->castProductObject($customData); $customData = $this->clearNoValues($customData); $this->logger->stop('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($product->storeId)); return $customData; }
/** * Get the product ID to use based on Item visibility * * @param Mage_Sales_Model_Order_Item $item * @param Mage_Catalog_Model_Product $itemProduct * @param boolean $checkVisible * * @return int */ protected function _getIdToUse(Mage_Sales_Model_Order_Item $item, Mage_Catalog_Model_Product $itemProduct, $checkVisible = true) { if ($checkVisible && in_array($itemProduct->getVisibility(), array('2', '4'))) { return $item->getProductId(); } else { $superProductConfig = $this->_getSuperProductConfig($item); if ($superProductConfig && array_key_exists('product_id', $superProductConfig)) { return $superProductConfig['product_id']; } elseif (method_exists($item, 'getParentItemId')) { return $item->getParentItemId(); } else { return $item->getProductId(); } } }
/** * @param Mage_Catalog_Model_Product $product * * @return bool */ public function isProductVisibleInCategories($product) { if ($product->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE && $product->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH) { return true; } if (Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_EXPORT_IS_EXPORT_STORES)) { $storesToExport = Mage::helper('shopgate')->getConfig()->getExportStores(true); foreach ($storesToExport as $storeId) { $value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($product->getId(), 'visibility', $storeId); if (in_array($value, array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG))) { return true; } } } return false; }
public function __construct(Mage_Catalog_Model_Product $product) { $this->id = $product->getId(); $this->sku = $product->getSku(); $this->name = $product->getName(); $statuses = Mage::getModel('catalog/product_status')->getOptionArray(); $this->status = $statuses[$product->getStatus()]; $options = Mage::getModel('catalog/product_visibility')->getOptionArray(); $this->visibility = $options[$product->getVisibility()]; $this->price = (double) number_format($product->getPrice(), 2, '.', ''); $this->specialPrice = (double) number_format($product->getSpecialPrice(), 2, '.', ''); $this->url = $product->getProductUrl(); $this->imagePath = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getSmallImage()); $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product); $this->stock = (double) number_format($stock->getQty(), 2, '.', ''); $short_description = $product->getShortDescription(); //limit short description if (strlen($short_description) > 250) { $short_description = substr($short_description, 0, 250); } $this->short_description = $short_description; //category data $count = 0; $categoryCollection = $product->getCategoryCollection()->addNameToResult(); foreach ($categoryCollection as $cat) { $this->categories[$count]['Id'] = $cat->getId(); $this->categories[$count]['Name'] = $cat->getName(); $count++; } //website data $count = 0; $websiteIds = $product->getWebsiteIds(); foreach ($websiteIds as $websiteId) { $website = Mage::app()->getWebsite($websiteId); $this->websites[$count]['Id'] = $website->getId(); $this->websites[$count]['Name'] = $website->getName(); $count++; } //bundle product options if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) { $optionCollection = $product->getTypeInstance()->getOptionsCollection(); $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds()); $options = $optionCollection->appendSelections($selectionCollection); foreach ($options as $option) { $count = 0; $title = str_replace(' ', '', $option->getDefaultTitle()); $selections = $option->getSelections(); $sOptions = array(); foreach ($selections as $selection) { $sOptions[$count]['name'] = $selection->getName(); $sOptions[$count]['sku'] = $selection->getSku(); $sOptions[$count]['id'] = $selection->getProductId(); $sOptions[$count]['price'] = (double) number_format($selection->getPrice(), 2, '.', ''); $count++; } $this->{$title} = $sOptions; } } //configurable product options if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { $productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product); foreach ($productAttributeOptions as $productAttribute) { $count = 0; $label = strtolower(str_replace(' ', '', $productAttribute['label'])); $options = array(); foreach ($productAttribute['values'] as $attribute) { $options[$count]['option'] = $attribute['default_label']; $options[$count]['price'] = (double) number_format($attribute['pricing_value'], 2, '.', ''); $count++; } $this->{$label} = $options; } } }
/** * Is this product visible individually? * * @param Mage_Catalog_Model_Product $product * @return boolean */ public function isVisibleInidividually($product) { return $product->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE; }
/** * Checks that the configurable product exists and can be show to the * customer * * @return bool */ protected function _canRedirect() { return $this->_product && $this->_product->getId() && $this->_product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_DISABLED && $this->_product->getVisibility() != Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE; }