/** * @param Mage_Catalog_Model_Product $child * @return $this * @throws InvalidArgumentException */ public function setPriceFromChild(Mage_Catalog_Model_Product $child) { if ($child === null) { throw new InvalidArgumentException("Product passed is a non-object"); } $price = $child->getPrice(); $specialPrice = $child->getSpecialPrice(); $specialFrom = $child->getSpecialFromDate(); $specialTo = $child->getSpecialToDate(); $this->setPrice($price, $specialPrice, $specialFrom, $specialTo); $this->_priceChild = $child; return $this; }
/** * 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); } }
/** * Apply special price for product if not return price that was before * * @param Mage_Catalog_Model_Product $product * @param float $finalPrice * @return float */ protected function _applySpecialPrice($product, $finalPrice) { return $this->calculateSpecialPrice($finalPrice, $product->getSpecialPrice(), $product->getSpecialFromDate(), $product->getSpecialToDate(), $product->getStore()); }
public function getProductEntity(Mage_Catalog_Model_Product $product, $storeId, $includeExtras = true) { $result = array(); $result['entity_id'] = $product->getEntityId(); $result['sku'] = $product->getSku(); $result['name'] = $product->getName(); $result['price'] = $product->getPrice(); $result['special_price'] = $product->getSpecialPrice(); $result['special_from_date'] = $product->getSpecialFromDate(); $result['special_to_date'] = $product->getSpecialToDate(); $result['cost'] = $product->getCost(); $result['description'] = $product->getDescription(); $result['short_description'] = $product->getShortDescription(); $result['weight'] = $product->getWeight(); if ($product->isVisibleInSiteVisibility()) { $result['url_path'] = $this->_getProductUrlWithCache($product); } $parentProduct = $this->_getParentProduct($product); if ($parentProduct != null) { $result['parent_id'] = $parentProduct->getEntityId(); $result['parent_sku'] = $parentProduct->getSku(); if (!$product->isVisibleInSiteVisibility()) { $result['name'] = $parentProduct->getName(); if ($parentProduct->isVisibleInSiteVisibility()) { $result['url_path'] = $this->_getProductUrlWithCache($parentProduct); } } if ($includeExtras && Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE == $parentProduct->getTypeId()) { $result['purchasable'] = $this->_isPurchasable($product, $parentProduct); $attributes = $parentProduct->getTypeInstance(true)->getUsedProductAttributes($parentProduct); $freshProduct = null; foreach ($attributes as $attribute) { if (!array_key_exists('configurable_attributes', $result)) { $result['configurable_attributes'] = array(); } $attr = array(); $attr['attribute_name'] = $attribute->getFrontend()->getLabel(); $attr['value'] = $product->getAttributeText($attribute->getAttributeCode()); if (empty($attr['value'])) { // use the EAV tables only if the flat table doesn't work if ($freshProduct == null) { $freshProduct = Mage::getModel('catalog/product')->load($product->getEntityId()); } $attr['value'] = $attribute->getFrontend()->getValue($freshProduct); } $result['configurable_attributes'][] = $attr; } } } if (!isset($result['purchasable'])) { $result['purchasable'] = $this->_isPurchasable($product); } $images = $this->_getProductImages($product); if (isset($images['image'])) { $result['image'] = $images['image']; } if (isset($images['small_image'])) { $result['small_image'] = $images['small_image']; } if (isset($images['thumbnail'])) { $result['thumbnail'] = $images['thumbnail']; } if ($includeExtras) { // Metas $metas = $this->_getMetas($storeId, $product, $parentProduct); if ($metas != null) { //if(isset($metas['meta1'])) $result['meta1'] = $metas['meta1']; //if(isset($metas['meta2'])) $result['meta2'] = $metas['meta2']; //if(isset($metas['meta3'])) $result['meta3'] = $metas['meta3']; //if(isset($metas['meta4'])) $result['meta4'] = $metas['meta4']; if (isset($metas['meta5'])) { $result['meta5'] = $metas['meta5']; } } //Custom For ST Bernard Sports // Set IsNew Flag if product is less then 45 days old. $result['meta4'] = strtotime($product->getCreatedAt()) + 45 * 24 * 60 * 60 < time() ? "False" : "True"; // Brand and Category $brandAndCategoryProduct = !$parentProduct || $product->isVisibleInSiteVisibility() ? $product : $parentProduct; $setSettings = $this->_getProductAttributeSetSettings($brandAndCategoryProduct); if ($setSettings['brandAttribute'] != null) { $result['brand'] = $brandAndCategoryProduct->getAttributeText($setSettings['brandAttribute']); } if ($setSettings['catFromMagento']) { $cats = $this->_getCategoryInformation($storeId, $brandAndCategoryProduct); if (isset($cats['category'])) { $result['category'] = $cats['category']; } if (isset($cats['sub_category'])) { $result['sub_category'] = $cats['sub_category']; } //Custom For ST Bernard Sports if (isset($cats['third_category'])) { $result['meta3'] = $cats['third_category']; } } else { if ($setSettings['catFromAttributes']) { if ($setSettings['categoryAttribute'] != null) { $result['category'] = $brandAndCategoryProduct->getAttributeText($setSettings['categoryAttribute']); } if ($setSettings['subcategoryAttribute'] != null) { $result['sub_category'] = $brandAndCategoryProduct->getAttributeText($setSettings['subcategoryAttribute']); } } } // Inventory $result['in_stock'] = $product->isAvailable() ? "true" : "false"; $stockItem = $product->getStockItem(); if ($stockItem) { $result['qty_on_hand'] = $stockItem->getStockQty(); } // Related Products $result['links'] = $this->_getProductLinks($product); } $result['type'] = $product->getTypeId(); return $result; }
/** * Get product final price * * @param double $qty * @param Mage_Catalog_Model_Product $product * @return double */ public function getFinalPrice($qty = null, $product) { /** * Calculating final price for item of configurable product */ if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) { $finalPrice = $product->getSuperProduct()->getFinalPrice($qty); $attributes = $product->getSuperProduct()->getTypeInstance()->getConfigurableAttributes(); foreach ($attributes as $attribute) { $value = $this->getValueByIndex($attribute->getPrices(), $product->getData($attribute->getProductAttribute()->getAttributeCode())); if ($value) { if ($value['pricing_value'] != 0) { $finalPrice += $product->getSuperProduct()->getPricingValue($value); } } } } else { $finalPrice = $product->getPrice(); $tierPrice = $product->getTierPrice($qty); if (is_numeric($tierPrice)) { $finalPrice = min($finalPrice, $tierPrice); } $specialPrice = $product->getSpecialPrice(); if (is_numeric($specialPrice)) { $today = floor(time() / 86400) * 86400; #echo " TEST:"; echo date('Y-m-d H:i:s', $today).' , '.$product->getSpecialToDate(); if ($product->getSpecialFromDate() && $today < strtotime($product->getSpecialFromDate())) { #echo ' test1: '.$product->getSpecialFromDate(); } elseif ($product->getSpecialToDate() && $today > strtotime($product->getSpecialToDate())) { #echo ' test2: '.$product->getSpecialToDate(); } else { $finalPrice = min($finalPrice, $specialPrice); } } } $product->setFinalPrice($finalPrice); Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product)); return $product->getData('final_price'); }
/** * Inflate an API object from a product object * * @param Mage_Catalog_Model_Product $product Product * @param int $storeId Magento store ID * @param bool $includeExtras Retrieve all information * * @return array */ public function getProductEntity(Mage_Catalog_Model_Product $product, $storeId, $includeExtras = true) { $result = array(); $result['entity_id'] = $product->getEntityId(); $result['sku'] = $product->getSku(); $result['name'] = $product->getName(); $result['price'] = $product->getPrice(); $result['special_price'] = $product->getSpecialPrice(); $result['special_from_date'] = $product->getSpecialFromDate(); $result['special_to_date'] = $product->getSpecialToDate(); $result['cost'] = $product->getCost(); $result['description'] = $product->getDescription(); $result['short_description'] = $product->getShortDescription(); $result['weight'] = $product->getWeight(); if ($product->isVisibleInSiteVisibility()) { $result['url_path'] = $this->_getProductUrlWithCache($product); } $parentProduct = $this->_getParentProduct($product); if ($parentProduct != null) { $result['parent_id'] = $parentProduct->getEntityId(); $result['parent_sku'] = $parentProduct->getSku(); if (!$product->isVisibleInSiteVisibility()) { $result['name'] = $parentProduct->getName(); if ($parentProduct->isVisibleInSiteVisibility()) { $result['url_path'] = $this->_getProductUrlWithCache($parentProduct); } } if ($includeExtras && $this->_isConfigurableProduct($parentProduct)) { $result['purchasable'] = $this->_isPurchasable($product, $parentProduct); /* @var Mage_Catalog_Model_Product_Type_Configurable $typeInst */ $typeInst = $parentProduct->getTypeInstance(true); $attributes = $typeInst->getUsedProductAttributes($parentProduct); /* @var Mage_Eav_Model_Entity_Attribute_Abstract $attribute */ foreach ($attributes as $attribute) { if (!array_key_exists('configurable_attributes', $result)) { $result['configurable_attributes'] = array(); } $result['configurable_attributes'][] = array('attribute_name' => $attribute->getAttributeCode()); } } } if (!isset($result['purchasable'])) { $result['purchasable'] = $this->_isPurchasable($product); } $images = $this->_getProductImages($product); if (isset($images['image'])) { $result['image'] = $images['image']; } if (isset($images['small_image'])) { $result['small_image'] = $images['small_image']; } if (isset($images['thumbnail'])) { $result['thumbnail'] = $images['thumbnail']; } if ($includeExtras) { $metas = $this->_getMetas($storeId, $product, $parentProduct); if ($metas != null) { /*if (isset($metas['meta3'])) { $result['meta3'] = $metas['meta3']; }*/ if (isset($metas['meta4'])) { $result['meta4'] = $metas['meta4']; } if (isset($metas['meta5'])) { $result['meta5'] = $metas['meta5']; } } //Custom for Sportys $stores = array(); foreach ($product->getStoreIds() as $storeID) { $store = Mage::getModel('core/store')->load($storeID); $stores[] = $store->getCode(); } $result["meta3"] = implode(',', $stores); // Brand and Category $brandCatProduct = $product; if ($parentProduct && !$product->isVisibleInSiteVisibility()) { $brandCatProduct = $parentProduct; } $setSettings = $this->_getProductAttributeSetSettings($brandCatProduct); if ($setSettings['brandAttribute'] != null) { $result['brand'] = $this->_getProductAttribute($brandCatProduct, $setSettings['brandAttribute']); } if ($setSettings['catFromMagento']) { $cats = $this->_getCategoryInformation($storeId, $brandCatProduct); if (isset($cats['category'])) { $result['category'] = $cats['category']; } if (isset($cats['sub_category'])) { $result['sub_category'] = $cats['sub_category']; } } else { if ($setSettings['catFromAttributes']) { if ($setSettings['categoryAttribute'] != null) { $result['category'] = $this->_getProductAttribute($brandCatProduct, $setSettings['categoryAttribute']); } if ($setSettings['subcategoryAttribute'] != null) { $result['sub_category'] = $this->_getProductAttribute($brandCatProduct, $setSettings['subcategoryAttribute']); } } } $result['attributes'] = $this->_getProductAttributes($product); // Inventory $result['in_stock'] = $product->isAvailable() ? "true" : "false"; /* @var Mage_Cataloginventory_Model_Stock_Item $stockItem */ $stockItem = $product->getStockItem(); if ($stockItem) { $result['qty_on_hand'] = $stockItem->getStockQty(); } // Related Products $result['links'] = $this->_getProductLinks($product); } $result['type'] = $product->getTypeId(); return $result; }
/** * Apply special price for product if not return price that was before * * @param Mage_Catalog_Model_Product $product * @param double $finalPrice * @return double */ protected function _applySpecialPrice($product, $finalPrice) { $specialPrice = $product->getSpecialPrice(); if (is_numeric($specialPrice)) { $storeDate = Mage::app()->getLocale()->storeDate($product->getStore()); $fromDate = Mage::app()->getLocale()->date($product->getSpecialFromDate(), null, null, false); $toDate = Mage::app()->getLocale()->date($product->getSpecialToDate(), null, null, false); if ($product->getSpecialFromDate() && $storeDate->compare($fromDate, Zend_Date::DATES) < 0) { } elseif ($product->getSpecialToDate() && $storeDate->compare($toDate, Zend_Date::DATES) > 0) { } else { $finalPrice = min($finalPrice, $specialPrice); } } return $finalPrice; }
/** * Retrieves the start and end date for the product's special price, if they exist. * * @see self::hasSpecialPrice() - you should check to see if the product is using a special price * * @param Mage_Catalog_Model_Product $product * @return false|Zend_Date[] 'start','end' */ protected function _getSpecialPriceEffectiveDates($product) { $special_from_date = $product->getSpecialFromDate(); $special_to_date = $product->getSpecialToDate(); if (empty($special_from_date) && empty($special_to_date)) { return false; } $cDate = Mage::app()->getLocale()->date(null, null, Mage::app()->getLocale()->getDefaultLocale()); $timezone = Mage::app()->getStore($this->getStoreId())->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE); // From Date if (is_empty_date($special_from_date)) { $special_from_date = $cDate->toString('yyyy-MM-dd HH:mm:ss'); } $fromDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $fromDate->setTimezone($timezone); } $fromDate->setDate(substr($special_from_date, 0, 10), 'yyyy-MM-dd'); $fromDate->setTime(substr($special_from_date, 11, 8), 'HH:mm:ss'); // To Date if (is_empty_date($product->getSpecialToDate())) { $special_to_date = $cDate->toString('yyyy-MM-dd HH:mm:ss'); } $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate(substr($special_to_date, 0, 10), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); if (is_empty_date($product->getSpecialToDate())) { $toDate->add(365, Zend_Date::DAY); } return array('start' => $fromDate, 'end' => $toDate); }
/** * Apply special price for bundle * * @param Mage_Catalog_Model_Product $product * @param decimal $finalPrice * @return decimal */ protected function _applySpecialPrice($product, $finalPrice) { $specialPrice = $product->getSpecialPrice(); if (is_numeric($specialPrice)) { $storeDate = Mage::app()->getLocale()->storeDate($product->getStore()); $fromDate = Mage::app()->getLocale()->date($product->getSpecialFromDate(), null, null, false); $toDate = Mage::app()->getLocale()->date($product->getSpecialToDate(), null, null, false); if ($product->getSpecialFromDate() && $storeDate->compare($fromDate) < 0) { } elseif ($product->getSpecialToDate() && $storeDate->compare($toDate) > 0) { } else { // special price in percents $specialPrice = $finalPrice * $specialPrice / 100; $finalPrice = min($finalPrice, $specialPrice); } } return $finalPrice; }
/** * Checks if the special price has expired * * @param Mage_Catalog_Model_Product $product * @param int | null $store * @return string | null special price or null if the special price has expired */ protected function _getValidSpecialPrice(Mage_Catalog_Model_Product $product, $store = null) { $price = null; $fromDate = $product->getSpecialFromDate(); $toDate = $product->getSpecialToDate(); if (Mage::app()->getLocale()->isStoreDateInInterval($store, $fromDate, $toDate)) { $price = $product->getSpecialPrice(); } return $price; }
public function getProductEntity(Mage_Catalog_Model_Product $product, $storeId, $includeExtras = true) { $result = array(); $result['entity_id'] = $product->getEntityId(); $result['sku'] = $product->getSku(); $result['name'] = $product->getName(); $result['price'] = $product->getPrice(); $result['special_price'] = $product->getSpecialPrice(); $result['special_from_date'] = $product->getSpecialFromDate(); $result['special_to_date'] = $product->getSpecialToDate(); $result['cost'] = $product->getCost(); $result['description'] = $product->getDescription(); $result['short_description'] = $product->getShortDescription(); $result['weight'] = $product->getWeight(); if ($product->isVisibleInSiteVisibility()) { $prodUrlStr = $product->getProductUrl(); $prodUrl = Mage::getSingleton('core/url')->parseUrl($prodUrlStr); $result['url_path'] = substr($prodUrl->getPath(), 1); } $parentProduct = $this->_getParentProduct($product); if ($parentProduct != null) { $result['parent_id'] = $parentProduct->getEntityId(); $result['parent_sku'] = $parentProduct->getSku(); if (!$product->isVisibleInSiteVisibility()) { $result['name'] = $parentProduct->getName(); if ($parentProduct->isVisibleInSiteVisibility()) { $prodUrlStr = $parentProduct->getProductUrl(); $prodUrl = Mage::getSingleton('core/url')->parseUrl($prodUrlStr); $result['url_path'] = substr($prodUrl->getPath(), 1); } } if ($includeExtras && Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE == $parentProduct->getTypeId()) { $result['purchasable'] = $this->_isPurchasable($product, $parentProduct); $attributes = $parentProduct->getTypeInstance(true)->getUsedProductAttributes($parentProduct); foreach ($attributes as $attribute) { if (!array_key_exists('configurable_attributes', $result)) { $result['configurable_attributes'] = array(); } $attr = array(); $attr['attribute_name'] = $attribute->getFrontend()->getLabel(); $attr['value'] = $product->getAttributeText($attribute->getAttributeCode()); $result['configurable_attributes'][] = $attr; } } } if (!isset($result['purchasable'])) { $result['purchasable'] = $this->_isPurchasable($product); } $images = $this->_getProductImages($product); if (isset($images['image'])) { $result['image'] = $images['image']; } if (isset($images['small_image'])) { $result['small_image'] = $images['small_image']; } if (isset($images['thumbnail'])) { $result['thumbnail'] = $images['thumbnail']; } if ($includeExtras) { // Metas $metas = $this->_getMetas($storeId, $product, $parentProduct); if ($metas != null) { //if(isset($metas['meta1'])) $result['meta1'] = $metas['meta1']; //if(isset($metas['meta2'])) $result['meta2'] = $metas['meta2']; if (isset($metas['meta3'])) { $result['meta3'] = $metas['meta3']; } if (isset($metas['meta4'])) { $result['meta4'] = $metas['meta4']; } if (isset($metas['meta5'])) { $result['meta5'] = $metas['meta5']; } } // Brand and Category $brandAndCategoryProduct = !$parentProduct || $product->isVisibleInSiteVisibility() ? $product : $parentProduct; $setSettings = $this->_getProductAttributeSetSettings($brandAndCategoryProduct); if ($setSettings['brandAttribute'] != null) { $result['brand'] = $brandAndCategoryProduct->getAttributeText($setSettings['brandAttribute']); } if ($setSettings['catFromMagento']) { $cats = $this->_getCategoryInformation($storeId, $brandAndCategoryProduct); if (isset($cats['category'])) { $result['category'] = $cats['category']; } if (isset($cats['sub_category'])) { $result['sub_category'] = $cats['sub_category']; } } else { if ($setSettings['catFromAttributes']) { if ($setSettings['categoryAttribute'] != null) { $result['category'] = $brandAndCategoryProduct->getAttributeText($setSettings['categoryAttribute']); } if ($setSettings['subcategoryAttribute'] != null) { $result['sub_category'] = $brandAndCategoryProduct->getAttributeText($setSettings['subcategoryAttribute']); } } } // Inventory $result['in_stock'] = $product->isAvailable() ? "true" : "false"; $stockItem = $product->getStockItem(); if ($stockItem) { $result['qty_on_hand'] = $stockItem->getStockQty(); } // Related Products $result['links'] = $this->_getProductLinks($product); } $result['type'] = $product->getTypeId(); return $result; }
/** * @param Mage_Catalog_Model_Product $product * @return bool */ public function isActualPriceSpecial(Mage_Catalog_Model_Product $product) { return $product->getSpecialPrice() && Mage::app()->getLocale()->isStoreDateInInterval(Mage::app()->getStore(), $product->getSpecialFromDate(), $product->getSpecialToDate()); }