/** * Prepare tier prices for save * * @param Mage_Catalog_Model_Product $product * @param array $tierPrices * @return array */ public function prepareTierPrices($product, $tierPrices = null) { if (!is_array($tierPrices)) { return null; } $updateValue = array(); foreach ($tierPrices as $tierPrice) { if (!is_object($tierPrice) || !isset($tierPrice->qty) || !isset($tierPrice->price)) { $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid Tier Prices')); } if (!isset($tierPrice->website) || $tierPrice->website == 'all') { $tierPrice->website = 0; } else { try { $tierPrice->website = Mage::app()->getWebsite($tierPrice->website)->getId(); } catch (Mage_Core_Exception $e) { $tierPrice->website = 0; } } if (intval($tierPrice->website) > 0 && !in_array($tierPrice->website, $product->getWebsiteIds())) { $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid tier prices. The product is not associated to the requested website.')); } if (!isset($tierPrice->customer_group_id)) { $tierPrice->customer_group_id = 'all'; } if ($tierPrice->customer_group_id == 'all') { $tierPrice->customer_group_id = Mage_Customer_Model_Group::CUST_GROUP_ALL; } $updateValue[] = array('website_id' => $tierPrice->website, 'cust_group' => $tierPrice->customer_group_id, 'price_qty' => $tierPrice->qty, 'price' => $tierPrice->price); } return $updateValue; }
public function testGetWebsiteIds() { // set /** @var $model Mage_Catalog_Model_Product */ $model = Mage::getModel('Mage_Catalog_Model_Product', array('data' => array('website_ids' => array(1, 2)))); $this->assertEquals(array(1, 2), $model->getWebsiteIds()); // fixture $this->_model->setId(1); $this->assertEquals(array(1), $this->_model->getWebsiteIds()); }
public function tryToPerformWebsiteProductActions(Mage_Catalog_Model_Product $productNew, $isJustAddedProduct, $websiteIdsOld) { $websiteIdsNew = $productNew->getWebsiteIds(); if ($isJustAddedProduct) { $websiteIdsNew[] = 0; } $addedWebsiteIds = array_diff($websiteIdsNew, $websiteIdsOld); foreach ($addedWebsiteIds as $websiteId) { $this->synchProductWithAddedWebsiteId($productNew, $websiteId); } $deletedWebsiteIds = array_diff($websiteIdsOld, $websiteIdsNew); foreach ($deletedWebsiteIds as $websiteId) { $this->synchProductWithDeletedWebsiteId($productNew, $websiteId); } }
private function processCategoriesActions(Mage_Catalog_Model_Product $product) { $productCategories = $product->getCategoryIds(); $categoriesByWebsite = array(0 => $productCategories); foreach ($product->getWebsiteIds() as $websiteId) { $categoriesByWebsite[$websiteId] = $productCategories; } /** @var Ess_M2ePro_Model_Listing_Auto_Actions_Mode_Category $autoActionsCategory */ $autoActionsCategory = Mage::getModel('M2ePro/Listing_Auto_Actions_Mode_Category'); $autoActionsCategory->setProduct($product); foreach ($categoriesByWebsite as $websiteId => $categoryIds) { foreach ($categoryIds as $categoryId) { $autoActionsCategory->synchWithAddedCategoryId($categoryId, $websiteId); } } }
private function processCategoriesActions(Mage_Catalog_Model_Product $magentoProduct) { $productCategories = $magentoProduct->getCategoryIds(); $categoriesByWebsite = array(0 => $productCategories); foreach ($magentoProduct->getWebsiteIds() as $websiteId) { $categoriesByWebsite[$websiteId] = $productCategories; } /** @var Ess_M2ePro_Model_Observer_Category $categoryObserverModel */ $categoryObserverModel = Mage::getModel('M2ePro/Observer_Category'); /** @var Ess_M2ePro_Model_Observer_Ebay_Category $ebayCategoryObserver */ $ebayCategoryObserver = Mage::getModel('M2ePro/Observer_Ebay_Category'); foreach ($categoriesByWebsite as $websiteId => $categoriesIds) { foreach ($categoriesIds as $categoryId) { $categoryObserverModel->synchProductWithAddedCategoryId($magentoProduct, $categoryId, $websiteId); $ebayCategoryObserver->synchProductWithAddedCategoryId($magentoProduct, $categoryId, $websiteId); } } }
/** * Create Product array from Mage_Catalog_Model_Product * * @param Mage_Catalog_Model_Product $product * @return array */ public function getProductData(Mage_Catalog_Model_Product $product) { try { $data = array('url' => $product->getProductUrl(), 'title' => htmlspecialchars($product->getName()), 'spider' => 1, 'price' => $product->getPrice(), 'description' => urlencode($product->getDescription()), 'tags' => htmlspecialchars($product->getMetaKeyword()), 'images' => array(), 'vars' => array('sku' => $product->getSku(), 'storeId' => '', 'typeId' => $product->getTypeId(), 'status' => $product->getStatus(), 'categoryId' => $product->getCategoryId(), 'categoryIds' => $product->getCategoryIds(), 'websiteIds' => $product->getWebsiteIds(), 'storeIds' => $product->getStoreIds(), 'groupPrice' => $product->getGroupPrice(), 'formatedPrice' => $product->getFormatedPrice(), 'calculatedFinalPrice' => $product->getCalculatedFinalPrice(), 'minimalPrice' => $product->getMinimalPrice(), 'specialPrice' => $product->getSpecialPrice(), 'specialFromDate' => $product->getSpecialFromDate(), 'specialToDate' => $product->getSpecialToDate(), 'relatedProductIds' => $product->getRelatedProductIds(), 'upSellProductIds' => $product->getUpSellProductIds(), 'getCrossSellProductIds' => $product->getCrossSellProductIds(), 'isSuperGroup' => $product->isSuperGroup(), 'isGrouped' => $product->isGrouped(), 'isConfigurable' => $product->isConfigurable(), 'isSuper' => $product->isSuper(), 'isSalable' => $product->isSalable(), 'isAvailable' => $product->isAvailable(), 'isVirtual' => $product->isVirtual(), 'isRecurring' => $product->isRecurring(), 'isInStock' => $product->isInStock(), 'weight' => $product->getSku())); // Add product images if (self::validateProductImage($product->getImage())) { $data['images']['full'] = array("url" => $product->getImageUrl()); } if (self::validateProductImage($product->getSmallImage())) { $data['images']['smallImage'] = array("url" => $product->getSmallImageUrl($width = 88, $height = 77)); } if (self::validateProductImage($product->getThumbnail())) { $data['images']['thumb'] = array("url" => $product->getThumbnailUrl($width = 75, $height = 75)); } return $data; return $data; } catch (Exception $e) { Mage::logException($e); } }
/** * Save product website relations * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product */ protected function _saveWebsiteIds($product) { $websiteIds = $product->getWebsiteIds(); $oldWebsiteIds = array(); $product->setIsChangedWebsites(false); $select = $this->_getWriteAdapter()->select()->from($this->_productWebsiteTable)->where('product_id=?', $product->getId()); $query = $this->_getWriteAdapter()->query($select); while ($row = $query->fetch()) { $oldWebsiteIds[] = $row['website_id']; } $insert = array_diff($websiteIds, $oldWebsiteIds); $delete = array_diff($oldWebsiteIds, $websiteIds); if (!empty($insert)) { foreach ($insert as $websiteId) { $this->_getWriteAdapter()->insert($this->_productWebsiteTable, array('product_id' => $product->getId(), 'website_id' => $websiteId)); } } if (!empty($delete)) { foreach ($delete as $websiteId) { $this->_getWriteAdapter()->delete($this->_productWebsiteTable, array($this->_getWriteAdapter()->quoteInto('product_id=?', $product->getId()), $this->_getWriteAdapter()->quoteInto('website_id=?', $websiteId))); } } if (!empty($insert) || !empty($delete)) { $product->setIsChangedWebsites(true); } return $this; }
/** * Refresh Product Enabled Index * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_Model_Resource_Product */ public function refreshIndex($product) { $writeAdapter = $this->_getWriteAdapter(); /** * Ids of all categories where product is assigned (not related with store) */ $categoryIds = $product->getCategoryIds(); /** * Clear previous index data related with product */ $condition = array('product_id = ?' => (int) $product->getId()); $writeAdapter->delete($this->getTable('catalog/category_product_index'), $condition); /** @var $categoryObject Mage_Catalog_Model_Resource_Category */ $categoryObject = Mage::getResourceSingleton('catalog/category'); if (!empty($categoryIds)) { $categoriesSelect = $writeAdapter->select()->from($this->getTable('catalog/category'))->where('entity_id IN (?)', $categoryIds); $categoriesInfo = $writeAdapter->fetchAll($categoriesSelect); $indexCategoryIds = array(); foreach ($categoriesInfo as $categoryInfo) { $ids = explode('/', $categoryInfo['path']); $ids[] = $categoryInfo['entity_id']; $indexCategoryIds = array_merge($indexCategoryIds, $ids); } $indexCategoryIds = array_unique($indexCategoryIds); $indexProductIds = array($product->getId()); $categoryObject->refreshProductIndex($indexCategoryIds, $indexProductIds); } else { $websites = $product->getWebsiteIds(); if ($websites) { $storeIds = array(); foreach ($websites as $websiteId) { $website = Mage::app()->getWebsite($websiteId); $storeIds = array_merge($storeIds, $website->getStoreIds()); } $categoryObject->refreshProductIndex(array(), array($product->getId()), $storeIds); } } /** * Refresh enabled products index (visibility state) */ $this->refreshEnabledIndex(null, $product); return $this; }
/** * Validate is valid association for website unassignment from product. * If fails validation, then this method returns false, and * getErrors() will return an array of errors that explain why the * validation failed. * * @param Mage_Core_Model_Website $website * @param Mage_Catalog_Model_Product $product * @return bool */ public function isWebsiteAssignedToProduct(Mage_Core_Model_Website $website, Mage_Catalog_Model_Product $product) { if (false === array_search($website->getId(), $product->getWebsiteIds())) { $this->_addError(sprintf('Product #%d isn\'t assigned to website #%d', $product->getId(), $website->getId())); } return !count($this->getErrors()); }
public function testGetWebsiteIds() { // set $model = new Mage_Catalog_Model_Product(array('website_ids' => array(1, 2))); $this->assertEquals(array(1, 2), $model->getWebsiteIds()); // fixture $this->_model->setId(1); $this->assertEquals(array(1), $this->_model->getWebsiteIds()); }
/** * Fill simple product data during generation * * @param Mage_Catalog_Model_Product $product * @param Mage_Catalog_Model_Product $parentProduct * @param array $postData */ protected function _fillSimpleProductData(Mage_Catalog_Model_Product $product, Mage_Catalog_Model_Product $parentProduct, $postData) { $product->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->setTypeId($postData['weight'] ? Mage_Catalog_Model_Product_Type::TYPE_SIMPLE : Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL)->setAttributeSetId($parentProduct->getNewVariationsAttributeSetId()); foreach ($product->getTypeInstance()->getEditableAttributes($product) as $attribute) { if ($attribute->getIsUnique() || $attribute->getAttributeCode() == 'url_key' || $attribute->getFrontend()->getInputType() == 'gallery' || $attribute->getFrontend()->getInputType() == 'media_image' || !$attribute->getIsVisible()) { continue; } $product->setData($attribute->getAttributeCode(), $parentProduct->getData($attribute->getAttributeCode())); } if (!isset($postData['stock_data']['use_config_manage_stock'])) { $postData['stock_data']['use_config_manage_stock'] = 0; } $product->addData($postData)->setWebsiteIds($parentProduct->getWebsiteIds())->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); }
/** * getWebClassesFromProduct * * @param Mage_Catalog_Model_Product $product * @return string */ public function getWebClassesFromProduct($product) { $websites = $product->getWebsiteIds(); $websiteClasses = ''; foreach ($websites as $websiteId) { $websiteClasses .= "website-" . $websiteId . " "; } return $websiteClasses; }
/** * Catalog product validate before delete * * @param Mage_Catalog_Model_Product $model */ public function catalogProductDeleteBefore($model) { // deleting only in exclusive mode if (!$this->_role->hasExclusiveAccess($model->getWebsiteIds())) { $this->_throwDelete(); } }
/** * Check product availability for current website * * @param Mage_Catalog_Model_Product $product * @return bool|string */ protected function _validateProductWebsite($product) { if (in_array($this->getCurrentStore()->getWebsiteId(), $product->getWebsiteIds())) { return true; } return Mage::app()->getStore()->isAdmin() ? Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_WEBSITE : Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_SKU; }
/** * Save product website relations * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product */ protected function _saveWebsiteIds($product) { $ids = $product->getWebsiteIds(); $this->_getWriteAdapter()->delete($this->_productWebsiteTable, $this->_getWriteAdapter()->quoteInto('product_id=?', $product->getId())); foreach ($ids as $websiteId) { $this->_getWriteAdapter()->insert($this->_productWebsiteTable, array('product_id' => $product->getId(), 'website_id' => $websiteId)); } return $this; }
/** * Map an array of website ids the product should be linked to, based upon * the item's client id, catalog id and/or store id. * * @param DOMNodeList * @param Mage_Catalog_Model_Product * @return array */ public function extractWebsiteIds(DOMNodeList $nodes, Mage_Catalog_Model_Product $product) { $websiteIds = $product->getWebsiteIds(); $itemNode = $nodes->item(0); // If there's no node to get data from, just let website ids remain // what it is. if (!$itemNode) { return $websiteIds; } $clientId = $itemNode->getAttribute('gsi_client_id'); $catalogId = $itemNode->getAttribute('catalog_id'); $storeId = $itemNode->getAttribute('gsi_store_id'); $websites = $this->catalogHelper->loadWebsiteFilters(); foreach ($websites as $websiteFilter) { $websiteId = $websiteFilter['mage_website_id']; // Assume no value in feed is a wildcard. $matches = !$catalogId || $catalogId === $websiteFilter['catalog_id']; $matches = $matches && (!$clientId || $clientId === $websiteFilter['client_id']); $matches = $matches && (!$storeId || $storeId === $websiteFilter['store_id']); // Just add any matched websites to the list, ignoring that this // might end up with dupes, list will be made unique when returned. if ($matches) { $websiteIds[] = $websiteId; } } return array_unique($websiteIds); }