Add an image field in your admin plugin (by example AW Blog)

Schermafbeelding 2013-07-03 om 14.28.17

Add an image field to plugins like AW Blog

Edit in /app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

$fieldset->addField('filename', 'file', array(
'label' => Mage::helper('blog')->__('Image'),
'required' => false,
'name' => 'filename',
'after_element_html' => 'Formaat 125 x 118 pixels',
));

Edit in /app/code/community/AW/Blog/controllers/Manage/BlogController.php after:

public function saveAction() {

if ($data = $this->getRequest()->getPost()) {

Add:

if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('filename');

// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);

// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);

// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['filename']['name'] );

} catch (Exception $e) {

}

//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
}

Further add an field ‘filename’ in your db (table ‘aw_blog’).

And finally in /app/code/community/AW/Blog/Block/Blog/Edit/Form.php make sure that $form variable is defined as follows, so it works with multipart/form-data:

$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
)
);

Table rates shipping based on price AFTER DISCOUNT – Magento

Magento has quite a big bug which enables the shipping module to calculate shipping costs without discount or shipping rules (if they are applied).

It cost me hours to find a workaround, so if you use it, I’d really appreciate it if you refer to this blog.

How I fixed it

Go to:

/app/code/local/Mage/Shipping/Model/Carrier/Tablerate.php

Add the following code at around line 77 (in the collectRates function):

$s_price = $request->getPackageValueWithDiscount();
$request->setPackageValue($s_price);

Now, the shipping rates are calculated on the subtotal minus the discount. I hope this solves the problem (it does in 1.7)

Magento import multiple images in dataflow csv/xml

Sometimes it’s really annoying that Magento only supports one image per product. On the Magento Commerce forum a solution is found:

Now, what you need to do is open the file
app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
and go to around line 773 and find

$addedFilesCorrespondence = $mediaGalleryBackendModel->addImagesWithDifferentMediaAttributes(
$product,
$arrayToMassAdd, Mage::getBaseDir('media') . DS . 'import',
false,
false
);

and paste the following code right after it
if (isset($importData['media_gallery']) && !empty($importData['media_gallery'])) {
$x = explode(',', $importData['media_gallery']);
foreach ($x as $file) {
$imagesToAdd[] = array('file' => trim($file));
}
$mediaGalleryBackendModel->addImagesWithDifferentMediaAttributes(
$product,
$imagesToAdd, Mage::getBaseDir('media') . DS . 'import',
false,
false
);
}

Source: http://www.magentocommerce.com/boards/viewthread/224928/P45/

Magento place order slow [FIXED]

The Magento “place order” step can take up to a minute. With thanks to this site I figured out how to fix slow checkouts.

Step 1

Find the file app/code/core/Mage/Rss/etc/config.xml and uncomment/remove:

<sales_order_save_after>
<observers>
<notifystock>
<class>rss/observer</class>
<method>salesOrderItemSaveAfterNotifyStock</method>
</notifystock>
</observers>
</sales_order_save_after>
<sales_order_save_after>
<observers>
<ordernew>
<class>rss/observer</class>
<method>salesOrderItemSaveAfterOrderNew</method>
</ordernew>
</observers>
</sales_order_save_after>

Step 2

Find the file app/code/core/Mage/Downloadable/etc/config.xml and uncomment/remove:

<!–sales_order_item_save_commit_after>
<observers>
<downloadable_observer>
<class>downloadable/observer</class>
<method>saveDownloadableOrderItem</method>
</downloadable_observer>
</observers>
</sales_order_item_save_commit_after–>

Magento upgrade to 1.7

Upgrading Magento Commerce 1.4, 1.5 and 1.6 to 1.7

Magento is a pretty complex program that hasn’t an easy upgrading solution. Because following the standard guides gives quite a lot of errors, I wrote my own upgrade guide with thanks to Duntuk.com and Turnkeye.

For versions lower than 1.4.2, you’d first have to follow the steps from Turnkeye for lower versions.

Pre-upgrading steps

  • MAKE A BACKUP!
  • Install a fresh install of Magento on a subdirectory or other host on a new database (don’t overwrite your own version of course).
  • With the db repair tool from Magento you can compare your own database versus the fresh database you just installed. The db repair tool looks for errors in the database and can upgrade them.
  • Open SSH and remove your cache:
    rm -rf var/cache/*
    rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*

Upgrading steps

  • Chmod the mage file: $ chmod 550 ./mage
  • Upgrade the site (according to Duntuk and Turnkeye):$ ./mage mage-setup .
    $ ./mage sync --force
    $ ./mage list-upgrades #(in order to see if there are upgrades)
    $ ./mage config-set preferred_state stable
  • Start the upgrade
    $ ./mage upgrade-all --force
  • If the upgrade is finished you'll see rows like:
    Package upgraded: community/Mage_Locale_en_US 1.7.0.2
  • Remove cache and sessions
    $ rm -rf var/cache/* var/session/*
    $ rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
  • Visit the homepage which will trigger the upgrade process. It can take up to 30 minutes. By doing it by SSH safes memory:
    $ php -f index.php
  • Sometimes it’s safer to trigger the index.php multiple times until it gives a blank return
  • Than visit the admin and make sure the version number is the number of the upgraded version

Aftercare

Reindex the website if the upgrade is succesful:

$ php shell/indexer.php reindexall

Set all the permissions back:

$ find . -type f -exec chmod 644 {} \;
$ find . -type d -exec chmod 777 {} \;

$ chmod 755 mage
$ chmod o+w var var/.htaccess app/etc
$ chmod -R o+w media
$ chmod -R 777 var/package var/locks var/report var/export downloader

Hopefully your site is upgraded now! If you’ve any questions respond below!

Issues

Error: Mage registry key “_resource_helper/core” already exists

rm -rf var/session/*
rm -rf var/cache/*

If that doesn’t work restart caching tools like Memcached!

sqlstate 23000 integrity constraint violation 1048 column ‘value’ cannot be null magento

How to solve the problem ‘sqlstate 23000 integrity constraint violation 1048 column ‘value’ cannot be null magento’?

This problem is quite hard to identificate, because it’s hard to seek the right column ‘value’. Here is what I did in order to find out the problem.

  • Start the import process and see on which record number the errors occur.
  • Locate these record numbers and find the difference between other records in a spreadsheet program.
  • In my example I found out that on some rows the meta_robots column was empty instead of INDEX, FOLLOW. This caused the error.
  • Fill these rows. This will solve the problem.

BTW verhoging 21 procent doorvoeren in Magento (dutch)

Het kan de meeste mensen inmiddels niet ontgaan zijn, per 1 oktober 2012 zal er een BTW verhoging ingaan van 19 naar 21 procent. Vervelend daarbij is dat er nogal wat onduidelijkheden zijn. Aangezien ik zelf verstand heb van het webwinkel pakket Magento, probeer ik in dit artikel alle onduidelijkheden omtrent het nieuwe BTW tarief de wereld uit te helpen. U kunt daarbij zelf kiezen of u de BTW in de prijs door wilt voeren liever niet.

Stap 1: BTW tarief verhogen

Dit kunt u doen bij Verkopen > BTW > Beheer Belastingzones en Tarieven

Stap 2a: BTW in de prijs door laten voeren (prijsverhoging)

Hier komt een technisch verhaal bij kijken. De bedoeling is dat de catalogusprijs met de stijging van het btw-niveau automatisch meegaat. Uitgerekend is dit een prijsstijging van 1,68% (aan de hand van (1/119)*121*100% ). De bedoeling is dus om een negatieve korting te geven op alle producten.

Magento accepteert echter standaard geen negatieve kortingen:

Magento Negatieve Korting

 

 

Conform het volgende artikel moeten we dus een paar wijzigingen doorvoeren in de kern:

  1. Ga naar: app/code/core/Mage/Rule/Model/Rule.php en vind:
    //check if discount amount > 0
    if ((int)$this->getDiscountAmount() < 0) { 3 Mage::throwException(Mage::helper(‘rule’)->__(‘Invalid discount amount.’));
    }
  2. Vervang deze code door deze uit te 'commenten':
    // if ((int)$this->getDiscountAmount() < 0) { 3 //Mage::throwException(Mage::helper(‘rule’)->__(‘Invalid discount //amount.’));
    //}
  3. Ga nu naar: app/code/core/Mage/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions.php en vind: 'class' => 'validate-not-negative-number',
  4. ‘Comment’ deze code ook uit: // 'class' => 'validate-not-negative-number',
  5. Herhaal stap 3 en 4 voor het bestand app/code/core/Mage/Adminhtml/Block/Promo/Catalog/Edit/Tab/Actions.php
  6. Verander in het bestand app/code/local/Mage/CatalogRule/Model/Observer.php :
    $finalPrice = min($product->getData(‘final_price’), $this->_rulePrices[$key]);
    in:
    $finalPrice = $this->_rulePrices[$key];
  7. Vervolgens kunt u deze bestanden uploaden (het veiligste is om de bestanden niet in app/code/core, maar app/code/local op te slaan).
Vervolgens kunt u de negatieve korting doorvoeren.
Ga naar promotie > catalogus prijsregels > nieuwe regel aanmaken. Geef de prijsregel een naam en voer bij actie het tarief van -1,68 procent in. Daarbij kunt u een vanaf datum van 1 oktober 2012 instellen. Vervolgens kunt u de wijzigingen opslaan en klikt u op ‘apply rules’.
Het nadeel van een automatische stijging is wel dat een ‘mooie’ prijs als €49,95 zal vanderen in €55,21. Mocht u later dus alle prijzen handmatig wijzigen, kunt u de zojuist ingevoerde prijsregel weer uitzetten.

Stap 2b: BTW niet in de prijs door laten voeren (gelijke prijs)

Als het goed is, is hier verder geen wijziging voor nodig.

Extra fee for Shopping Cart AND Catalog Cart Price Rules in Magento – Negative discount

This article is incomplete and the solution might not work!

Ever wondered wether it would be possible creating a negative discount? This article describes how to create a negative shopping cart price rule.

By default, Magento doesn’t accept negative price rule discount values:

Magento Negatieve Korting

We have to add some changes through the core in Magento conform the article:

  1. Go to: app/code/core/Mage/Rule/Model/Rule.php and fin:
    //check if discount amount > 0
    if ((int)$this->getDiscountAmount() < 0) { 3 Mage::throwException(Mage::helper(‘rule’)->__(‘Invalid discount amount.’));
    }
  2. Replace this code, by 'commenting':
    // if ((int)$this->getDiscountAmount() < 0) { 3 //Mage::throwException(Mage::helper(‘rule’)->__(‘Invalid discount //amount.’));
    //}
  3. Go to: app/code/core/Mage/Adminhtml/Block/Promo/Quote/Edit/Tab and find: 'class' => 'validate-not-negative-number',
  4. ‘Comment’ this code as well: // 'class' => 'validate-not-negative-number',
Now it works for shopping cart price rules. Unfortunately the article doesn’t describe how to enable negative values for catalog price rules. We have the solution:
  1. Repeat step 3 and 4 for the file app/code/core/Mage/Adminhtml/Block/Promo/Catalog/Edit/Tab/Action.php
  2. Then you can upload these files, I’ll prefer to save it in the app/code/local directory instead of core.

Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

When having trouble with the error: “Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction” or when your place order / order confirmation gets stuck.

Fixing the order process getting stuck:

According to the Magento Forum, in MySQL, try:

TRUNCATE `log_customer`;
TRUNCATE `log_quote`;
TRUNCATE `log_summary`;
TRUNCATE `log_summary_type`;
TRUNCATE `log_url`;
TRUNCATE `log_url_info`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;

Of course create a backup first. Doesn’t this fix the problem or do you have a better solution? Comment it!