magento

warning: Creating default object from empty value in /data/web/1/000/027/003/273448/htdocs/panticz.de/modules/taxonomy/taxonomy.pages.inc on line 33.

Magento Cache

get all cache types
Mage::app()->getCacheInstance()->getTypes();

types
Mage::app()->getCacheInstance()->cleanType('config');
Mage::app()->getCacheInstance()->cleanType('layout');
Mage::app()->getCacheInstance()->cleanType('block_html');
Mage::app()->getCacheInstance()->cleanType('translate');
Mage::app()->getCacheInstance()->cleanType('collections');
Mage::app()->getCacheInstance()->cleanType('eav');
Mage::app()->getCacheInstance()->cleanType('config_api');

clear cache
Mage::app()->cleanCache();

Magento: Update free shipping

PHP script (/root/scripts/set_free_shipping.sh)

#!/usr/bin/php
<?php
if(count($_SERVER['argv']) > 1) {
    $value=$_SERVER['argv'][1];
    
    require '/var/www/app/Mage.php';
    Mage::app(0);

    $config = new Mage_Core_Model_Config();
    $config->saveConfig('carriers/flatrate/active', $value, 'default', 0);
    $config->saveConfig('carriers/matrixrate/active', !$value, 'default', 0);

    Mage::app()->getCacheInstance()->cleanType('config');
} else {
    echo "Usage: " . $_SERVER['argv'][0] . " 0|1\n";
}
?>

Cronjob (/etc/cron.d/newyear)
0 0 1 1 * root /root/scripts/set_free_shipping.sh 0

Magento: Update footer copyright year

PHP script (/root/scripts/update_copyright_year.php)

#!/usr/bin/php
<?php
require '/var/www/app/Mage.php';
Mage::app(0);
  
# update configuration
$config = new Mage_Core_Model_Config();
$config->saveConfig('design/footer/copyright', '&copy; ' . date("Y") . ' YOUR COMPANY INC.', 'default', 0);

# refresh cache
Mage::app()->getCacheInstance()->cleanType('config');
sleep(1);
Mage::app()->getCacheInstance()->cleanType('block_html');
?>

Cronjob (/etc/cron.d/newyear)
0 0 1 1 * root /root/scripts/update_copyright_year.php

Use in CMS block
{{config path='design/footer/copyright'}}

Dep: Update by SQL
update core_config_data
set value = CONCAT('© ', YEAR(CURDATE()), ' YOUR COMPANY INC.')
where path = 'design/footer/copyright';

Links
http://www.panticz.de/Magento-Cache

Magento: Update custom core config data and refresh cache to view the changes

#!/usr/bin/php
<?php
require '/var/www/app/Mage.php';
Mage::app(0);

if(count($_SERVER['argv']) == 2) {
  $value = $_SERVER['argv'][1];

  # update magento core config data
  $config = new Mage_Core_Model_Config();
  $config->saveConfig('stocklist/number', $value, 'default', 0);

  # refresh magento configuration cache
  Mage::app()->getCacheInstance()->cleanType('config');
} else {
  echo "Usage: " . $_SERVER['argv'][0] . " LIST_ID\n";
}
?>

Links
http://inchoo.net/ecommerce/magento/how-to-programmatically-change-magentos-core-config-data/
http://makandracards.com/magento/8277-cache-clearing-and-general-information

Magento: Add upload field to contact form

wget -q http://dl.panticz.de/magento/contact_attachment/form.phtml.diff -O - | patch -p2 \
/var/www/app/design/frontend/default/default/template/contacts/form.phtml

--- /var/www/app/design/frontend/default/default/template/contacts/form.phtml.org	2013-11-11 17:35:28.000000000 +0100
+++ /var/www/app/design/frontend/default/default/template/contacts/form.phtml	2013-11-12 16:20:06.000000000 +0100
@@ -28,7 +28,7 @@
 <div class="page-title">
     <h1><?php echo Mage::helper('contacts')->__('Contact Us Form') ?></h1>
 </div>
-<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
+<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" enctype="multipart/form-data"> 
     <div class="fieldset">
         <h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
         <ul class="form-list">
@@ -68,6 +68,13 @@
                     <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"><?php echo $this->getRequest()->getParam('message');?></textarea>
                 </div>
             </li>
+            <li>
+                <label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
+                <div class="input-box">
+                    <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
+                    <input name="attachment" id="attachment" class="input-text" type="file" />
+                </div>
+            </li>
         </ul>
     </div>
     <div class="buttons-set">

wget -q http://dl.panticz.de/magento/contact_attachment/IndexController.php.diff -O - | patch -p2 \
/var/www/app/code/core/Mage/Contacts/controllers/IndexController.php

--- /var/www/app/code/core/Mage/Contacts/controllers/IndexController.php.org	2013-11-11 17:44:06.000000000 +0100
+++ /var/www/app/code/core/Mage/Contacts/controllers/IndexController.php	2013-11-13 11:34:56.000000000 +0100
@@ -91,7 +91,40 @@
                 if ($error) {
                     throw new Exception();
                 }
+
+                $fileName = '';
+                if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') {
+                    try {
+                        $fileName       = $_FILES['attachment']['name'];
+                        $fileExt        = strtolower(substr(strrchr($fileName, "."), 1));
+                        $fileNamewoe    = rtrim($fileName, $fileExt);
+                        $fileName       = preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;
+ 
+                        $uploader       = new Varien_File_Uploader('attachment');
+                        $uploader->setAllowedExtensions(array('jpg', 'png', 'bmp', 'gif', 'pdf', 'zip', 'rar'));
+                        $uploader->setAllowRenameFiles(false);
+                        $uploader->setFilesDispersion(false);
+                        $path = Mage::getBaseDir('media') . DS . 'contacts';
+                        if(!is_dir($path)){
+                            mkdir($path, 0777, true);
+                        }
+                        $uploader->save($path . DS, $fileName);
+ 
+                    } catch (Exception $e) {
+                        $error = true;
+                    }
+                }
+
                 $mailTemplate = Mage::getModel('core/email_template');
+                if($fileName != '') {
+                    $attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
+                    if(file_exists($attachmentFilePath)){
+                        $fileContents = file_get_contents($attachmentFilePath);
+                        $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
+                        $attachment->filename = $fileName;
+                    }
+                }
+
                 /* @var $mailTemplate Mage_Core_Model_Email_Template */
                 $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                     ->setReplyTo($post['email'])

wget -q http://dl.panticz.de/magento/contact_attachment/Mage_Contacts.csv.diff -O - | patch -p2 \
/var/www/app/locale/de_DE/Mage_Contacts.csv

--- /var/www/app/locale/de_DE/Mage_Contacts.csv.org	2013-11-13 15:57:34.000000000 +0100
+++ /var/www/app/locale/de_DE/Mage_Contacts.csv	2013-11-13 11:40:11.000000000 +0100
@@ -15,5 +15,6 @@
 "Send Emails To","Sende E-Mails an"
 "Submit","Absenden"
 "Telephone","Telefon"
+"Attachment","Anhang"
 "Unable to submit your request. Please, try again later","Fehler beim Übertragen der Anfrage. Bitte versuchen Sie es später noch einmal."
 "Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.","Ihre Anfrage wurde übertragen. Wir werden diese umgehend bearbeiten und uns gegebenenfalls mit Ihnen in Verbindung setzen. Vielen Dank!"

Links
http://www.blog.magepsycho.com/adding-upload-field-in-contact-form-and-send-as-attachment/
http://www.magentocommerce.com/boards/viewthread/440759/

Magento: Create top category offer box

Create a top category offer box on Magento front page linked to a category and filled with one of selected products from a hidden category.

/var/www/app/design/frontend/default/default/template/catalog/product/top-category.phtml
Download: http://www.panticz.de/sites/default/files/magento/home/top-category.phtml

<?php
$offer = array(
    array('TVs', '/shop/tv.html', 24231),
    array('Computer', '/shop/computer.html', 24238),
    array('Cameras', '/shop/cameras.html', 24245),
    array('RAM', '/shop/ram.html', 24244),
    array('HDD', '/shop/hdd.html', 24243)
);
?>

<div class="category-products">
    <ul class="products-grid">
    <?php for($i = 0; $i < count($offer); $i++): ?>
        <li class="item <?php if($i == 0): ?> first <?php elseif($i - 1 == count($offer)): ?> last <?php endif; ?>">
            <?php $offer_name = $offer[$i][0]; ?>
            <?php $offer_url = $offer[$i][1]; ?>
            <?php $offer_category = $offer[$i][2]; ?>

            <h4><a href="<?php echo $offer_url; ?>"><?php echo $offer_name; ?></a></h4>
            <?php
                $catagory_model = Mage::getModel('catalog/category')->load($offer_category);
                $collection = Mage::getResourceModel('catalog/product_collection');
                $collection->addCategoryFilter($catagory_model);
                $collection->addAttributeToFilter('status', 1);
                $collection->getSelect()->order('rand()');   
                $collection->addStoreFilter();
                $collection->setPageSize(1);
                $collection->setCurPage(1);
            ?>
            <?php if(!empty($collection)): ?>
                <?php foreach ($collection as $_product): ?>
                    <?php $_product = Mage::getModel('catalog/product')->setStoreId($this->store_id)->load($_product->getId()); ?>
                    <?php $_url = $_product->getSku(); ?>
                    <a href="/<?php echo $_url ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135,73); ?>" width="135" height="73" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
                    <h2 class="product-name">
                        <a href="/<?php echo $_url ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
                    </h2>
                <?php endforeach ?>
            <?php endif ?>
        </li>
    <?php endfor ?>
    </ul>
</div>

Magento: Fix 404 URL not found on language change in a multilanguage shop

diff -Naur /tmp/magento/app/code/core/Mage/Core/Model/Url/Rewrite.php /var/www/app/code/core/Mage/Core/Model/Url/Rewrite.php
--- /root/magento/app/code/core/Mage/Core/Model/Url/Rewrite.php	2010-02-20 01:48:33.000000000 +0100
+++ /var/www/app/code/core/Mage/Core/Model/Url/Rewrite.php	2012-11-27 18:28:06.000000000 +0100
@@ -83,6 +83,23 @@
         if (is_array($path)) {
             foreach ($path as $pathInfo) {
                 $this->load($pathInfo, 'request_path');
+
+                if (!$this->getId() && !isset($_GET['___from_store'])) {
+                    $db = Mage::getSingleton('core/resource')->getConnection('default_read');
+                    $result = $db->query('select store_id from core_url_rewrite WHERE request_path = "' . $pathInfo . '"');
+                    if ($result) {
+                        $storeIds = array();
+                        if($row = $result->fetch(PDO::FETCH_ASSOC)) {
+                            $storeId = $row['store_id'];
+                            $storeCode = Mage::app()->getStore($storeId)->getCode();
+
+                            header("HTTP/1.1 301 Moved Permanently");
+                            header("Location: http://" . $_SERVER['HTTP_HOST'] . "/" . $pathInfo . "?___store=" . $storeCode);
+                            exit();
+                        }
+                    }
+                }

Magento: Create product image sitemap

<?php
#
# include
#
require_once '/var/www/app/Mage.php';
Mage::app(0);


#
# functions
#
function print_head() {
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    echo "\n";
}

function print_url($url, $date) {
    echo "\n";
    echo "    $url\n";
    echo "    $date\n";
    echo "    monthly\n";
    echo "    0.3\n";
    echo "\n";
}

function print_foot() {
    echo "\n";
}


#
# main
#
try {
    print_head();

    $collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToFilter('image', array('notnull' => '', 'neq' => 'no_selection'))
        ->setStoreId(1);

    foreach($collection as $product) {
        // get last product update
        $updatedAt = substr($product->getUpdatedAt(), 0, 10);

        // get main image
        $image = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getImage());
        print_url($image, $updatedAt);

        // get additional images
        $gallery = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();
        foreach ($gallery as $image) {
            print_url($image->getUrl(), $updatedAt);
        }
    }

    print_foot();
} catch (Exception $e) {
    Mage::printException($e);
}
?>
1

View featured products on top from product listing

<?php
require '/var/www/app/Mage.php';
Mage::app(0);

# remove script name from array
$items = array_slice($argv, 1); 

# set featured product
Mage::getConfig()->saveConfig('catalog/featured/product', implode(",", $items));
?>

php -f set_featured_product.php sku1 sku2 sku3

clear magento cache

<?php
require '/var/www/app/Mage.php';
Mage::app(0);

function updateProduct($product) {
    try {
        if(!empty($product)) {
            $write = Mage::getSingleton('core/resource')->getConnection('core_write');
            $write->query("
                UPDATE magento.catalog_product_entity
                SET updated_at = now() + INTERVAL 1 year
                WHERE catalog_product_entity.sku = '". $product . "';
            "); 
        }
    } catch (Exception $e) {
        Mage::printException($e);
    }
}

$products = explode(',', Mage::getStoreConfig('catalog/featured/product'));
$products = array_reverse($products);
foreach($products as $product) {
    updateProduct($product);
    sleep(1);
}
?>

php -f update_featured_product.php

Syndicate content