Magento 1.8 CE

I decided to try out the new Alpha release of Magento 1.8 CE ( Community Edition ). As always I opted to install the sample data so that I have some products to play with.

The installation went fine, however as soon as I tried to re-index I got an error. It looks like there has been some logic created to stop duplicate URL keys. After some googling I found a nice little fix here: https://gist.github.com/Vinai/5451584

Defiantly worth remembering and using if you have a similar error.

New Magento Reading

A new magento book was release at Imagine this year and my copy just hit my home. This book is an updated to the Magento 1.3 version of the pakt book and talks about some good topics. The dev environment uses vagrant and there is an entire chapter on testing ( A little short for me and doesn’t cover alternatives to installing a module but at least its included.)

The way that the book is wrote is very good, Lots of examples of core code and theory behind each example. A very good read and a good up to date reference for any magento developer.

Megento Social Media Integration Toolkit update

The Magento Social Media Toolkit now has support for Pintrest. As with all the other social network integrations Pintrest has the option to be enabled / disabled depending on what the requirements are.

With all the social media integrations enabled a product page can look like this:

pintrest

The Magento Social Media Integration Toolkit now supports the following social networks:

  • Twitter
  • Facebook
  • Pintrest.

All are manageable via the admin html console. If there are any other networks people require let me know or fork the project on github.

Magento Social Integration toolkit module

Its been a action packed weekend for what started as a basic Twitter tweet button module, now we have a full simple to use social media integration module. Currently supported are Twitters tweet button and Facebooks like. All are customizable from the admin html section of Magento.

The module displays the below for end users of the site, For demo purposes I have included it on the product page but this module is available for any section of the site from CMS pages to products to category pages. All you need to do is place the XML in the section you want it to be displayed.

magentoSocialFrontend1

Ive changed how the module is displayed in the backed, in the code it still uses the Jcowie namespace however in the admin section its now known as the Magento Social Integration. Found via System -> Configuration. Each section has the ability to be enabled / disabled so if you only want Facebook thats all an end user will be given.

magentoSocialIntegration1

This next release will be available on git hub very soon. Enjoy and keep up the feedback its been great to hear from people installing this module already.

Magento Twitter Module updated

Ive updated the Twitter Module to work a little better, Now the module can be installed without being configured. This just results in the twitter sharing link not displaying until the username has been entered into the admin html console.

From the feedback that I have received im going to add the option to choose the style of button E.g. Large / small as well as have the ability to add a custom message that will be tweeted replacing the URL if set.

Im also considering adding a custom attribute to products that will act as the tweeted message so each product can have its own unique message if required.

Anyway the code is up on git hub for everyone to use, It has composer support so its super simple to install. Enjoy what is the simplest Social Sharing module for Magento.

Magento Twitter Module

As a Friday night project I decided to build a simple twitter tweet module, Its available to all via git hub so anyone can download install and modify to suite there needs.

Currently it has the ability to show a twitter badge on any aspect of the Magento site, Within the tweet it will grab the URL off the current page and insert a hash tag and from tag editable from the Magento adminhtml:

This example show the twitter share button added to the catalog product view page. In order to achieve this you need to add:

<?php echo $this->getChildHtml(‘tweet’) ?>

To the catalog/product/view.phtml template ( This will be fixed in the next release to use only XML and not require template to be updated. )

tweetButton

To configure the module is simple, under System -> Configuration there is a new option called Jcowie Twitter where you just need to enter username and hash tag save and the module is ready to use.

twitteradmin

When a user clicks the link they get the following box where they can send out a tweet about the product or page to all there followers. The number of tweets on that page then increases.

message

Enjoy the module and I will be updating it over the next few days / weeks / months.

Pair Programming TDD and other impractical things

I wasn’t able to get the PHP UK this year unfortunately but from watching all the videos it looks like yet another great conference. The one presentation that stands out for me is this one,

It really stood out to me as a great guide to how we should work as developers and look at our approach. Definatly one that I will be watching again.

Profiling Magento modules

As we all know magento does some heavy lifting to deliver what it does, Sometimes we can help ease the load on magento by looking at some of PHP’s better practices. For a module ive been developing I decided to profile it to see what I could improve.

My module had an IndexController and within the indexAction I added the following:

Mage::log(memory_get_usage());

$sales = Mage::getModel(‘sales/orders’)->getCollection();

Mage::log(memory_get_usage());

foreach($sales as $sale) {

echo $sale->getCustomerEmail();

Mage::log(memory_get_usage());

}

Mage::log(memory_get_usage());

Now before you question why im loading all sales and not limiting, Think about business cases. The client wanted to be able to export all sales orders to check and validate in store sales.

The problem is that the stack memory for PHP was getting higher calling get collection, then during the foreach php loaded the entire array into memory and maxed out causing the script to fail. PHP’s internals for a foreach mean that during a loop the foreach copies the array before looping and copies the current(array) value per iteration

To optimize the script I used a for loop and used count on the collection this increased memory and visual performance.

Working with the Magento API v2

A project im working on at the moment has the requirement to pull certain stats in from Magento sites that might not be hosted on the same server, Or even have remote MySQL access.

I did some research and decided the explore the Magento API, More specifically SOAP API v2.

The example we will work with should help you show how to load the days sales orders from the Magento API.

As were working with a SOAP API we need to install php-soap extension on both the client machine making the requests and the server where Magento is located. On more distributions this is as simple as apt-get install php5-soap. ( If your using debian ).

Now were almost set up ready to develop some code, However we need to create an API user in Magento and give that user some permissions.

From the Magento AdminHTML section navigate to:

System -> Web Services -> SOAP XML/RPC Users

This section will give us the ability to create a new user. Save the user and now select:

System -> Web Services -> SOAP XML/RPC Roles

Create a new role with some permissions so that we can test the API. In a production usage we should be more restrictive with what a user has access to but for this example select all.

The final step here is to go to the user section again and assign the user we created to the role we created.

Lets create some example code that will pull in some order information from the today, Create a new file anywhere on your system.

The first aspect is the instantiate a SoapClient instance with the API URL of the store:

$client     = new SoapClient(“http://www.example.com/api/v2_soap?wsdl\
=1″);

This initializes client with an instance of the SoapClient and gets us ready to make some calls.

Now we need to authenticate with the Magento instance, Using the SoapClient instance lets make a call to get us authenticated:

$sessionId = $client->__soapCall(‘login’, array($username, $password));

If we entered the correct username / password combination here we should have a connection to Magento and be ready to query some information. Lets make a call to the salesOrderList using a filter:

       $complexFilter = array(
‘complex_filter’ => array(
array(
‘key’ => ‘created_at’,
‘value’ => array(‘key’ => ‘gt’, ‘value’ => date(‘Y-m-d 00:00:00′))
)
)
);
$result = $client->salesOrderList($sessionId, $complexFilter);

As I want to filter by an attribute I need to set up a Filter, The Magento API refers to these as complexFilters but in essence im just setting a key => value array where key is the attribute and value contains what I want to filter, In this instance I want to search for all salesOrders where created_at is greater than this morning.

Dumping out the value of result will give you all the orders that have been placed today, When this API call is executed its returning us a similar call to Mage::getModel(‘sales/order’)->getCollection(); We have access to all the orders properties.

The only problem I have faced so far is that the API is a little slow compared to native Magento calls, Yet to have this remote interface packed with as many features as the Magento API has im sure we can live with it.