Skip to content

Instantly share code, notes, and snippets.

@herveguetin
herveguetin / customer_group_massupdate_magento.php
Created June 12, 2015 09:50
Affect all Magento customers to a group
<?php
$customerGroupId = SOME_CUSTOMER_GROUP_ID;
$table = Mage::getModel('customer/customer')->getResource()
->getWriteConnection()
->getTable('customer/entity');
$query = "UPDATE {$table} SET group_id = {$customerGroupId} WHERE group_id != {$customerGroupId}";
$conn->query($query);
?>
@herveguetin
herveguetin / create_group_prices_magento.php
Created June 10, 2015 06:59
Create/update group prices programmatically in Magento
<?php
class Namespace_Module_Model_Price_Import extends Mage_Core_Model_Abstract
{
/**
* DB Connection
*
* @var Varien_Db_Adapter_Interface
*/
protected $_conn;
@herveguetin
herveguetin / next_business_day.php
Last active August 26, 2017 18:22
Retrieve the next business or calendar day in Magento with an optional leadtime. Magento weekend days config is taken into account.
<?php
/**
* Retrieve the next business day with an optional leadtime.
* If leadtime = 1 => find the next business day
* If leadtime = 2 => find the "next next" business day
* etc...
*
* @param int $leadtime
* @return string|bool
*/
@herveguetin
herveguetin / run_method.php
Created May 5, 2015 09:09
Call a Magento model / method from shell
#!/usr/bin/php -f
<?php
require_once 'abstract.php';
class Herve_Run_Method extends Mage_Shell_Abstract
{
protected function _parseArgs()
{
@herveguetin
herveguetin / convert_string_to_url_key.php
Last active November 13, 2015 19:21
Transform any string to url-like string in Magento (useful for dynamic CSS classes for example)
<?php
$string = 'My String';
$urlKeyString = Mage::getSingleton('catalog/product')->formatUrlKey($string);
// Returns => my-string
?>
@herveguetin
herveguetin / cart_price_rule_1_steps.txt
Last active January 13, 2017 06:08
Programmatically create a cart price rule in Magento
This is a quick an easy hack to programmatically create a cart price rule.
1. Temporarely edit Mage_Adminhtml_Promo_QuoteController::saveAction()
2. Around line 123, you will find $data = $this->getRequest()->getPost();
3. Below this line create a new line containing :
var_export($data);
die();
@herveguetin
herveguetin / create_cms_block.php
Last active August 29, 2015 14:16
Create a CMS block programmatically in Magento
<?php
$content = <<<EOD
Block Content line 1
Block Content line 2
EOD;
$cmsBlock = Mage::getModel('cms/block')->addData(
array(
'title' => 'Block Title',
'identifier' => 'block_identifier',
@herveguetin
herveguetin / Observer.php
Last active April 1, 2019 11:06
Dynamically add items to admin menu in Magento
<?php
class My_Module_Model_Observer {
/**
* Update admin menu with dynamic items
*/
public function updateAdminMenu()
{
$menu = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu/MAIN_MENU_ITEM/children/MENU_ITEMS_CONTAINER/children');
@herveguetin
herveguetin / htaccess_avoid_robots_indexing.sh
Last active August 29, 2015 14:15
.htaccess lines to avoid robots indexing
# Make sure mod_headers is enabled in Apache
sudo a2enmod headers
# Place this in .htaccess
<IfModule mod_headers.c>
Header set X-Robots-Tag "noindex, nofollow"
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</IfModule>
@herveguetin
herveguetin / cache_warmer.sh
Created December 9, 2014 09:50
Quick, easy and dirty cache warmer based on sitemap.xml
#!/bin/bash
URL='www.domain.nd'
SITEMAP='sitemap.xml'
wget --quiet http://$URL/$SITEMAP --no-cache --output-document - | egrep -o "http://$URL[^<]+" | while read line; do
time curl -A 'Cache Warmer' -s -L $line > /dev/null 2>&1
echo $line
done