Magento: Remove all categories from a store

This is just the Sql commands that I just discovered today in order to to remove all categories. For more information, please check here. Please also notice that you may have to change the prefix of database tables, enjoy 🙂

SET FOREIGN_KEY_CHECKS = 0;

TRUNCATE TABLE `catalog_category_entity`;
TRUNCATE TABLE `catalog_category_entity_datetime`;
TRUNCATE TABLE `catalog_category_entity_decimal`;
TRUNCATE TABLE `catalog_category_entity_int`;
TRUNCATE TABLE `catalog_category_entity_text`;
TRUNCATE TABLE `catalog_category_entity_varchar`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `catalog_category_product_index`;
INSERT INTO `catalog_category_entity`
(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`POSITION`,`level`,`children_count`)
VALUES
(1,3,0,0,‘0000-00-00 00:00:00’,‘2009-02-20 00:25:34’,‘1’,1,0,1),
(2,3,3,0,‘2009-02-20 00:25:34’,‘2009-02-20 00:25:34’,‘1/2’,1,1,0);
INSERT INTO `catalog_category_entity_int`
(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`)
VALUES
(1,3,32,0,2,1),
(2,3,32,1,2,1);
INSERT INTO `catalog_category_entity_varchar`
(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`)
VALUES
(1,3,31,0,1,‘Root Catalog’),
(2,3,33,0,1,‘root-catalog’),
(3,3,31,0,2,‘Default Category’),
(4,3,39,0,2,‘PRODUCTS’),
(5,3,33,0,2,‘default-category’);
SET FOREIGN_KEY_CHECKS = 1;

Building a Magento Admin Module

Have you ever wanted to build a custom module for the Magento admin panel? Today I am going to share this information hoping you will find this post useful!

Creating the Module Folder Structure

Lets start by creating the following folders:

app/code/community/Toto/ImportXml/etc
app/code/community/Toto/ImportXml/controllers
app/code/community/Toto/ImportXml/Helper

Building the Module

Within the ‘etc’ folder create a file called config.xml and paste in the following code:

configxml

Next create a file called adminhtml.xml within the ‘etc’ folder and paste in the following:

adminhtml

We need to tell Magento to load our module and to do this we need to create a file within /app/etc/modules. I called my file Toto_ImportXml.xml and it contained the below XML code:

xml

Now if you refresh your Magento admin panel, you should notice a new tab called ‘Tofu & Tomato Commerce’. However, if you click on it you will be given a PHP error saying that Magento can’t find a helper class. This is because within adminhtml.xml we told Magento that the tab is associated with the module ‘importxml’ and by default Magento looks for a helper file called Data.php.

module=”importxml”

In the config file we also declared the helper class within the global node.

globalhelper

To fix this problem we need to create a file called Data.php with the Helper folder (/app/code/community/ Toto/ImportXml/ Helper/Data.php). This will be an empty class for the time being but it will fix the warning we are currently getting. Paste the following into Data.php:

helper

Note: The class name needs to be file path structure to Helper/Data.php.

The final stage is to add the controller. This will route the user to the method specified within adminhtml.xml. For example lets take the following:

<action>greenmodule/index/index</action>

Magento will look in the module ‘Greenmodule’ for an index controller with the method called indexAction. So lets create a php file called ‘IndexController.php’ within the controllers folder (/app/code/community/Toto/ImportXml/controllers/ IndexController.php). Paste in the below PHP code:

controller

The above method simply creates a block with a the heading ‘Tofu & Tomato Commerce’ by using Magento factory methods. Refresh your browser and you click on the custom menu tab – you should now see a brand new page with the h1 tag.

Note: You may have to clear your magento cache or log out and log back in for the new page to show. This has caught me out many times.

tofu&tomato-commerce

Enjoy!

Magento: Get Bundled Items name / title by bundled product Id

This post is jut a small snippets that will help you to get all bundle items’ title.

Of course, I’m not sure how useful can it be as is, so if you need optimized version of it, you will need to play with it for a while. I tried to find it on google few days ago and I had no luck, so if anybody has better solution, please comment.

Here we go:

bundle products

Bundled Products, Magento will not recalculate the Price – Bundle.reloadPrice()

Hello, I’m Tofu, nice to see you again.

My client gave me a purchased Magento template and asked me to install and configure a new Magento store for him. The template was very clean and professional. However, there was an weird issue since it did not support the latest Magento version 1.9.

I created serveral bundle products just for testing purpose then I added or subjected items to the cart, the price did not change. I got js errors:

Uncaught ReferenceError: taxCalcMethod is not defined 
Uncaught TypeError: Cannot call method ‘reloadPrice’ of undefined

Finally, I figured out the problem. We will need to copy the following javascript snippets from BASE theme to our activated theme in
/app/design/frontend/default/YOUR_TEMPLATE/template/
/bundle/catalog
/product/view/type/bundle.phtml

bundle-magento-products

Enjoy!

[Tofu And Tomato] [Magento Solutions] Fix email not sent

Today, my client at http://www.dubuy.it/ told me that, he had an weird problem with email settings in Magento, he had never receive any sale / order emails after submitting one-page-checkout form.

If the order displays the email as sent and you or your clients are not receiving the email, then the problem is most likely the email settings you have for Magento or the local email server that Magento is using.

I also checked the settings:

  • Admin -> Configuration > Advanced -> System > SMTP Settings and see that you do not have emails disabled.
  • Admin -> Configuration > General > Store Email Addresses and check if you have the email addresses in there correctly. Sometimes email servers would refuse to send email if the senders email is not a proper email address.

However, the settings are correct but emails still were not sent. After digging into the code, I found the issue:

Just been looking into this myself. I’ve followed the code and functions back to this function “public function sendNewOrderEmail()” in:

app/code/core/Mage/Sales/Model/Order.php

starting at line 1270 is the following code:

$emailSentAttributeValue = $this->load($this->getId())->getData('email_sent');
    $this->setEmailSent((bool)$emailSentAttributeValue);
    if ($this->getEmailSent()) {
        return $this;
    }

This is checking the sales_flat_order db table to see if the email has already been sent to the customer and if so just to return, but this does not send the email and should throw an error. So I would say it is a bug somewhere. To fix it so you can resend order/invoice emails etc..

Copy

app/code/core/Mage/Sales/Model/Order.php

to

app/code/local/Mage/Sales/Model/Order.php

and comment out line 1273

if ($this->getEmailSent()) {
            //return $this;
        }

Works like a charm for me. But literally only this second tested it.

magento-fix-email-not-send

[Tofu And Tomato] How to add CSS classes to Magento top links.

Ever wonder or ever hit your head on the wall what bloody template file controls the site link, footer link in Magento and how to add a class to it? That’s right, it’s thepage/template/links.phtml.

The template file generates ‘first’ and ‘last’ class in li tag, but sometimes we need more styling in each individual link list and we want to add a class either in li or anchor tag without hacking the core file or extend a class in Magento core, how do we do that?

The answer is in the xml file

magento-top-links

LET’S HAVE A LOOK AT MY CODE:Screen Shot 2014-10-18 at 11.38.48 AM

[Tofu And Tomato] Magento – How to add another language to your store

I currently have two sites under development that are going to take advantage of multiple languages.

One will have a second store view in French and another will have a second store view in Spanish.

The fist step is to change the default store view name to English. Login into admin and go to the System>Manage Stores section.
Click on Store View Name for your default store. Change the store name to English. Then save.

Next step is to create the second store view, which will be Spanish.
Click on Create Store View button at top.
Enter your information for the Spanish store per image below:

new-spanish-store

Click on Save Store View and you should have a new Spanish store view.
On the front end, you will now see that there is a store switcher in the header area.

switcher

Now, we have two stores, one english and one spanish.
But as you can see, it’s not all working as it should.
If you go to the home page in the Spanish Store, we will get a 404.
You can fix that by assigning the home page to All Store Views, per below.

home

You will also need to go to Catalog>Manage Products and select all your products, then update attributes.
Then go to websites section, per below, and add product to website.

update-products

You should now see all your products showing in categories, regardless of what store view you are in.
We will cover products later.

If you take a quick look at your Spanish view store, you will notice that it’s not really in Spanish, it’s still in English.
That’s because you haven’t given Magento any translations for your Spanish store. That’s next.

Go to Magento’s Translations Page.
Search for your language pack then click on it to go to the pack’s download page.
You are now ready to download the Spanish pack, see below.

translations

Save the es_MX folder to your desktop, and upload to your server in the app>locale folder.See below.

locale

Now that you have installed the Spanish language page, it’s time to tell Magento about it.
Go to System> Config> General tab, then click on Locale Options tab.
In top left, select Current Configuration Scope, and select Spanish. See below.

config Now that Magento knows about our Spanish language pack, it should be serving translated files in the front end.
Let’s see…

translated

Finally, we will talk about product translations.
If you want to have Spanish in a product, for example, you will need to go to Catalog>Manage Products.
Select your product, then select Spanish store view in top left.
Add your Spanish translations to short description, for example, by unchecking ‘Use Default Value’ and then pasting in your Spanish translation. See below.

short-desc

Here is result of that translation on front end…

short-desc-tl

I know that’s alot of steps, but it’s really amazing what we have just done.

We now have a multi-language store, serving different visitors, in their own tongue.