Skip to content

Instantly share code, notes, and snippets.

@jclecas
Last active November 29, 2016 14:43
Show Gist options
  • Save jclecas/26342359c39ea0af19eb323e2479e8e6 to your computer and use it in GitHub Desktop.
Save jclecas/26342359c39ea0af19eb323e2479e8e6 to your computer and use it in GitHub Desktop.
Magento Catalog Helper
// Magento Catalog Helpers
// Amasty, Data, Product
<?php
/**
* Amasty Helper
* @package Jcl_Catalog
*/
class Jcl_Catalog_Helper_Amasty extends Mage_Core_Helper_Abstract
{
protected $_imagePath = null;
/**
* Get Image Url path
*
* @param $filename
* @return string
*/
protected function _getImageUrl($filename)
{
if (null == $this->_imagePath) {
$this->_imagePath = Mage::getUrl('media') . DS . 'amshopby' . DS;
}
return $this->_imagePath . $filename;
}
/**
* Return the url path to the product image
* Attention ! Return false if no image associated to the product jcl_color attribute
*
* @param Mage_Catalog_Model_Product $product
* @return false|string
*/
public function getProductColorImage(Mage_Catalog_Model_Product $product)
{
$amShopByItem = Mage::getModel('amshopby/value')->getCollection()
->addFieldToFilter('option_id', $product->getData('jcl_color'))
->setPageSize(1)
->getFirstItem();
$img = false;
if ($amShopByItem && $amShopByItem->getData('img_medium')) {
$img = $this->_getImageUrl($amShopByItem->getData('img_medium'));
}
return $img;
}
}
<?php
/**
* Data Helper
* @package Jcl_Catalog
*/
class Jcl_Catalog_Helper_Data extends Mage_Core_Helper_Abstract
{
protected $_attributes = array();
/**
* Load Attribute Model
*
* @param int|string $attributeCode
* @param string $attributeEntity
* @return mixed
* @throws Mage_Core_Exception
*/
protected function _getAttributeModel($attributeCode, $attributeEntity = Mage_Catalog_Model_Product::ENTITY)
{
if (!array_key_exists($attributeCode, $this->_attributes)) {
if (is_string($attributeCode)) {
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode($attributeEntity, $attributeCode);
} else {
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->load($attributeCode);
}
$this->_attributes[$attributeCode] = $attribute;
}
return $this->_attributes[$attributeCode];
}
/**
* Return the option label for the given option id
*
* @param $attributeCode
* @param $optionId
* @param string $attributeEntity
* @return bool|string
* @throws Mage_Core_Exception
*/
public function getAttributeOptionLabelById($attributeCode, $optionId, $attributeEntity = Mage_Catalog_Model_Product::ENTITY)
{
try {
return $this->_getAttributeModel($attributeCode, $attributeEntity)
->getSource()
->getOptionText($optionId);
} catch (Exception $e) {
Mage::logException($e);
if (Mage::getIsDeveloperMode()) {
Mage::throwException($e);
}
}
return false;
}
<?php
/**
* Product Helper
* @package Jcl_Catalog
*/
class Jcl_Catalog_Helper_Product extends Mage_Core_Helper_Abstract
{
const BLOCK_TYPE = 'jcl_catalog/product';
const TEMPLATE = 'catalog/product/list/item.phtml';
protected $_products = array();
/**
* Return a full loaded product
*
* @param Mage_Catalog_Model_Product $fakeProduct
* @return mixed
*/
protected function _getProduct(Mage_Catalog_Model_Product $fakeProduct)
{
if (!array_key_exists($fakeProduct->getId(), $this->_products)) {
$this->_products[$fakeProduct->getId()] = Mage::getModel('catalog/product')->load($fakeProduct->getId());
}
return $this->_products[$fakeProduct->getId()];
}
/**
* Returns stock class
*
* @param $product
* @return string
*/
protected function _getStockClass($product)
{
$qty = $product->getStockItem()->getQty();
if ($qty > 0) {
if ($qty > Mage::getStoreConfig(Mage_CatalogInventory_Block_Stockqty_Abstract::XML_PATH_STOCK_THRESHOLD_QTY)) {
$class = 'in-stock';
} else {
$class = 'limit-stock';
}
} else {
$class = 'out-of-stock';
}
return $class;
}
/**
* Get product item HTML
*
* @param Mage_Catalog_Model_Product $product
* @param null|string $template
*
* @return mixed
*/
public function getProductItemHtml(Mage_Catalog_Model_Product $product, $template = null)
{
return $this->getLayout()
->createBlock(self::BLOCK_TYPE)
->setTemplate($template ? $template : self::TEMPLATE)
->setProduct($product)
->toHtml();
}
/**
* Return a product fully loaded from the quote item
*
* @param Mage_Sales_Model_Quote_Item $productItem
* @return false|Mage_Catalog_Model_Product
*/
public function getProductFromQuoteItem(Mage_Sales_Model_Quote_Item $productItem)
{
$fakeProduct = $productItem->getProduct();
return $this->_getProduct($fakeProduct);
}
/**
* Get product item selected options
*
* @param Mage_Sales_Model_Quote_Item $productItem
* @return array
* @throws Mage_Core_Exception
*/
public function getQuoteItemOptions(Mage_Sales_Model_Quote_Item $productItem)
{
/**
* This is an other method to do it, but the result will appear like this:
* array('attributeLabel' => 'optionLabel', ...)
*
* $helper = Mage::helper('catalog/product_configuration');
* $options = $helper->getOptions($productItem);
*/
try {
$options = $productItem->getOptions();
$options = current($options);
$options = unserialize($options->getValue());
$options = $options['super_attribute'];
$selectedOptions = array();
foreach ($options as $attributeId => $optionId) {
$attribute = $this->_getAttributeModel($attributeId);
$optionLabel = $this->getAttributeOptionLabelById($attribute->getAttributeCode(), $optionId);
$selectedOptions[] = array('code' => $attribute->getAttributeCode(), 'value' => $optionLabel);
}
return $selectedOptions;
} catch (Exception $e) {
Mage::logException($e);
if (Mage::getIsDeveloperMode()) {
Mage::throwException($e);
}
}
return array();
}
/**
* Get Product size label
*
* @param Mage_Sales_Model_Quote_Item $productItem
* @return bool|string
*/
public function getProductSizeFromQuoteItem(Mage_Sales_Model_Quote_Item $productItem)
{
$options = $this->getQuoteItemOptions($productItem);
$value = false;
foreach ($options as $option) {
if ('ltdc_size' == $option['code']) {
$value = $option['value'];
break;
}
}
return $value;
}
/**
* Retrieve available product sizes
* (configurable only)
*
* @param Mage_Catalog_Model_Product $product
*
* @return array
*/
public function getAvailableProductSizes(Mage_Catalog_Model_Product $product)
{
$availableSizes = array();
$properOrderedArray = array();
$sortOrder = 1;
if ($product->isConfigurable()) {
$optionsAtt = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'jcl_size')->getFrontend()->getSelectOptions();
$optionsByValue = array();
foreach ($optionsAtt as $option) {
$optionsByValue[$option['value']] = $option['label'];
}
foreach ($product->getTypeInstance(true)->getUsedProducts(null, $product) as $usedProduct) {
$usedProduct = Mage::getModel('catalog/product')->load($usedProduct->getId());
if(!$usedProduct->isDisabled()) {
if ($size = $usedProduct->getAttributeText('jcl_size')) {
$productAttribute = $usedProduct->getResource()->getAttribute('jcl_size');
if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) {
continue;
}
$id = $usedProduct->getData('jcl_size');
$availableSizes[$id] = array(
'label' => $size,
'order' => $sortOrder++,
'stock_class' => $this->_getStockClass($usedProduct),
);
}
}
}
if(count($availableSizes)) {
$properOrderedArray = array_intersect_key($optionsByValue, $availableSizes);
$properOrderedArray = array_replace($properOrderedArray, $availableSizes);
} else {
$properOrderedArray = $availableSizes;
}
}
return $properOrderedArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment