About jontas

I like to make things that people find useful.

Magento change store via url

In a multi store environment there is sometimes a need to change the store when the user clicks a link.
A “simple” way to do this is to pass the store along in the url (just add a “?___store=” either id or the store name, so for instance.

$stores = $category->getStoreIds();
echo '<p><a href="'. Mage::getUrl() . '?___store=' . end($stores). '">'.$category->getName()."</a></p>";

PHP showing the name of the current file

Sometimes there is a need to show the name of the current file.
Using $_SERVER[‘PHP_SELF’] will only show the file that is executed (not the a included file)
Using __FILE__ gives the current file (with a full path)

For this example running.php is accessed in the browser/cli.

//included.php
<?php
echo $_SERVER['PHP_SELF']; //will show running.php
echo __FILE__; //will show included.php
?>
//running.php
<?php
INCLUDE('included.php')
?>

SimpleXml save formated output

When using the SimpleXml->asXML(‘file.xml’) the output is simply written onto one line.
like

<?xml version="1.0" encoding="UTF-8"?>
<product><companyId>1</companyId><productId>1:1</productId></product>

There is nothing wrong with this but if you add line breaks and indentations the xml file looks better and is easier to (manually) read.
Unfortunately there is no way to do this using SimpleXML, but there is a quick and dirty way to do this; and that is to import the SimpleXMLobject to a DOMElement and do it there so some example code

$xmlDom = dom_import_simplexml($simpleXmlObject);
$xmlDom->formatOutput = true;
$xmlDom->save("test.xml");

This would result in an xml file looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<product>
	<companyId>1</companyId>
	<productId>1:1</productId>
</product>

Easier to read but takes some extra space on the disk (might not be much but it is good to remember).

Magento: Get the skin url

When editing Magento tempates or CMS pages you will have a need to access images and other content inside the skin/—/images folder. Instead of making hard coded links in the CMS or templates a better way is to get this from Magento (one of the many reasons for doing this is that it will still work if something in the directory structure is changed – like say you dev server is dev.something/shopverige but the live store is directly on a domain like say http://www.shopsverige.se

Here is how to do it:

<!-- this is inside of .phtml files -->
<img src="<?php echo $this->getSkinUrl('images/coolimage.png'); ?>" alt="arrow"/>
 
<!-- use this is in cms block -->
<img src="{{skin url='images/coolerimage.png'}}" alt="" />

Internet Explorer not rendering empty div correctly

Internet Explorer won’t render empty divs correctly.

I had a div with a height and a background image but no content; It was not rendered with the correct height it was only like one line.

A work around for this is to have some “content” inside the div (like a whitespace, a comment or why not both) then Internet Explorer renders the correct height.

<div class="some-class-with-size-and-background">
&nbsp;<!-- keep to get IE to render this div -->
</div>

PHP5: Quick on calling a parents constructor

Using OOP there comes a need to call the constructor of a parent class, this is not hard to do

class TestParent {
    public function __construct() {
        var_dump('blah');
    }
}
 
class TestChild extends TestParent {
    public function __construct() {
        parent::__construct();
    }
}
 
$a = new TestChild(); //Output will be: string 'blah' (length=4)

In PHP4 this would have looked like (this still works in PHP5)

class TestParent {
    public function TestParent() {
        var_dump('blah');
    }
}
 
class TestChild extends TestParent {
    public function TestChild() {
        parent::TestParent();
    }
}
$a = new TestChild(); //Output will be: string 'blah' (length=4)

Magento Selling clothing (or anyother product with options) while having Magento keep track of stock

Creating confugurable products is more work than adding custom attributes but it might be better in some cases (keeping track of stock, having one set of attributes to select from etc).

In short the steps needed are

  1. Create the attributes that will be configurable by the user – for our example they will be Size and Color
  2. Create the attribute set that will be assigned to the variant products – for our example, we’ll call it “T-shirt”
  3. Create the individual variant products
  4. Create the configurable product, and add the “T-shirt” attribute set
  5. Add the individual variants to this configurable product

Create the attributes that will be configurable by the user – for our example they will be Size and Color
In the adminpanel this is under Catalog->Attributes->Manage Attributes
Scope needs to be set to Global and Input type to drop down. Once this is done then Use To Create Configurable Product is shown and this should be set to yes. Setting required to yes is a good idea as well.

Under label / options create a label (for instance size) and give the different options it can take.

Repeat for all attributes you want to create (for instance size and color)

Create the attribute set that will be assigned to the variant products – for our example, we’ll call it “T-shirt”
Now we’re ready to create an attribute set called “T-shirt” to start using for this product. Go to “Catalog -> Attributes -> Manage Attribute Sets” and press “Add New Set”.
Give the new set a name and then drag and drop the attributes to the group where you want them to be when creating a product.

Create the individual variant products
Create a product and make sure that you use the attribute set you have created. Add the attributes where you placed them.


Repeat for all the products you have

Create the configurable product, and add the “T-shirt” attribute set

Add the individual variants to this configurable product
This can be done when the configurable product is created or later on using edit product.

And then it should look something like this in the shop

Magento Selling clothing (or another product with options) without having Magento keep track of stock

Some products have a number of options that are important for the customer to select.
Here I show how to make a product with options (color and size) where Magento only sees one product – so it won’t it will not be possible to see how many large in red you have from Magento.

When you create a product in the admin panel there is a label Custom Options.


Use “Add new option” to make the option (color, size or whatnot)
Then for each of the options use add new row to add the deferent values that the option can have.
The column labeled price is for adding a extra cost for a option (if for instance the red shirt should be more expensive).

Then this will look something like this in the store

Then only down sides I have seen with this is that I have to add the options one time for each of the products (but that is also nice since not all products have the same values) and that Magento will treat this as one single product so I can’t let Magento manage the stock for this/these products.