Skip to content

Magenting moving to www.catgento.com

Hey few people that run into this blog.

If you want to continue following me, go to www.catgento.com. From now on, I’m going to post there (so I can get rid of adds and have a domain that doesn’t conflicts with others!).

Get customer product alert subscriptions list in Magento

Some things are easy, some things are not. This one is easy-peasy.

If you want to get the product list that a customer has been subscribed to (price or stock alert), you need to use the productalert model.

  • Product stock alert list

$customer_id = Mage::getSingleton('customer/session')-&gt;getCustomer()-&gt;getId();</p>
<p>$customer_stock_alerts = Mage::getModel('productalert/stock')<br />
         -&gt;getCollection()<br />
         -&gt;addFieldToFilter('customer_id', $customer_id);</p>
<p>foreach ( $customer_stock_alerts as $alert )<br />
{<br />
 echo $alert-&gt;getProductId();<br />
}

  • Product price alert list

$customer_id = Mage::getSingleton('customer/session')-&gt;getCustomer()-&gt;getId();</p>
<p>$customer_price_alerts = Mage::getModel('productalert/price')<br />
         -&gt;getCollection()<br />
         -&gt;addFieldToFilter('customer_id', $customer_id);</p>
<p>foreach ( $customer_price_alerts as $alert )<br />
{<br />
 echo $alert-&gt;getProductId();<br />
}

What you need if you want to develop with magento

Here are a few tips to take into account if you want to start developing with Magento.

On the backend

Go to System » Configuration and click on the Advanced » Configuration tab. There you should enable the profiler (debug tab) and also the log (log settings tab). Remember that the /var directory has to have writing permissions.

Developer Mode

Enable the developer mode in htaccess (or your vhost configuration). If you want to develop on Magento you have to enable the developer mode. Just add the following line to your .htaccess file (or vhost configuration)

#Magento Developer Mode
SetEnv MAGE_IS_DEVELOPER_MODE "true"
  • Edit the index.php file

Next step is to edit the index.php file in order to enable the Varien Profiler and also display errors. Your index.php file should look like this:

Varien_Profiler::enable();
if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
}

ini_set('display_errors', 1);

MySQL performance

When it comes to performance we always need to know which queries in Magento are taking more time to execute. It is possible to log all the queries and also show (along with the profiler) how long it took to load the page, how long was the longest query (in  time) and the longest query itself. In order to enable this, do the following:

  • Modify: lib/Varien/Db/Adapter/Pdo/Mysql.php and set to true: $_debug, $_logCallStack and set to “0″ the $_logQueryTime value.
  • Create a writable directory in var called debug (that is, var/debug) in your magento directory.

Once you do this. At the bottom of each loaded page in Magento you’ll see the profiler and also the mysql data. You can also find a file called pdo_mysql.log with all the queries that have been executed during the page loading. You can also watch this file and see in live mode which queries are being executed and see the time each query needs to do its work.

Useful extensions

Install a couple of useful extensions. There are many extensions to help developers. Not all of them work in all Magento versions. Up to now I’ve found two extensions that are free and compatible up to Magento CE 1.6 and EE 1.11.

  • Wee_developertoolbar | It adds a tool button at the top-right corner of our website showing some basic information. Clicking there we’ll be able to see information about blocks, mysql queries, clearing the cache, enable translation, block hints, etc. It’s the greatest extension for developers I’ve found up to this moment

  • Layout analyzer | This extensions allows us to see what’s the final layout that has been loaded on our page. The most interesting thing is that we can take a look at the final xml layout that has been used to load the page.

Conclusion

With all this you should have all the tools you need to start developing on Magento and be able to debug properly. Magento is somewhat hard sometimes, so it’s important to have all the available debugging and developing tools with us.

Multiple newsletter lists in Magento

The best extension I’ve run into related to multi-newsletter (or simply newsletter groups) is the following one:

It’s free and works like a charm. It allows us to easily create newsletter groups from the backend (Newsletters » Newsletters groups) and then modifies the newsletter blocks on the website to add more checkboxes, so the customers can check on the groups they want to sign up. After that, we can send/exports newsletters filtering by these groups.
Newsletter groups on the magento backend

Newsletter groups on the magento backend

Adding a category attribute in Magento

I was looking for a code in order to create category attributes in Magento and found a post from Mukesh Chapagain where he explains in a really easy way how to do so. But it didn’t work for me. I’m supposing that the problem is the way how we call the installer in the mysql php file. So I want to explain here, step by step, how I did to make it work:

Create the module

Create a file YourCompany_CategoryAttribute.xml file with the following code and place it in your app/etc/modules folder:

<config>
  <modules>
    <YourCompany_CategoryAttribute>
      <active>true</active>
      <codePool>local</codePool>
    </YourCompany_CategoryAttribute>
  </modules>
</config>

Now create the folder structure for your module. Basically, you need to create the folders: app/code/local/YourCompany/etc and app/code/local/YourCompany/sql/categoryattributesetup. In the first one you have to place a config.xml with this content:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_CategoryAttribute>
            <version>0.1.0</version>
        </YourCompany_CategoryAttribute>
    </modules>
    <global>
            <resources>
                <categoryattribute_setup>
                  <setup>
                    <module>YourCompany_CategoryAttribute</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                  </setup>
                  <connection>
                    <use>default_setup</use>
                  </connection>
                </categoryattribute_setup>
            </resources>
    </global>
</config>

Finally, create the mysql php file that is going to create the category attribute:

<?php

$this->startSetup();

$this->addAttribute('catalog_category', 'new_attribute', array(
    'group'         => 'General',
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'New attribute',
    'backend'       => '',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

$this->endSetup();

?>

This will create a new tab on the category edit page named «General» and inside it you’ll find the new attribute name «New attribute».

 

Fixing reindexing price errors in Magento

I’ve upgraded a couple of Magento CE (1.4) to Magento EE (1.11) using the traditional (and effective) way. But I found that there was an important issue when reindexing prices that prevented Magento from doing it.

Reading this post on Magento Boards I found that the problem (in my case) was related to an error with the catalog_product_entity_tier_price table where some of the entries were related to products that didn’t exist anymore. This was the actual error and when trying to reindex product prices, Magento would throw this message:

There was a problem with reindexing process.

How to solve it

So you want to solve this. You have to delete all the entries on the catalog_product_entity_tier_price table that are related to a product that doesn’t exist anymore. You can do it manually or you can use the following script I created. It’s really easy to use.

In that file you’ll find:

  • The script: clean_tier_prices.php
  • A library to connect in a really easy way to the db

Follow this steps:

  1. Make a copy of your database (just in case)
  2. Uncompress the zip file in your magento base directory
  3. Edit the lib/db.php file and put there your connection settings (db user, user pass and db)
  4. Open the browser and go to http://yourmagentopath.com/clean_tier_prices.php
  5. You’ll see wich references have been deleted from the catalog_product_entity_tier_price table
  6. Try reindexing again in your Magento backoffice

Important note: You use this script at your own risk. Make a copy of your database before applying it. If you improve this, please share it with me and the rest of the world!

 

Follow

Get every new post delivered to your Inbox.