Our blog

 

If you want to serve up text files for download (perhaps product feeds etc) then you might like this little snippet.

Not only will it force the file to be downloaded but it allows you to specify a custom filename that it should be saved as.

PHP:
  1. if(isset($_GET['download_file'])){
  2.     header("Content-type: application/octet-stream");
  3.     header("Content-Disposition: attachment; filename=Export.txt");
  4.     readfile('Export.txt');
  5.     die;
  6. }

More Reading:



 

The first development release of Magento 2 has a few areas worthy of note for developers of the coming changes to Magento.

Magento 2 now requires PHP 5.3
Zend is still version 1.11.1.
Magento 2 now implements templates on a per modules basis. So now all the different bits of the theme are separated into the modules it belongs to. This means that any existing version 1 theme cannot be dropped into version 2.
Themes in Magento 2 have the concept of variations of the same theme. So a theme can have different CSS and images but use the same templates, this can be configured per store per user agent in the admin. This is useful if you have different seasonable themes that are the same as each other but with a few different images and colours. To achieve the same in Magento 1 you would have to use packages and Magento's ability to cascade up, this is not longer necessary.

This development release of Magento has a major focus on updating the theme and template system to be more ridged unlike Magento version 1 where the template files could be basically any where within a theme.

More Reading:



 

Magento version two is coming. For those of us who are eagerly awaiting the next major version of the most popular and powerful open source e-commerce platform, you can now see, track and download the code on GitHub.

Not had chance to have an in depth look as yet but we expect great things!

https://github.com/magento/magento2

More Reading:



 

If you are thinking of adding a confirmation of e-mail address to the front-end registration pages of your store e.g the Checkout billing page or the customer account registration, the little snippets below could be of use to you

For the Checkout billing page
1. Locate the magento checkout billing page (billing.phtml) which can be found in app/design/frontend/default/ /template/checkout/onepage
2. Add code 1 below where situable;
Code 1

HTML:
  1. <div class="field">
  2. <label for="billing:confirm_email" class="required"><em>*</em><?php echo $this->__('Confirm Email Address') ?></label>
  3. <div class="input-box">
  4. <input type="text" name="billing[confirm_email]" title="<?php echo $this->__('Confirm Email') ?>" id="billing:confirm_email" class="input-text required-entry validate-cemail" />
  5. </div>

For the validation of this email confirmation field with the actual email field
3. Locate the Javascript file which Magento uses for validation (validation.js) in /js/prototype
4. Add code 3 where suitable e.g immediately after this line(code 2) in validation.js
Code 2

JAVASCRIPT:
  1. return !(pass.length <7);
  2.     }],

Code 3

JAVASCRIPT:
  1. ['validate-cemail', 'Please make sure your emails match.', function(v) {
  2. var conf = $$('.validate-cemail')[0];
  3. var pass = false;
  4. if ($('email')) {
  5. pass = $('email');
  6. }
  7. var emailElements = $$('.validate-email');
  8. for (var i = 0; i <emailElements.size(); i++) {
  9. var emailElement = emailElements[i];
  10. if (emailElement.up('form').id == conf.up('form').id) {
  11. pass = emailElement;
  12. }
  13. }
  14. if ($$('.validate-admin-email').size()) {
  15. pass = $$('.validate-admin-email')[0];
  16. }
  17. return (pass.value == conf.value);
  18. }],

Then that should just do exactly what you want.

For the Customer Account Registration page

1. Locate the magento account register page ,register.phtml in
app/design/frontend/ /default/template/persistent/customer/form/
2. Include code 4 where suitable
Code 4

HTML:
  1. <div class="field">
  2. <label for="confirm_email" class="required"><em>*</em><?php echo $this->__('Confirm Email Address') ?></label>
  3. <div class="input-box">
  4. <input type="text" name="confirm_email" title="<?php echo $this->__('Confirm Email') ?>" id="confirm_email" class="input-text required-entry validate-cemail" />
  5. </div>
  6. </div>

After this, you should have another field on your registration page which can be subjected to any styling you need to do, most importantly when styling do not change the tag id's and class names, in a situation where you need to change them, you must also change the javascript selectors accordingly.

I hope this works for you.

More Reading:



 

If you have created a new product type and need to enable it to be included with configurable products you need to let Magento know that it should allow your product type to work with configurable products. To do this open your config xml and add the following xml.

XML:
  1. <config>
  2.     <global>
  3.         <catalog>
  4.             <product>
  5.                 <type>
  6.                     <configurable translate="label" module="catalog">
  7.                         <allow_product_types>
  8.                             <custom_type_name/>
  9.                         </allow_product_types>
  10.                     </configurable>
  11.                 </type>
  12.             </product>
  13.         </catalog>
  14.     </global>
  15. </config>

This tells Magento to include your product type for use with configurable products.

More Reading:



 

I recently needed magento to be able to load a product, and if that product was part of a configurable product detect this and return the parent product if that existed. Looking around google I came across this code snippet and thought it should work

PHP:
  1. $_product = Mage::getModel('catalog/product')->load($productId);
  2. $parentIdArray = $_product->loadParentProductIds()
  3.                  ->getData('parent_product_ids');
  4. if(!empty($parentIdArray)) {
  5.     // do something
  6. }

The problem is that the loadParentProductIds method was depreciated in 1.4.2.0 and I'm running 1.6. The new way of doing detecting if a product has a parent is to do this

PHP:
  1. $configurable_product_model = Mage::getModel('catalog/product_type_configurable');
  2. $parentIdArray= $configurable_product_model->getParentIdsByChild($productId);
  3. if(!empty($parentIdArray)) {
  4.     // do something
  5. }

Using this method I overrode the Mage_Catalog_Model_Product class and added this method, which will always return a configurable product if the product is associated with one. Be aware, that if you products belong to more than one configurable product you will have to modify the logic

PHP:
  1. public function getConfigurableProduct() {
  2.     switch ($this->type_id) {
  3.         case 'configurable':
  4.             return $this;
  5.             break;
  6.         default:
  7.             $configurable_product_model = Mage::getModel('catalog/product_type_configurable');
  8.             $parentId= $configurable_product_model->getParentIdsByChild($this->getId());
  9.             if(isset($parentId[0])) {
  10.                 $this->load($parentId[0]);
  11.             }
  12.             break;
  13.     }
  14.  
  15.     return $this;
  16. }

More Reading:



 

If you are a PHP developer looking for a job in the Bradford area then please do get in touch with Edmonds Commerce.

We are a team of PHP developers who focus on e-commerce based on open source packages such as Magento, osCommerce, Prestashop, OpenCart and others. We also do a fair bit of WordPress, Drupal, Zend Framework etc as well. We even do bespoke PHP development.

If you are a skilled PHP developer and would like to join a team of enthusiastic developers in a friendly flexible environment with a focus on productivity, personal skills development and fun then get in touch with us today.

More Reading:



 

If you use Netbeans then you have no doubt seen the red wavy underline that pops under lines that have errors.

Unfortunatley if your errors relate to punctuation, eg concatenation full stops, then the red wavy line can actually obscure these making finding and fixing the error tricky.

An nice alternative is to set Netbeans to use a red strikethrough instead of the wavy line. This is just as visilble but does not obscure punctuation in any way making finding and fixing the error a lot easier.

To change this simply go to Tools->Options->Fonts & Colors

Then Set Language to All Languages

Then select Error in the category drop down

Simply change the Effects: drop down from Wave Underlined line to Strike Through

More Reading:



 

Just found out this bug that occurs when you try to call an overridden Magento magic methods within the override method. I created a custom attribute for a product with code "price_grid_csv" and I needed to still call the actual Magento magic method; see the below code

PHP:
  1. public function getPriceGridCsv(){
  2. $price_csv=parent::getPriceGridCsv();
  3. if(condition)
  4. $price_csv=<further processing of $price_csv>
  5. return $price_csv;
  6. }

By default parent::getPriceGridCsv() should return the value of the current object with attribute code price_grid_csv, but it does not because it breaks the camel case structure of the method name by changing getPriceGridCsv to getpricegridcsv which breaks magento from returning the current object attribute with this code.

The way I got around this was by simply calling $this->getData('price_grid_csv') or by running Magento on PHP versions higher than 5.2.9, but I would recommend the first solution in order to make your code PHP version independent.

Also note that LAMP 1.7.1 is packaged with PHP 5.2.9

More Reading:



 

I have been making a mwnu system for magento that would allow a client to add extra links to CMS page to top menu through a static block. One of the requirements was when you clicked the link the menu should display as active.

To do this I extended the Category_Navigation Block so it would get the content of the static block and then check each href against the current url. If they matched then it would add the active class to the menu structure. This worked perfectly when running locally, so I pushed it to our staging site where it broke.

Assuming that there was a difference with the url structure on the stagin site that I hadn't anticipated, I echoed out the link variables and compared them, but they looked identical. Thinking there may be spacing that had snuck past trim, I var_dumped them and again they looked identical, apart for the fact that the character count was different.

After much messing around I came to two conclusions. Firstly Magento will append a session id to urls if you're using multi-store, and far more importantly, this extra information is not displayed if you var_dump the variable. I finally found this by turning the string into an array and echoing each character onto a new line, like so

PHP:
  1. $link = $this->getUrl();
  2. // outputs http://www.example.com/index.php
  3. var_dump($link);
  4. $array = preg_split('//', $link, -1);
  5. foreach($array as $char) {
  6. // simplified replace with print($char."\n") to get each character on a new line
  7. print($char);
  8. }
  9. // outputs http://www.example.com/index.php?___SID=U

To solve this replace your link with the following

PHP:
  1. $link = preg_replace('%\?.*%', '', $this->getLink());

and it will work the way that you expect.

More Reading: