Pages

Showing posts with label Magento. Show all posts
Showing posts with label Magento. Show all posts

Thursday, January 1, 2015

How to adding custom product attributes in Magento using setup script

Have you tried to add custom product attributes using the setup script and wondering why certain properties of the attribute are not set?
And in top of that, you're are running out time for this simple task, just because you don't find the right answer..
Here is an approach, how to solve this problem. Probably not the perfect solution, but it works ..!
To get started, you need to handle 3 parts in your module:

/yournamespace/modulename/sql/modulename_setup/install-0.1.0.php
/yournamespace/modulename/Resource/Eav/Mysql4/setup.php
/yournamespace/modulename/etc/config.xml

So let's start with the 1st part:
(Hint: if you update your module, name the script 'update-x.x.x.php')


<?php
/**
 * Add my_attribute values 
 * example by http://magento4newbies.blogspot.com.
 *
 */

$installer = $this;
$installer->startSetup();

$installer->addAttribute('catalog_product', 'my_attribute_date', array(
    'group'             => 'My Attribute Set',
    'label'             => 'My Attribute Date',
    'type'              => 'datetime',
    'input'             => 'date',
    'backend'           => 'eav/entity_attribute_backend_datetime',
    'frontend'          => '',
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => true,
    'visible_in_advanced_search' => false,
    'unique'            => false,
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));


$installer->endSetup();

You can modify 'input' and 'type' according to your needs:

//For textfield
'input' => 'text',
'type' => 'text',

//For textarea
'input' => 'textarea',
'type' => 'text',

//For date field
'input' => 'date',
'type' => 'datetime',

//For select list 
'input' => 'select',
'type' => 'text',

//For yes/no
'input' => 'boolean',
'type' => 'int',

2nd part is to define your resource class, so that your attribute's properties are written as expected:

<?php
/**
 * Yournamespace_Modulename_Model_Resource_Eav_Mysql4_Setup
 * example by http://magento4newbies.blogspot.com.
 *
 */

class Yournamespace_Modulename_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
 
     /**
     * Prepare catalog attribute values to save
     * From: Mage_Catalog_Model_Resource_Setup
     *
     * @param array $attr
     * @return array
     */
    protected function _prepareValues($attr)
    {
        $data = parent::_prepareValues($attr);
        $data = array_merge($data, array(

            'apply_to'                      => $this->_getValue($attr, 'apply_to'),
            'frontend_input_renderer'       => $this->_getValue($attr, 'input_renderer'),
            'is_comparable'                 => $this->_getValue($attr, 'comparable', 0),
            'is_configurable'               => $this->_getValue($attr, 'is_configurable', 1),
            'is_filterable'                 => $this->_getValue($attr, 'filterable', 0),
            'is_filterable_in_search'       => $this->_getValue($attr, 'filterable_in_search', 0),
            'is_global'                     => $this->_getValue(
                $attr,
                'global',
                Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE
            ),
            'is_html_allowed_on_front'      => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
            'is_searchable'                 => $this->_getValue($attr, 'searchable', 0),
            'is_used_for_promo_rules'       => $this->_getValue($attr, 'used_for_promo_rules', 0)
            'is_visible'                    => $this->_getValue($attr, 'visible', 1),
            'is_visible_on_front'           => $this->_getValue($attr, 'visible_on_front', 1),
            'is_wysiwyg_enabled'            => $this->_getValue($attr, 'wysiwyg_enabled', 0),
            'is_visible_in_advanced_search' => $this->_getValue($attr, 'visible_in_advanced_search', 0),
            'position'                      => $this->_getValue($attr, 'position', 0),
            'used_for_sort_by'              => $this->_getValue($attr, 'used_for_sort_by', 0),
            'used_in_product_listing'       => $this->_getValue($attr, 'used_in_product_listing', 0),
        ));
        return $data;
    }
}

And for the last part, you need to set the resource class and the right version number in config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <yournamespace_modulename>
            <version>x.x.x</version> 
        </yournamespace_modulename>
    </modules>
    <global>
        <resources>
            <yournamespace_modulename_setup>
                <setup>
                    <module>Yournamespace_Modulename</module>
                    <class>Yournamespace_Modulename_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
            </yournamespace_modulename_setup>
        </resources>
    </global>
</config>

Now when you check your attributes within the Magento Admin GUI (Catalog->Attributes->Manage Attributes), you see is_filterable, is_visible, is_visible_on_front, is_html_allowed_on_front and the other parameters set to the same value as in the setup script.

Wednesday, November 5, 2014

How to get Magento's attribute names and values

For my own reference, quick and dirty:
$product = Mage::getModel('catalog/product')->load($productId);
$attribute_value = $product->getData($attribute_code);

The attribute code used below is 'attribute_code'.

/**
 * get attribute collection
 */
$attribute = $_product->getResource()->getAttribute('attribute_code');

/**
 * get attribute type
 */
$attribute->getAttributeType();

/**
 * get attribute label
 */
$attribute->getFrontendLabel();

/**
 * get attribute default value
 */
$attribute->getDefaultValue();

/**
 * check if the attribute is visible
 */
$attribute->getIsVisible();

/**
 * check if the attribute is required
 */
$attribute->getIsRequired();

/**
 * get attribute value
 */
$attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getAttributeCode();

//or without loading the product
$attributeValue = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);

/**
* get attribute option code/value list 
*/
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'attribute_code');
$options = $attribute->getSource()->getAllOptions(false);


in a view.php or similar, you can access the attribute text related to a product like:
<?php echo $_product->getAttributeText('attribute_code'); ?>

Sunday, September 14, 2014

How to get Magento Urls

(for my own reference)

Base Url , Skin Url , Media Url , Js Url , Store Url and Current Url Get Url in phtml files

  • Base Url : Mage::getBaseUrl(); 
  • Skin Url : Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN); 
  • Unsecure Skin Url : $this->getSkinUrl('images/image_name.jpg'); 
  • Secure Skin Url : $this->getSkinUrl('images/image_name.gif', array('_secure'=>true)); 
  • Media Url : Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); 
  • Js Url : Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS); 
  • Store Url : Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); 
  • Current Url Mage::helper('core/url')->getCurrentUrl(); 

Urls in cms pages or static blocks

  • Base Url : {{store url=""}} 
  • Skin Url : {{skin url='images/image_name.jpg'}} 
  • Media Url : {{media url='/image_name.jpg'}} 
  • Store Url : {{store url='somepage.html'}}

Saturday, July 12, 2014

How to download extensions from Magento Connect manually

Usually you need to install at some point extensions for your Magento shop.
Best practice is to install it manually. That's to keep control and check, whether the extension is ok without breaking your shop installation. There are two ways to do it.

Either you go to freegento.com/ddl-magento-extension.php and enter the extension key from Magento Connect. As result, you get a download link for your module.


Or you use mage, a shell provided by Magento.
Got to the root of your test store

orion:~ user$ cd /Users/user/dev/magento/latest


If you use it the first time, you need to adapt the permissions

orion:~ user$ chmod 550 mage
orion:~ user$ ./mage

Connect commands available:
===========================
channel-add          Add a Channel       
channel-alias        Specify an alias to a channel name
channel-delete       Remove a Channel From the List
channel-info         Retrieve Information on a Channel
channel-login        Connects and authenticates to remote channel server
channel-logout       Logs out from the remote channel server
clear-cache          Clear Web Services Cache
config-get           Show One Setting    
config-help          Show Information About Setting
config-set           Change Setting      
config-show          Show All Settings   
convert              Convert old magento PEAR package to new format
download             Download Package    
info                 Display information about a package
install              Install Package     
install-file         Install Package Archive File
list-available       List Available Packages
list-channels        List Available Channels
list-files           List Files In Installed Package
list-installed       List Installed Packages In The Default Channel
list-upgrades        List Available Upgrades
package              Build Package       
package-dependencies Show package dependencies
package-prepare      Show installation information of package
sync                 Synchronize Manually Installed Packages
sync-pear            Synchronize already Installed Packages by pear
uninstall            Un-install Package  
upgrade              Upgrade Package     
upgrade-all          Upgrade All Packages
If you get the command list above, everything works as it should.
Now you can download your extension, like

orion:~ user$ ./mage install http://connect20.magentocommerce.com/community extension_name

Thursday, July 10, 2014

How to get a list of manufacturers ids

To get an array of the manufacturer names & ids use the following code:


$product = Mage::getModel('catalog/product');
$attributes =Mage::getResourceModel('eav/entity_attribute_collection')
  ->setEntityTypeFilter($product->getResource()->getTypeId())
  ->addFieldToFilter('attribute_code', 'manufacturer');
$attribute = $attributes->getFirstItem()
  ->setEntity($product->getResource());
$manufacturers = $attribute->getSource()->getAllOptions(false);


you can test code snippet like above with a php shell as provided in Magento PHP Developer's Guide (p.58)


the result looks similar to this:

magento > print_r($manufacturers);
Array
(
  [0] => Array
  (
   [value] => 6
   [label] => Manfucturer A
  )
  [1] => Array
  (
   [value] => 20
   [label] => Manfucturer B
  )
  [2] => Array
  (
   [value] => 21
   [label] => Manfucturer C
  )
  [3] => Array
  (
   [value] => 27
   [label] => Manfucturer D
  )
)
magento >


Thursday, July 3, 2014

How to disable Paypal logo in Magento 1.9.x

had some difficulty to find valid information on how to disable Paypal logo in Magento 1.9.x.
For older versions, there was a setting in Configuration/Payment method/paypal something and enable/disable logo in frontend.

The proper way is to disable it in your local.xml file located
[your_magento_installation]app/design/frontend/[your_template]/[your_package]/local.xml

add the remove tag within
<layout>
             <default>
                  [...]
                  <remove name="paypal.partner.right.logo"/>
                  [...]
             </default>
</layout>

if it doesn't work, there is always a lumberjack method... overwrite the default behaviour from the default template.

So copy from [your_magento_installation]app/design/frontend/base/default/template/paypal/partner/logo.phtml
to  [your_magento_installation]app/design/frontend/[your_template]/default/template/paypal/partner/logo.phtml

and remove the php code.



Dummy style but effective...

if you know the right way to do it over the admin panel, please let me know!
thx