Create configuration for your Magento extension

Share
Posted on May 30th, 2011 | Posted by admin

Today I’m going to give you and quick HOW-TO for beginners on how to create simple configuration for your extension inside Magento’s configuration section.
If you’re interested, read on:

Well, fist you need an extension that you’re going to build configuration for. If you’re new to Magento and you don’t know how to create an extension of your own, there’s a bunch of articles on this site about that, but my suggestion is to start on fresh extension, for testing..

Adding related products problem

Share
Posted on May 30th, 2011 | Posted by admin

Today I had a problem when I want to add related product to Magento Professional 1.9.0.0., it did not work. After some tracing thrown errors and comparing files with other Magento versions I found missing part. Magento Professional has missing lines of code in Related.php. So, if someone stuck on the same problem, here is the solution!

To correct this issue, do it in Magento way. So create your new module and rewrite that block. Here is the file where you can find these lines of code:
\app\code\core\Mage\Adminhtml\Block\Catalog\Product\Edit\Tab\Related.php

Magento code library – Handling files and folders

Share
Posted on May 30th, 2011 | Posted by admin

Among most often custom coding tasks are usually those related to some import/export between Magento and other system. Usually existing store owners when switching from their current shop system to Magento want to transfer all of their products, orders, and most importantly customers.

Most of the time, standard Magento product /customer import/export is not sufficient for the task at hand. Limitations like file size upload and structure of original data require you to do a custom scripts that will handle the job of data transfer. Think of shops with several tens of thousands of orders, customers, products.

Luckily Magento comes with several nice classes that handle files and directory manipulation that might help you greatly. Surely all of this can be done with plain functional PHP, but hey why not impress your boss right?!

So, in the example below, I will show you how you can create a folder, fetch data via some external SFTP, parse/import, then parse/export it and finally push the data to some external SFTP.

First we will define some folder/file names on top of the script like shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$baseDir = Mage::getBaseDir();
$varDir = $baseDir.DS.'var';
$timeOfImport = date('jmY_his');
$importReadyDir = $varDir.DS.'import_ready'.DS.$timeOfImport;
$exportReadyDir = $varDir.DS.'export_ready'.DS.$timeOfImport;
$_fileToImportRemote = '/home/ajzele/customers.txt';
$_fileToExportRemote = '/home/ajzele/customers_export.txt';
$_fileToImportBaseName = 'customers.txt';
$_fileToImportLocal = $importReadyDir.DS.'customers.txt';
$_fileToExportLocal = $exportReadyDir.DS.'customers_parsed.txt';

After this, lets separate our process to few steps. First we will grab the files from some SFTP and dump them locally to /var/import_ready/{{date(‘jmY_his’)}}/ folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$file = new Varien_Io_File();
//Create import_ready folder
$importReadyDirResult = $file->mkdir($importReadyDir);
if (!$importReadyDirResult) {
//Handle error
}
$sftpPickupFile = new Varien_Io_Sftp();
try {
$sftpPickupFile->open(
array(
'host' => 'some.server.com',
'username' => 'ajzele',
'password' => 'MyPass',
'timeout' => '10'
)
);
$_fileToImportRemoteTmp = $sftpPickupFile->read($_fileToImportRemote);
if(!$_fileToImportRemoteTmp) {
//Handle error
}
$sftpPickupFile->close();
if (!$file->write($_fileToImportLocal, $_fileToImportRemoteTmp)) {
//Handle error
}
} catch (Exception $e) {
//Handle error
}

Please note that the examples above are for ideal scenario with no errors in the process. Surely you would need more robust code to handle error scenarios.

At this point you should have your data pulled from SFTP and store locally in /var/import_ready/{{date(‘jmY_his’)}}/ folder. Where {{date(‘jmY_his’)}} will give you the exact date, hour, minute for folder name.

So, next step might be exporint some data to remote SFTP server? If so, here is a little example that demostrates how.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$flocal = new Varien_Io_File();
$flocal->open(array('path' => $importReadyDir));
$flocal->streamOpen('customers.txt', 'r');
while (false !== ($csvLine = $flocal->streamReadCsv())) {
//Parse the data and import it...
//Zend_Debug::dump($csvLine, '$csvLine');
/**
$csvLine array(4) {
[0] => string(13) "Branko Ajzele"
[1] => string(16) "ajzele@email.com"
[2] => string(2) "28"
[3] => string(25) "Sample address for Ajzele"
}
*/
}
//Now we do reverse, grab some data from Magento and upload it to SFTP
$data = '"Branko2 Ajzele", "ajzele@email.com", "28", "Sample address for Ajzele"
"Tomas2 Novoselic", "tomas.novoselic@email.com", "29", "Sample address for Tomas"';
$sftpDumpFile = new Varien_Io_Sftp();
try {
$sftpDumpFile->open(
array(
'host' => 'some.server.com',
'username' => 'ajzele',
'password' => 'MyPass',
'timeout' => '10'
)
);
//Make a local backup that will be send to SFTP
$file->mkdir($exportReadyDir);
$file->write($_fileToExportLocal, $data);
//Upload to SFTP
$_fileToExportRemoteTmp = $sftpDumpFile->write($_fileToExportRemote, $data);
} catch (Exception $e) {
echo $e->getMessage();
}

Enough with the examples. To conclude the things. There are some pretty decent classes in Varien library under Magento that enable you file and directory manipulation.

All you need to do sometimes is just poke around the existing library.

How To Setup A Cron Job In Magento – Magento Cron Tutorial

Share
Posted on May 26th, 2011 | Posted by admin

magento extensions

A CRON is a daemon which keeps running and gets activated at a said interval (time period). Cron helps store owners to setup up and schedule commands which will run periodically at set time and date. Various Magento services such as “Sending Newsletter “, Cleanup with logs, Cleanup cache, Customer notification etc.  requires a mechanism where store owners can setup and schedule commands to run periodically.

Most of the web hosts have their own set of task scheduler; you can ask and verify the task scheduler from your webhost. While setting a cron-job make sure that minimum time duration is set to run every 15 minutes (not less than this) as  Magento spends around 1 minute to index 1000 products and sets the status to “Success” from “Pending”.

How To Integrate Mailchimp Into Your Magento Store

Share
Posted on May 26th, 2011 | Posted by admin

Are you looking to integrate Mailchimp email subscription within your Magento powered store? If yes then you are at right place. There is a sudden rise in Magento stores but some store owners are disappointed with Magento’s default email subscription and newsletter handling capabilities. Basically they face the following problems:

  1. They can not send the newsletter right away because their local time is not the same as of server time. Eventually, their newsletter campaign will be sent few hours later than the intended time. Although, this can be adjusted with a little tweak.
  2. They can not send large number of emails in one go. Magento limits how many emails should be sent at once.
  3. Managing CRON is a pain.
Page 1 of 1212345»10...Last »