Magento 1 Get Visible Products

This is how you get products that are in stock and at the same time are visible on frontend.

$collection = Mage::getModel( 'catalog/product' )->getCollection()
                  ->addAttributeToSelect( 'stock_location', 'is_in_stock' ) // Only products that are in stock
                  ->addFieldToFilter( 'visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH );

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1 Get Configurable Products
$productsCollection = Mage::getResourceModel( 'catalog/product_collection' )
                ->addAttributeToFilter( 'type_id', array( 'eq' => 'configurable' ) );

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1 Subscribe customer programatically

This can be done with one line of code:

$subscriber = Mage::getModel('newsletter/subscriber')->subscribe('customer@magentoguides.com');

Please note that this will send a confirmation email to the customer.

You can then find the new email in Backend > Newsletter > Newsletter Subscribers.

You can get subscribers collection with this snippet.

$collection = Mage::getModel('newsletter/subscriber')->getCollection()
                  ->setPageSize(10)
                  ->setOrder('subscriber_id', 'desc');

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1.9 – Helper class not found in…

When developing Magento modules this is one of the common errors you will encounter.
The error is caused by these 3 most common mistakes:

1. When creating a new module you always have to create a helper file, even though you are not using it. This is just how Magento works. If you want to know how to create a helper file in module, go to this blog post.

2. The second most common mistake is the wrong structure in the XML config file.

Magento strictly follows a structure in XML files, having something wrong or having a typo somewhere always leads to an error. The correct structure for defining new helper in etc/config.xml is as follows:

<global>
    <helpers>
        <unique_identifier> 
            <class>Package_ModuleName_Helper</class>
        </guides_meta>
    </helpers>
</global>

Make sure that your helper definition follows the above structure.

3. If you are sure that everything from above is correct but you still get the same error then there is one more thing. Turn of the code compilation.
You can do it from Administration by going to System > Tools > Compilation > Disable compilation.

Or if for some reason don’t have access to Admin are, you can execute these shell commands.

php -f shell/compiler.php -- disable
php -f shell/compiler.php -- clear
php -f shell/compiler.php -- state

 

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1.9 Delete All Attribute Values
$attribute_code = 'YOUR_ATTRIBUTE_CODE'; // Write your attribute code here
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code); // Replace catalog_product with something else if attribute belongs to different model
$options = $attribute->getSource()->getAllOptions();
$optionsDelete = array();
foreach($options as $option) {
    if ($option['value'] != "") {
        $optionsDelete['delete'][$option['value']] = true;
        $optionsDelete['value'][$option['value']] = true;
    }
}
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->addAttributeOption($optionsDelete);

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1.9 – How to create attribute values programatically
$arg_attribute = 'plusstocklocation'; // Attribute code, can be color, brands, manufacturer or your custom attribute.
        $arg_value = 'Red'; // New value name

        $attr_model = Mage::getModel('catalog/resource_eav_attribute');
        $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
        $attr_id = $attr->getAttributeId();

        $option['attribute_id'] = $attr_id;
        $option['value']['any_option_name'][0] = $arg_value;

        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($option);

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento how to join a collection with SQL

Joining tables in Magento is relatively easy.

// Create som ecollection.
$collection = Mage::getModel('catalog/product')->getCollection();

// Join this collection with sizes.
                $collection->getSelect()->joinLeft(
                    array( 'sizes' => 'catalog_product_entity_int' ),
                    'e.entity_id = sizes.entity_id AND sizes.attribute_id = 132', // 132 is id of sizes attribute
                    array( 'size' => 'sizes.value' ));

This will produce the following SQL: SELECT `e`.*, `sizes`.`value` AS `size` FROM `catalog_product_entity` AS `e` LEFT JOIN `catalog_product_entity_int` AS `sizes` ON e.entity_id = sizes.entity_id AND sizes.attribute_id = 132 In case you want to do another type of join, there is also joinRight or joinInner


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1.9 How to select multiple categories by ids at once

I was looking for this very long time and I had to dig for a very long time to find it, hence it is so simple.

$categoriesIDs = [125,250];
$categories = Mage::getModel('catalog/category')
              ->getCollection()
              ->addAttributeToFilter('entity_id', array('in'=>$categoriesIDs))
              ->addAttributeToSelect('name');

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
Magento 1.9 – Order product collection by biggest percentage discount

The other day I had to create a new filter option for products which could order them by the largest discount. I made this simple workaround where I joined product collection with table “catalog_product_entity_decimal”, which is the table containing all the prices. “attribute_id” with value 76 is special_price.
Then, I created a new column “discount” which is calculated as:

discount = (price – special_price) / (price / 100)

 

// Join the special price into the discount column.
$store = Mage::app()->getStore();
    $_productCollection->getSelect()
                       ->joinLeft(
                           array( 'sfoi' => 'catalog_product_entity_decimal' ),
                           'e.entity_id = sfoi.entity_id AND sfoi.attribute_id = 76 AND sfoi.store_id = ' . $store->getId(),
                           array( 'discount' => '((price - sfoi.value) / (price / 100))' )
                       );
// Set order to DESC
    $_productCollection->getSelect()->Order( array( new Zend_Db_Expr( 'discount DESC' ) ) );

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.
How to create new products attribute programmatically

Attributes can be also created manually in Catalog > Attributes.
However, if your custom module requires to use new attributes, then the best practice is to create them programmatically, so when you install the module, new attributes will be created automatically without any further actions.

<?php  
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', 'magentoguides_attribute_key', array(
            'group'           => 'General',
            'label'           => 'Magento Guides front end label',
            'input'           => 'text',
            'type'            => 'varchar',
            'required'        => 0,
            'visible_on_front'=> 1,
            'filterable'      => 0,
            'searchable'      => 0,
            'comparable'      => 0,
            'user_defined'    => 1,
            'is_configurable' => 0,
            'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'note'            => '',
));
$installer->endSetup();

The new Attribute will be at the bottom of the product page by default. If you want to move it for example to prices tab. Then change ‘General’ to ‘Prices’. Or you can do it manually by navigating to¬†Catalog > Manage attribute sets

If instead you want to use a different type than ‘text’, change ‘type’ to for example ‘textarea’.


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.