`FK_REWARD_CUSTOMER_ID` FOREIGN KEY Error in Magento

After some searching I found the answer to this problem on the Magento Commerce Forums here: http://goo.gl/FA42o

In Magento Professional or Magento Enterprise you may encounter this error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`enterprise_reward`, CONSTRAINT `FK_REWARD_CUSTOMER_ID` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE)

Here is the fix.

Edit app/code/core/Enterprise/Reward/Model/Observer.php

Replace the following code:

protected function _revertRewardPointsForOrder(Mage_Sales_Model_Order $order)
{
Mage::getModel(‘enterprise_reward/reward’)
->setCustomerId($order->getCustomerId())
->setWebsiteId(Mage::app()->getStore($order->getStoreId())->getWebsiteId())
->setPointsDelta($order->getRewardPointsBalance())
->setAction(Enterprise_Reward_Model_Reward::REWARD_ACTION_REVERT)
->setActionEntity($order)
->updateRewardPoints();

return $this;
}

With this:

protected function _revertRewardPointsForOrder(Mage_Sales_Model_Order $order)
{
        if (!$order->getCustomer()->getId()) {
            return $this;
        }

Mage::getModel(‘enterprise_reward/reward’)
->setCustomerId($order->getCustomerId())
->setWebsiteId(Mage::app()->getStore($order->getStoreId())->getWebsiteId())
->setPointsDelta($order->getRewardPointsBalance())
->setAction(Enterprise_Reward_Model_Reward::REWARD_ACTION_REVERT)
->setActionEntity($order)
->updateRewardPoints();

return $this;
}

Fix your Windows install after a trojan

There’s a particularly nasty and annoying trojan going around.  It’s the old “Anti-virus” fake out but seems to have found news ways to get in.  The trojan claims that your system has a bunch of problems and you need to buy their software to fix it.  It will pop up every couple of days to nag you and prevent you from running things like regedit or msconfig.

Despite being paranoid and very careful I managed to pick this bug up.  I installed AVG Internet Security 2012 and it fixed the infection but I still had some of the lingering damage that the program caused.  If this has happened to you then I highly recommend checking out Tweaking.com’s All-In-One Windows Repair. You can get it here for free: http://www.tweaking.com/content/page/windows_repair_all_in_one.html.

They recommend the follow anti-virus and rootkit utilities as well:

http://support.kaspersky.com/faq/?qid=208280684
http://www.malwarebytes.org/
http://www.avast.com/en-us/index
http://majorgeeks.com/Combofix_d6402.html

My firewall and BFE wouldn’t start after cleaning this infection.  The information here fixed it: http://www.sevenforums.com/system-security/202166-unable-start-windows-firewall-2.html

Hope this helps someone!

P.S.  You can always just upgrade to Ubuntu 12.04 🙂

Make all categories “anchor” in Magento

In Magento, an Anchor category allows for the display of the Layered Navigation.  This is the column on the left that displays the “Shop By” attributes to help narrow down browsing results when shopping.

Usually I like for all of the categories to be set as Anchors but sometimes clients will forget to set this when entering their product data.  Instead of having to click through each category individually the folks at overlycaffeinated.com posted this handy fix.

I made a minor tweak that works better for me.  I’ve used this code on Community, Professional and Enterprise Magento installations successfully.

Create a file called anchor.php and place the following code in it.  You can run it from the browser or CLI.  Be sure to change the path definition YOUR_MAGENTO_PATH_HERE to your Magento install location.

<?php
error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);

# Path to your Magento installation
define(‘MAGENTO’, realpath(‘YOUR_MAGENTO_PATH_HERE’));

require_once(MAGENTO . ‘/app/Mage.php’);

$app = Mage::app();

$categories = Mage::getModel(‘catalog/category’)
 ->getCollection()
 ->addAttributeToSelect(‘*’)
# ->addAttributeToFilter(‘is_anchor’, 0)  # I changed this from the original author’s code because it was skipping categories.  It works great without this line here.
 ->addAttributeToFilter(‘entity_id’, array(“gt” => 1))
 ->setOrder(‘entity_id’);

foreach($categories as $category) {
     echo $category->getId() . “\t &nbsp;&nbsp;” . $category->getName() . “<br />\n”;
     $category->setIsAnchor(1);
     $category->save();
}

?>

Get cart contents outside of Magento using external PHP

It’s possible to get the cart contents in an external PHP file in Magento with the following code:

<?php

require(“app/Mage.php”);
umask(0);
Mage::app();

$cart_contents = Mage::helper(‘checkout’)->getQuote();

foreach ($cart_contents->getItemsCollection() as $item) {
   echo $item->getName() . “<br />”;
   echo $item->getQty() . “<br />”;
   $price = $item->getPrice();
   echo $price . “<br />”;
   $qty = $item->getQty();
   $total = $qty * $price;
   echo $total;
}

?>

You can use this code inside of Magento’s phtml files this way:

<?php

$cart_contents = Mage::helper(‘checkout’)->getQuote();

foreach ($cart_contents->getItemsCollection() as $item) {
   echo $item->getName() . “<br />”;
   echo $item->getQty() . “<br />”;
   $price = $item->getPrice();
   echo $price . “<br />”;
   $qty = $item->getQty();
   $total = $qty * $price;
   echo $total;
}

?>

Changing Magento’s default logos

Magento has a lot of customization available through the Admin interface under Admin > System > Configuration > Design.  From there you can set the following items:

Favicon Icon: This is the small 16×16 icon that displays in the next to the site url in the address bar in most browsers.  A good place to find and customize favicons is at http://www.favicon.cc.

Default Title:  This is the main default page title.

Title Prefix:  This field adds static text to the top of every page.  If you put “Test” in this field then a product called “Product Name” would have “Test – Product Name” as the page title.

Title Suffix: Same as “Title Prefix” above but this places the static text after the page title.

Default Description, Default Keywords:  This is the default description and keywords respectively in your Meta Tags.  You can also specify this per-product.

Default Robots: Define your default robots directives here.

Miscellaneous Scripts:  You can place javascripts here (tracking codes, analytics, etc).  These will run before the closing tag on every page.

Display Demo Store Notice:  If you set this to “yes” then a blurb will display on every page indicating that the site is a demo.

You should also update the print logo by going to System > Sales > Invoice and Packing Slip Design.  This logo is used whenever anything is printed or when a PDF is generated.

There are some things that aren’t controlled by the admin yet.  The default logos should be customized by uploading them to the site’s file structure.

You should replace the logos in the following locations:

Main site logo
/skin/frontend/YOUR_PACKAGE_NAME/YOUR_THEME_NAME/images/logo.gif

E-Mail site logo
/skin/frontend/YOUR_PACKAGE_NAME/YOUR_THEME_NAME/images/logo_email.gif

Display the full error message in your web browser in Magento

By default you will only see the report number when an error message is displayed in Magento.  To enable the display of the full error message:

1. Go to the /errors/ folder in your Magento root.

2. Rename local.xml.sample to local.xml

Your error messages will now display in full in the browser.  This is disabled by default for security reasons and should be disabled again by renaming the local.xml to local.xml.sample when you make your site live.