Subscribe To RSS Feed


Mike D's SharpBlog

Just another Sharpdot weblog

Posts Tagged ‘magento’

Magento: Customer Desired Delivery Date Module

- Monday, February 22nd, 2010 -

This Magento Module allows you to add a date picker to the checkout process, which the customer can use to input a desired delivery date. There is minimal setup required. You will need to add the supplied code to your template pages in order to display the date-picker on the checkout process and add one line of code to the admin template. As soon as I find a convenient way to add the necessary block to the template files with out having to modify the template files I will remove the manual part of the Module.

Here is a link to the modules page: Delivery Date Plugin

To Add to email templates: (provided by Magento user: mrgniarf)
You can modify yourtheme/yourdesign/template/email/order/items.phtml and before :

<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border:1px solid #bebcb7;background:#f8f7f5">
    <thead>
        <tr>
            <th align="left" bgcolor="#d9e5ee">__('Sku') ?></th>
            <th align="left" bgcolor="#d9e5ee">__('Item') ?></th>
            <th align="right" bgcolor="#d9e5ee">__('Subtotal') ?></th>
            <th align="center" bgcolor="#d9e5ee">__('Qty') ?></th>
        </tr>
    </thead>

Add:

<table cellspacing="0" cellpadding="0" border="0" width="100%">
    <thead>
        <tr>
            <th align="left" width="100%" bgcolor="#d9e5ee">__('Desired Arrival Date') ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td valign="top" style="padding:7px 9px 9px 9px;border:1px solid #bebcb7;border-top:0;background:#f8f7f5">
                helper('deliverydate')->getFormatedDeliveryDate($_order->getShippingArrivalDate()); ?>
            </td>
        </tr>
    </tbody>
</table>
<br>

Tags: , ,
Posted in magento | 28 Comments »

Transfering Magento Database

- Wednesday, April 8th, 2009 -

*Note: The Best way to migrate the database is to do it command line, using mysqldump.

I have Had a few problems when transferring a Magento database. Usually when pushing a development site to Production. Importing the database sometimes throws foreign key constraint errors. What I have found is that if you use phpmyadmin to export the database you need to check the box next to disable foreign key checks. Then you can import this database with out getting the constraint errors. Exporting the database through Magento’s built in functionality does this automatically.

The next issue I have run into is that( I believe, but don’t quote me ) mysql versions prior to 4.1 don’t automatically add the commands to allow inserting primary keys with an id of 0. This will cause Magento to throw an error. The way I have found to fix this is by manually editing a few db entries to set them to an id of 0. The tables I found that need to be edited are: core_store, core_website, core_store_group, and customer_group. I believe it will allays be the last entry that needs to be set to zero, but if in doubt compare it against the old database.

Hope this helps someone else, and let me know of any other work-arrounds to this issue.

Tags: ,
Posted in magento | 2 Comments »

Magento: Getting product attributes values and labels

- Monday, April 6th, 2009 -

I have found that it is very useful to be able to get attributes from the system and use them in places other than a products category page. I always forget the exact syntax to use so, this is going to be my unofficial cheat sheet.

This is how to get a drop down lists options. I don’t think it will work for a mulit-select attribute. I stick the value/label pairs into an array to use how I please.

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attrubuteArray[$option['value']] = $option['label'];
}

I had a trickier time getting values for a multi-select attribute. I don’t think that this is the best method, but it is one that worked for me. First the multi-select attribute must be set to be used in the advanced search. You can set this in the manage attributes area.

$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
$attributeArray=array();
foreach($attributes as $a){
if($a->getAttributeCode() == 'desired_attribute_code'){
foreach($a->getSource()->getAllOptions(false) as $option){
$attributeArray[$option['value']] = $option['label'];
}
}
}

If you only need to retrieve a value for a product you can try this way

//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443
$_product->getResource()->getAttribute('club_type')->getFrontend()->getValue($_product)

I added this code snip it below to possibly answer a question that was posted. The code below will get an attribure collection. Set {entityType} to 4 to get attributes for products. You can remove the “setCodeFilters” line if you want to get all the attributes. To get anything really useful you will probably need to get the resulting attribute ids and do someting with them, like use them in a filter for the products collection or something.

//
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter({entityType})
->setCodeFilter($attributes)
->addSetInfo()
->getData();

Entity Type Id’s
$entityType is an integer id for what type of entity the attribute is associated to. If you look at the “eav_attribute” table you will see that each attribute has an entity_type_id.
1 = Customer Entity
2 = Shipping Entity (I believe)
3 = Category Entity
4 = Product Entity

The Following code was added to answer a question in the replies:
To make a product attribute avaliable when getting a product collection(such as on a category page or search results page) you can add some code the a config.xml file that instructs Magento to load allways the attribute when a product collection is loaded.

How to add an  attribute to be loaded whenever a product collection is loaded:
This can go in any config.xml file. I would recommend putting it in a custom module rather than one from the core code. Just replace “attribute_name” with the attribute code of the attribute you are trying to add.

<frontend>
<product>
<collection>
<attributes>
<attribute_name/>
</attributes>
</collection>
</product>
</frontend>

Tags:
Posted in magento | 32 Comments »

Magento: error message – Notice: Undefined index: 0 app/code/core/Mage/Core/Model/Mysql4/Config.php on line 92

- Tuesday, March 17th, 2009 -

The Quick resolution to this problem it to move the database command line, and not use phpmyadmin. If you just need to fix your database keep reading.

I was moving a installation to a production server and was getting this error message: Notice: Undefined index: 0 in app/code/core/Mage/Core/Model/Mysql4/Config.php on line 92. It took alot of searching to figure out the problem, but here is what I found. When Magento installs it sets store and website ids in the database. When transferring the database the new database did not like having an id of 0 for the admin part of the site, so it made it have an id of 2. This is what my problem was. To fix it I went into the core_store and core_website Tables and gave the admin site an id of 0. It looks like this may only affect certain versions of magento(Earlier versions). Hope this helps someone else.

P.S. I also got foreign_key_constraint errors when importing my database. To solve this disable foreign key check when you export your database. Then when you need to change the core_store and core_website id’s you will probably need to surround your sql with the following code:

set foreign_key_checks=0;
//SQL to update ids
SET FOREIGN_KEY_CHECKS=1;

Tags: , ,
Posted in magento | 35 Comments »

Magento: Adding a static block to a view file (AKA: phtml file)

- Wednesday, March 11th, 2009 -

In magetnto you can add static blocks that you have created to pages using the layout xml files and adding code there. There is plenty of documentation on this method, but what if you want to call the static block from a view (view files generally end in .phtml). Well this is how I have been able to do it.

Just call

<?php
     echo  $this->getLayout()->createBlock('cms/block')->setBlockId('contacts_text')->toHtml() ;
?>

or

<?php
 echo $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/list/related.phtml')->toHtml() ;
?>

Where setBlockId is the identifier you set up in the static block.

Tags:
Posted in magento | 15 Comments »

Magento: Error – SimpleXMLElement::addAttribute() Attribute already exists

- Tuesday, March 10th, 2009 -

Warning: SimpleXMLElement::addAttribute() [simplexmlelement.addattribute]: Attribute already exists.

This Magento Error Message was giving me the hardest time. It seems to occur when you have a mis-match in your layout files.

What happend to me was I hard coded the newsletter into a page. In the newsletter layour file It was instructed to put the newsletter in the page also. This meant that it was included in the page more than once. That is why the error is thrown. Check your layout files to get a better grip on the problem.

I need to give thanks to Branko Ajzele for his post. It helped me realize what the problem was.

Tags: ,
Posted in magento | 3 Comments »

Magento: Adding Image Uploads to TinyMCE

- Thursday, February 19th, 2009 -

If you are using Magento and want to add a WYSIWYG Editor I recomend the Fontis Plugin (Fontis WYSIWYG Editor).  This editor allows you to use TinyMCE or FCKeditor.  The FCKeditor had upload built in. If you want to use TinyMCE Keep reading.

First download ibrowser, this tutorial was written using 1.3.8. (Download Ibrowser)

Install the files to your tinyMCE plugins directory. Mine was: /js/fontis/tiny_mce/plugins/ibrowser.

Open the config.inc.php (tiny_mce\plugins\iBrowser\config\config.ini.php) and set your image path. I set it to: /media/uploads/. Be sure that the file path exists. I like to use dynamic folders so I uncomment: “$cfg['ilibs_inc']” and enter the base path for my folders in “$cfg['ilibs_dir']“.

Next copy the tinyMCE.editor_plugin.js file to the ibrowser plugin directory. This file is located here (ibrowser/ingerface/tinyMCE.editor_plugin.js). Rename the editor_plugin.js file ( in \tiny_mce\plugins\iBrowser) to editor_plugin_src.js. Now rename the tinyMCE.editor_plugin.js (in \tiny_mce\plugins\iBrowser\interface) to editor_plugin.js and move to \tiny_mce\plugins\iBrowser.

*Note: Depending on the version of tinyMCE you may need to use one of the other files labled with tinyMCE in the tiny_mce\plugins\ibrowser\interface directory.

Lastly  you need to add code to the javascript function that created the editor. My file was located in /app/design/adminhtml/default/default/template/fontis/wysiwyg/wysiwyg.phtml.

Find This javascript code: (yours could look slightly different)

tinyMCE.init({
mode : "exact",
elements : editable_areas,
theme : "advanced",
strict_loading_mode : true,
width: "640",
height: "400",
content_css: "<?php echo $this->getSkinUrl() ?>tinymce_content.css",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
relative_urls : false,
});

Insert the below code just under (theme_advanced_statusbar_location : “bottom”,).

plugins : "ibrowser",
theme_advanced_buttons3_add : "ibrowser",

Tags: , ,
Posted in PHP, magento | 16 Comments »

Magento – Store locator with Google Maps

- Saturday, February 14th, 2009 -

How to make google maps show up in a store locator in Magento:

These instructions were created using Magento 1.2.1 and the store locator plugin v1.3 created by ellisoj.

The first step is to install the store locator plugin.  Get the store locator extension key form the link above and use magento connect to install the plugin. Next you need to apply for a google maps API key. If you are installing this on a local installation of magento and your are using a host name like “localMagento ” here are a few notes:
1.  As of Magento 1.2.1 it appears that you need to use a name like “localMagento.com”.
2. Your local build will need it’s  own google api key.
You will need to place the google maps api key that you recieved into the Data.php file, located in the app/code/local/Eroi/Locator/Helper folder.

Below are the code updates to incorperate google maps. The main changes I made were to add the google maps javascript code, the html container for the map, and create the xml code for use by the google map javascript functions. I also change the unit of distance from Km to Miles. This is just a basic map. Check out the google api for more cool things to do with the map.

First add the javascript to the top of the page.

<script src="http://maps.google.com/maps?file=api&v=2&key=<?php echo $this->helper('locator')->getGoogleAPIKey();?>" type="text/javascript"></script>

<script type="text/javascript">
//<![CDATA[
var map;
var geocoder;

function load() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
map = new GMap2($('map'));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(40, -100), 4);
}
}

function showLocations(data){
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName('marker');
map.clearOverlays();

var sidebar = $('sidebar');
sidebar.innerHTML = '';
if (markers.length == 0) {
sidebar.innerHTML = 'No results found.';
map.setCenter(new GLatLng(40, -100), 4);
return;
}

var bounds = new GLatLngBounds();
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute('name');
var address = markers[i].getAttribute('address');
var distance = parseFloat(markers[i].getAttribute('distance'));
var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
parseFloat(markers[i].getAttribute('lng')));

var marker = createMarker(point, name, address);
map.addOverlay(marker);
var sidebarEntry = createSidebarEntry(marker, name, address, distance);
sidebar.appendChild(sidebarEntry);
bounds.extend(point);
}
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}

function createMarker(point, name, address) {
var marker = new GMarker(point);
var html = '<b>' + name + '</b> <br>' + address;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}

function createSidebarEntry(marker, name,  address, distance) {
var div = document.createElement('div');
var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ')<br>' + address;
div.innerHTML = html;
div.style.cursor = 'pointer';
div.style.marginBottom = '5px';
GEvent.addDomListener(div, 'click', function() {
GEvent.trigger(marker, 'click');
});
GEvent.addDomListener(div, 'mouseover', function() {
div.style.backgroundColor = '#eee';
});
GEvent.addDomListener(div, 'mouseout', function() {
div.style.backgroundColor = '#fff';
});
return div;
}
//]]>

Add this html just below the closing form tag.
Div containers for the map and side bar.

<div style="width:615px;font-family:Arial, sans-serif;font-size:11px;border:1px solid black">
<div id="map" style="width:615px;height:400px"></div>
</div>
<div id="sidebar"></div>

In the project I used this on we were only allowing search by zip code. So my code looked like this:
Replace

while ($row = $readresult->fetch())
{
$locationID = $row['locator_id'];
$locator = Mage::getModel('locator/locator')->load($locationID);
echo "n  <li><h3>" . $locator->getName() . "</h3> " . $this->__('distance') . ": " .                      number_format($row['distance'], 1) . " kilometers";
echo "n<br />" . $locator->getAddress()
. "<br />" . $locator->getCity() . ", " . $locator->getState()
. " " . $locator->getPostalCode() . "<br />" . $locator->getPhone() . "</li>";
}

echo '</ul>';

With

//Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

//header("Content-type: text/xml");
//Iterate through the rows, adding XML nodes for each
while ($row= $readresult->fetch()){
$locationID = $row['locator_id'];
$marker = Mage::getModel('locator/locator')->load($locationID);
//
$node = $dom->createElement("marker");
$newNode = $parnode->appendChild($node);
$newNode->setAttribute("name", $marker->getName());
$newNode->setAttribute("address", $marker->getAddress());
$newNode->setAttribute("lat", $marker->getLatitude());
$newNode->setAttribute("lng", $marker->getLongitude());
$newNode->setAttribute("distance", $row['distance']);
}
//remove xml tag and trim white spaces
$jsXmlLocations = substr($dom->saveXML(),21);
$jsXmlLocations = trim($jsXmlLocations);
?>

Put this Javascript code just below the php code above.

load();
showLocations();

Now just echo “addslashes(jsXmlLocations)” inside of the showLocations() function you just inserted. This is how the Google javascript functions at the top of the page get the data for the locations on the map. NOTE: This will only work on searching by zip code. You should be able to use simmilar code to get the search by state working with the maps.

If I have missed something let me know.

Need help with a Magento site. Let us help.

Tags: , , ,
Posted in magento | 7 Comments »

Magento 1.2.1 – Can’t Login Error

- Tuesday, February 10th, 2009 -

It seems that things have changed in magento for the 1.2.1 version. I tried to set-up this version locally using a host name like ‘localmagento’ and prior to this version this worked just fine. Now it seems that magento 1.2.1 needs/wants a full domain name. If you set it up like this (localmagento.com) it should work just fine.

- Intrested in a magento e-commerce site? We can do that. Drop us a line.

Tags: ,
Posted in magento | 1 Comment »

Adding Lightbox to Magento

- Saturday, November 15th, 2008 -

If you want to add Lightbox to a Magento Install, begin with the following instructions (  http://www.magentocommerce.com/wiki/adding_lightbox_to_magento_v2 ).

I was using the modern theme and had to modify the instructions as follows:

Go to /js/

Create a directory: /lightbox

and copy the lightbox.js file to this directory

The “/js/” directory is the top most “JS”directory of the magento install. It did not work if you put the lightbox.js file in the /skin/frontend/default/default/js/ or /skin/frontend/{your}/{path}/js/lightbox

Tags: ,
Posted in magento | No Comments »