private function _getAttributeValue(Mage_Catalog_Model_Product $product, $attributeCode) { $attribute = $product->getResource()->getAttribute($attributeCode); if ($attribute) { return $attribute->getFrontend()->getValue($product); } }
/** * Set current attribute to entry (for specified product) * * @param Mage_Catalog_Model_Product $product * @param Google_Service_ShoppingContent_Product $shoppingProduct * @return Google_Service_ShoppingContent_Product */ public function convertAttribute($product, $shoppingProduct) { // get category from product attribute Mage::log(__LINE__ . $product->getResource()->getAttribute('google_shopping_category')); $value = $product->getResource()->getAttribute('google_shopping_category')->getFrontend()->getValue($product); $value = preg_replace('/\\d+ /', '', $value); $shoppingProduct->setGoogleProductCategory($value); return $shoppingProduct; }
/** * Get product tier price by qty * * @param double $qty * @param Mage_Catalog_Model_Product $product * @return double */ public function getTierPrice($qty = null, $product) { $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL; #$defaultGroup = Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID); $prices = $product->getData('tier_price'); /** * Load tier price */ if (is_null($prices)) { if ($attribute = $product->getResource()->getAttribute('tier_price')) { $attribute->getBackend()->afterLoad($product); $prices = $product->getData('tier_price'); } } if (is_null($prices) || !is_array($prices)) { if (!is_null($qty)) { return $product->getPrice(); } return array(array('price' => $product->getPrice(), 'price_qty' => 1, 'cust_group' => $allGroups)); } $custGroup = $this->_getCustomerGroupId($product); if ($qty) { // starting with quantity 1 and original price $prevQty = 1; $prevPrice = $product->getPrice(); $prevGroup = $allGroups; foreach ($prices as $price) { if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) { // tier not for current customer group nor is for all groups continue; } if ($qty < $price['price_qty']) { // tier is higher than product qty continue; } if ($price['price_qty'] < $prevQty) { // higher tier qty already found continue; } if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) { // found tier qty is same as current tier qty but current tier group is ALL_GROUPS continue; } $prevPrice = $price['price']; $prevQty = $price['price_qty']; $prevGroup = $price['cust_group']; } return $prevPrice; } else { foreach ($prices as $i => $price) { if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) { unset($prices[$i]); } } } return $prices ? $prices : array(); }
/** * Check unique url_key value in catalog_product_entity_url_key table. * * @param Mage_Catalog_Model_Product $object * @return Mage_Catalog_Model_Product_Attribute_Backend_Urlkey * @throws Mage_Core_Exception */ protected function _validateUrlKey($object) { $connection = $object->getResource()->getReadConnection(); $select = $connection->select()->from($this->getAttribute()->getBackendTable(), array('count' => new Zend_Db_Expr('COUNT(\'value_id\')')))->where($connection->quoteInto('entity_id <> ?', $object->getId()))->where($connection->quoteInto('value = ?', $object->getUrlKey())); $result = $connection->fetchOne($select); if ((int) $result) { throw new Mage_Core_Exception(Mage::helper('catalog')->__("Product with the '%s' url_key attribute already exists.", $object->getUrlKey())); } return $this; }
/** * Load and set gift card amounts into product object * * @param Mage_Catalog_Model_Product $product */ public function getAmounts($product) { $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL; $prices = $product->getData('giftcard_amounts'); if (is_null($prices)) { if ($attribute = $product->getResource()->getAttribute('giftcard_amounts')) { $attribute->getBackend()->afterLoad($product); $prices = $product->getData('giftcard_amounts'); } } return $prices ? $prices : array(); }
/** * Get custom configurable product * * @return Mage_Catalog_Model_Product */ public function getProduct() { if (!$this->_product) { $this->_product = Mage::getModel('catalog/product')->setStoreId($this->getStoreId()); $id = $this->_product->getResource()->getIdBySku($this->_sku); $this->_product->load($id); if (!$this->_product->getId()) { Mage::log('CustomConfigurable configurable product could not be loaded', null, 'aydus_customconfigurable.log'); } } return $this->_product; }
/** * Encodes a single Magento Product. * * @param Mage_Catalog_Model_Product $product * * @return RTDProduct */ protected function _encodeProduct(Mage_Catalog_Model_Product $product) { $encodedProduct = new RTDProduct(); $customMapping = Mage::getConfig()->getNode('rtd_mappings/product/export'); $attributeTypes = array('multiselect', 'dropdown'); foreach ($customMapping->asArray() as $magentoKey => $rtdKey) { try { $attribute = $product->getResource()->getAttribute($magentoKey); if ($attribute != false && in_array($attribute->getFrontendInput(), $attributeTypes)) { $encodedProduct->setParam($rtdKey, $product->getAttributeText($magentoKey)); } else { $encodedProduct->setParam($rtdKey, $product->{'get' . $magentoKey}()); } } catch (Exception $ex) { Mage::log($ex->getMessage(), null, 'realtimedespatch.log'); } } if (Mage::getStoreConfigFlag('sixbysix_realtimedespatch/product_export/image_export')) { $this->_swapPlaceholderWithParentImage($product, $encodedProduct); } return $encodedProduct; }
/** * @param Mage_Catalog_Model_Product $parent */ public function setAttributes($parent) { $result = array(); if ($this->getIsChild() && $parent->isConfigurable()) { /** @var Mage_Catalog_Model_Product_Type_Configurable $productTypeInstance */ $productTypeInstance = $parent->getTypeInstance(true); $allowAttributes = $productTypeInstance->getConfigurableAttributes($parent); foreach ($allowAttributes as $attribute) { /** @var Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute */ $itemAttribute = new Shopgate_Model_Catalog_Attribute(); $attribute = $attribute->getProductAttribute(); if ($attribute == null) { continue; } $itemAttribute->setGroupUid($attribute->getAttributeId()); $attrValue = $this->item->getResource()->getAttribute($attribute->getAttributeCode())->getFrontend(); $itemAttribute->setLabel($attrValue->getValue($this->item)); $result[] = $itemAttribute; } } parent::setAttributes($result); }
/** * getAttributesPerProduct * * @param Mage_Catalog_Model_Product $product * @return array */ public function getAttributesPerProduct($product) { $attributeCodes = Mage::helper('merchandiser')->getAttributeCodes(); $currency = $this->getCurrency(); if (is_null($attributeCodes)) { return array(); } $attributes = array(); foreach ($attributeCodes as $attributeCode) { if (strcmp($attributeCode, '') == 0) { continue; } $eavAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', $attributeCode); if ($eavAttribute->getId() == null) { continue; } $resource = $product->getResource()->getAttribute($attributeCode); $label = $resource->getFrontendLabel(); $value = $resource->getFrontend()->getValue($product); if ($value) { if ($eavAttribute->getFrontendInput() == 'price') { $value = $currency->formatPrecision($value, 2); } $attributes[] = array('label' => $label, 'value' => $value); } } return $attributes; }
/** * get gift wrapping label * * @param Mage_Catalog_Model_Product $product * * @return null|string */ protected function _getGiftWrappingLabel(Mage_Catalog_Model_Product $product) { // set this label only once if (null === $this->_giftWrappingLabel) { $this->_giftWrappingLabel = $this->_getHelper()->__('Gift Wrapping'); if (empty($this->_giftWrappingLabel)) { $this->_giftWrappingLabel = $product->getResource()->getAttribute('gift_wrapping_available')->getStoreLabel(); } } return $this->_giftWrappingLabel; }
/** * Get proper ref value for given product * * @param Mage_Catalog_Model_Product $product * @param int $refNumber * @param int $storeId * @return null|string */ protected function _getRefValueByProductAndNumber($product, $refNumber, $storeId) { $value = null; $helperMethod = 'getRef' . $refNumber . 'AttributeCode'; $refCode = Mage::helper('avatax')->{$helperMethod}($storeId); if ($refCode && $product->getResource()->getAttribute($refCode)) { try { $value = (string) $product->getResource()->getAttribute($refCode)->getFrontend()->getValue($product); } catch (Exception $e) { Mage::logException($e); } } return $value; }
/** * Load a single attribute value onto a product. * * This method is not nice. I only keep it because if it reduces * the number of support requests, when people specify attributes * in the template string but don't have them loaded on product * collections. * * @param Mage_Catalog_Model_Product $product * @param string $attributeCode * @return $this */ protected function _loadAttributeOnProduct(Mage_Catalog_Model_Product $product, $attributeCode) { $value = $product->getResource()->getAttributeRawValue($product->getId(), $attributeCode, Mage::app()->getStore()->getId()); $product->setData($attributeCode, $value); return $this; }
/** * Remove don't applicable attributes data * * @param Mage_Catalog_Model_Product $product */ protected function _removeNotApplicableAttributes($product) { $eavConfig = Mage::getSingleton('Mage_Eav_Model_Config'); $entityType = $product->getResource()->getEntityType(); foreach ($eavConfig->getEntityAttributeCodes($entityType, $product) as $attributeCode) { $attribute = $eavConfig->getAttribute($entityType, $attributeCode); $applyTo = $attribute->getApplyTo(); if (is_array($applyTo) && count($applyTo) > 0 && !in_array($product->getTypeId(), $applyTo)) { $product->unsetData($attribute->getAttributeCode()); } } }
/** * Get product attribute value * * @param Mage_Catalog_Model_Product $product * @param string $code * @return string */ protected function _getProductAttributeValue($product, $code) { $value = ''; if ($code && $product->getResource()->getAttribute($code)) { try { $value = (string) $product->getResource()->getAttribute($code)->getFrontend()->getValue($product); } catch (Exception $e) { Mage::logException($e); } } return $value; }
/** * @param Mage_Catalog_Model_Product $product * @param array $additionalAttributes * @return array */ public function getProductData(Mage_Catalog_Model_Product $product, $additionalAttributes = array()) { $product_data = array(); $attributes = array_merge((array) $this->config->attributes, $additionalAttributes); // store $store = Mage::getModel('core/store')->load($product->getStoreId()); foreach ($attributes as $attribute) { if ($attribute->type == 'constant') { $value = $attribute->value; } elseif ($attribute->type == 'product_attribute') { // if this is a normal product attribute, retrieve it's frontend // representation, or the default value if it doesn't have a value // for this attribute if ($product->getData($attribute->magento) === null) { if (isset($attribute->default)) { $value = $attribute->default; } else { $value = ''; } } else { /** @var $attributeObj Mage_Catalog_Model_Resource_Eav_Attribute */ $attributeObj = $product->getResource()->getAttribute($attribute->magento); $value = $attributeObj->getFrontend()->getValue($product); // The output helper performs this check as well, but we don't want to unnecessarily send off // every single attribute to this helper if we can avoid it. if ($attributeObj->getIsWysiwygEnabled()) { $value = $this->catalogOutputHelper->productAttribute($product, $value, $attribute->magento); } } } elseif ($attribute->type == 'stock_attribute') { $value = $product->getStockItem()->getData($attribute->magento); if ($value === null) { if (isset($attribute->default)) { $value = $attribute->default; } else { $value = ''; } } } elseif ($attribute->type == 'computed') { // if this is a computed attribute, handle it depending on its code switch ($attribute->magento) { case 'final_price': $price = Mage::helper('tax')->getPrice($product, $product->getFinalPrice(), true); $value = sprintf('%.2f', (double) $store->convertPrice($price, false, false)); $value .= ' ' . Mage::getStoreConfig('currency/options/default', $product->getStoreId()); break; case 'special_price': $price = Mage::helper('tax')->getPrice($product, $product->getSpecialPrice(), true); $value = sprintf('%.2f', (double) $store->convertPrice($price, false, false)); $value .= ' ' . Mage::getStoreConfig('currency/options/default', $product->getStoreId()); break; case 'price': $price = Mage::helper('tax')->getPrice($product, $product->getFinalPrice(), true); $value = sprintf('%.2f', (double) $store->convertPrice($price, false, false)); $value .= ' ' . Mage::getStoreConfig('currency/options/default', $product->getStoreId()); break; case 'product_link': $value = $this->getCleanProductUrl($product); break; case 'image_url': $value = (string) Mage::helper('catalog/image')->init($product, 'image'); break; case 'instock_y_n': $value = $product->isSaleable() ? "Y" : "N"; // myshopping break; case 'availability_yes_no': $value = $product->isSaleable() ? "yes" : "no"; break; case 'availability_google': $value = $product->isSaleable() ? 'in stock' : 'out of stock'; break; case 'currency': $value = Mage::getStoreConfig('mehulchaudhari_feedsgenerator/' . $this->config->config_path . '/currency'); break; case 'google_taxonomy': if (!isset($attribute->exclude_cats)) { $attribute->exclude_cats = array(); } $categoryIds = $this->getCategoryPathIds($product, $attribute->exclude_cats); if (count($categoryIds) > 0) { $categoryId = $categoryIds[count($categoryIds) - 1]; $category = Mage::getModel('catalog/category')->load($categoryId); $value = $category->getResource()->getAttribute('google_product_category')->getFrontend()->getValue($category); } else { $value = ''; } break; case 'category_path': case 'category_last': if (!isset($attribute->exclude_cats)) { $attribute->exclude_cats = array(); } $value = $this->getCategoryPath($product, $attribute->exclude_cats); if ($attribute->magento == 'category_last') { if (count($value) > 0) { $value = $value[count($value) - 1]; } else { $value = utf8_encode(Mage::getStoreConfig('mehulchaudhari_feedsgenerator/' . $this->config->config_path . '/defaultcategory')); } } break; case 'child_skus': $value = array(); if ($product->getTypeId() == 'configurable') { foreach ($product->getTypeInstance()->getUsedProducts() as $child) { $value[] = $child->getSku(); } } break; default: $this->log("Unknown computed attribute code: {$attribute->magento}"); $value = 'UNKNOWN_COMPUTED_ATTRIBUTE_CODE'; } } else { $this->log("Unknown attribute type: {$attribute->type}"); $value = 'UNKNOWN_ATTRIBUTE_TYPE'; } // Clean up the value, or each of its elements if it's an array if (is_array($value)) { foreach ($value as &$element) { $element = $this->cleanValue($attribute, $element); } unset($element); } else { $value = $this->cleanValue($attribute, $value); } $product_data[$attribute->feed] = $value; } return $product_data; }
private function getAttributeValue(Mage_Catalog_Model_Product $product, $attributeName) { $attribute = $product->getResource()->getAttribute($attributeName); if ($attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) { $attribute_data = $product->getData($attributeName); $attribute_value = $product->getAttributeText($attributeName); if (empty($attribute_value) && is_scalar($attribute_data)) { $attribute_value = $attribute_data; } } else { $attribute_value = null; } return trim($attribute_value); }
/** * set attributes for the product * * @param Mage_Catalog_Model_Product $product * * @return bool */ protected function _setAttributes(Mage_Catalog_Model_Product $product) { $tmpProduct = null; if ($this->_parentProduct && $this->_parentProduct->isConfigurable()) { $tmpProduct = $this->_parentProduct; } else { if ($product->isConfigurable()) { $tmpProduct = $product; } else { return false; } } // Create a stack with all attribute-codes // fetching it on every item it get very high memory usage on some systems if (!isset($this->_stack[$tmpProduct->getId()])) { $productTypeInstance = $tmpProduct->getTypeInstance(true); if ($productTypeInstance == null || !method_exists($productTypeInstance, "getUsedProducts")) { return false; } $allowAttributes = $productTypeInstance->getConfigurableAttributes($tmpProduct); $this->_stack[$tmpProduct->getId()] = $allowAttributes; } $i = 0; if ($this->_parentProduct) { foreach ($this->_stack[$this->_parentProduct->getId()] as $attribute) { $i++; $attribute = $attribute->getProductAttribute(); if ($attribute == null) { continue; } $attribute = $attribute->getAttributeCode(); $attrValue = $product->getResource()->getAttribute($attribute)->getFrontend(); $this->_defaultRow["attribute_{$i}"] = $attrValue->getValue($product); } } else { if ($product->isConfigurable()) { foreach ($this->_stack[$product->getId()] as $attribute) { $i++; $this->_defaultRow["attribute_{$i}"] = $attribute->getLabel(); } } } }
/** * Return assigned images for specific stores * * @param Mage_Catalog_Model_Product $product * @param int|array $storeIds * @return array * */ public function getAssignedImages($product, $storeIds) { if (!is_array($storeIds)) { $storeIds = array($storeIds); } $mainTable = $product->getResource()->getAttribute('image')->getBackend()->getTable(); $read = $this->_getReadAdapter(); $select = $read->select()->from(array('images' => $mainTable), array('value as filepath', 'store_id'))->joinLeft(array('attr' => $this->getTable('eav/attribute')), 'images.attribute_id = attr.attribute_id', array('attribute_code'))->where('entity_id = ?', $product->getId())->where('store_id IN (?)', $storeIds)->where('attribute_code IN (?)', array('small_image', 'thumbnail', 'image')); $images = $read->fetchAll($select); return $images; }
/** * @param Mage_Catalog_Model_Product $product * * @return string */ public function getManufacturer($product) { $manufacturer = $product->getManufacturer(); if ($manufacturer) { $manufacturer = $product->getResource()->getAttribute('manufacturer')->getSource()->getOptionText($manufacturer); } return $manufacturer; }
/** * Get product tier price by qty * * @param float $qty * @param Mage_Catalog_Model_Product $product * @return float */ public function getTierPrice($qty = null, $product) { $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL; $prices = $product->getData('tier_price'); if (is_null($prices)) { $attribute = $product->getResource()->getAttribute('tier_price'); if ($attribute) { $attribute->getBackend()->afterLoad($product); $prices = $product->getData('tier_price'); } } if (is_null($prices) || !is_array($prices)) { if (!is_null($qty)) { return $product->getPrice(); } return array(array('price' => $product->getPrice(), 'website_price' => $product->getPrice(), 'price_qty' => 1, 'cust_group' => $allGroups)); } $custGroup = $this->_getCustomerGroupId($product); if ($qty) { $prevQty = 1; $prevPrice = $product->getPrice(); $prevGroup = $allGroups; foreach ($prices as $price) { if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) { // tier not for current customer group nor is for all groups continue; } if ($qty < $price['price_qty']) { // tier is higher than product qty continue; } if ($price['price_qty'] < $prevQty) { // higher tier qty already found continue; } if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) { // found tier qty is same as current tier qty but current tier group is ALL_GROUPS continue; } if ($price['website_price'] < $prevPrice) { $prevPrice = $price['website_price']; $prevQty = $price['price_qty']; $prevGroup = $price['cust_group']; } } return $prevPrice; } else { $qtyCache = array(); foreach ($prices as $i => $price) { if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) { unset($prices[$i]); } else { if (isset($qtyCache[$price['price_qty']])) { $j = $qtyCache[$price['price_qty']]; if ($prices[$j]['website_price'] > $price['website_price']) { unset($prices[$j]); $qtyCache[$price['price_qty']] = $i; } else { unset($prices[$i]); } } else { $qtyCache[$price['price_qty']] = $i; } } } } return $prices ? $prices : array(); }
/** * Check attribute source * * @param Mage_Catalog_Model_Product $product * @param string $value * @return bool */ protected function _checkAttributeSource($product, $value) { if (!array_key_exists($value, $this->_attributeSources)) { $this->_attributeSources[$value] = $product->getResource()->getAttribute($value)->usesSource(); } return $this->_attributeSources[$value]; }