Skip to content

Instantly share code, notes, and snippets.

@btray77
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btray77/0e34cda8fa751524b610 to your computer and use it in GitHub Desktop.
Save btray77/0e34cda8fa751524b610 to your computer and use it in GitHub Desktop.
Update MinPrice
I created a new attribute called min_price (price field), hash_code (text)
added the attribute set to prices.
The following code "should" update the minPrice attribute in Magento.
Single Product update seems to work to generate a new min_price. hash
Everything seems to be working
Suggestions are welcome.
<?xml version="1.0" encoding="UTF-8"?>
<!-- html/app/code/local/Overnight/Pricer/etc/config.xml -->
<config>
<modules>
<Overnight_Pricer>
<version>0.1.1</version>
</Overnight_Pricer>
</modules>
<global>
<models>
<overnight_pricer>
<class>Overnight_Pricer_Model</class>
</overnight_pricer>
</models>
</global>
<adminhtml>
<events>
<catalog_product_save_before>
<observers>
<overnight_pricer>
<class>overnight_pricer/observer</class>
<method>catalogUpdateMinPrice</method>
</overnight_pricer>
</observers>
</catalog_product_save_before>
<catalog_product_attribute_update_before>
<observers>
<overnight_pricer>
<class>overnight_pricer/observer</class>
<method>catalogUpdateMinPriceMassaction</method>
</overnight_pricer>
</observers>
</catalog_product_attribute_update_before>
</events>
</adminhtml>
<!-- -->
<crontab>
<jobs>
<overnight_pricer>
<schedule>
<cron_expr>*/10 * * * *</cron_expr>
</schedule>
<run>
<model>overnight_pricer/observer::updateCost</model>
</run>
</overnight_pricer>
</jobs>
</crontab>
<!-- -->
</config>
<?php class Overnight_Pricer_Model_Observer
//html/app/code/local/Overnight/Pricer/Model/Observer.php
{
protected $additionalValue = 1.50;
protected $multiplicationValue = 0.84; // 16% fees
public $echoout = false;
/**
* Handle catalog_product_save_before event
*
* @param Varien_Event_Observer $observer
* @return void
*/
private function output($out){
echo $out. PHP_EOL;
}
public function catalogUpdateMinPrice(Varien_Event_Observer $observer)
{
/** @var $product Mage_Catalog_Model_Product */
$product = $observer->getEvent()->getDataObject();
$cost = $product->getCost();
$minPrice = $product->getMinPrice();
$hashcode = $product->getHashCode();
$newMinPrice = null;
if($cost != ""){
$newMinPrice = round(($cost+$this->additionalValue)/$this->multiplicationValue,2);
}
$hash = md5($cost . $newMinPrice);
if (($hashcode != $hash) || ($minPrice == "")) {
$product->setMinPrice($newMinPrice);
$product->setHashCode($hash);
Mage::log("catalogUpdateMinPrice ".$newMinPrice." MinPrice - {$cost} Cost",null,'updatedprice.log');
} else
{
Mage::log("catalogUpdateMinPrice No Update Needed",null,'updatedprice.log');
}
}
/**
* Handle catalog_product_attribute_update_before event
*
* @param Varien_Event_Observer $observer
*/
public function catalogUpdateMinPriceMassaction(Varien_Event_Observer $observer)
{
/** @var $block Mage_Adminhtml_Block_Catalog_Product_Edit_Action_Attribute_Tab_Attributes */
$attributesData = $observer->getEvent()->getAttributesData();
$cost = $attributesData['cost'];
if($cost != ""){
//$newMinPrice = ($cost+$this->additionalValue)/$this->multiplicationValue;
$newMinPrice = round(($cost+$this->additionalValue)/$this->multiplicationValue,2);
$hash = md5($cost . $newMinPrice);
$attributesData['min_price'] = $newMinPrice;
$attributesData['hash_code'] = $hash;
$observer->getEvent()->setAttributesData($attributesData);
}
Mage::log("catalogUpdateMinPriceMassaction ".$newMinPrice." MinPrice - {$cost} Cost",null,'updatedprice.log');
}
public function postlog($stringData){
/*
try {
$myFile = "/usr/share/nginx/html/app/code/local/Overnight/Pricer/cronlog.txt";
$fh = fopen($myFile, 'a+');
$stringData = date('l jS \of F Y h:i:s A'). ' '.$stringData ."\n";
fwrite($fh, $stringData);
fclose($fh);
} catch (Exception $e) {
Mage::printException($e);
}
*/
}
public function updateCost() {
/**
* suppose you have hash_code attribute for catalog products which is store
* hash code of synchronizeable attribute values.
*/
// Need to add force for manual update
// Mage:log doesn't seem to work inside cron??
Mage::log("cron Started.....",null,'updatedprice.log');
$this->postlog("cron Started.....");
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('min_price')
->addAttributeToSelect('cost')
->addAttributeToSelect('hash_code')
->addAttributeToSelect('min_price');
$counter = 0;
foreach ($productCollection as $product) {
$counter++;
$cost = $product->getCost();
$minPrice = $product->getMinPrice();
$sku = $product->getSku();
$hash = md5($cost . $minPrice);
$hashcode = $product->getHashCode();
$this->output('Counter: '. $counter);
if (($hashcode != $hash) || ($minPrice == "")) {
$this->output('Sku: '.$sku);
$this->output('Cost: '.$cost);
$this->output('Min Price: '.$minPrice);
$this->output('Hash : '.$hash);
$this->output('Hash Code: '.$hashcode);
$this->output('Hash Being Set ******');
$newMinPrice = null;
if($cost != ""){
$newMinPrice = round(($cost+$this->additionalValue)/$this->multiplicationValue,2);
}
Mage::log("Updating Product {$sku} ".$newMinPrice." MinPrice - {$cost} Cost",null,'updatedprice.log');
$this->postlog("Updating Product {$sku} ".$newMinPrice." MinPrice - {$cost} Cost");
$product->setData('hash_code', $hash)->getResource()->saveAttribute($product, 'hash_code');
$product->setData('min_price', $newMinPrice)->getResource()->saveAttribute($product, 'min_price');
// break;
$minPrice = $product->getMinPrice();
$hashcode = $product->getHashCode();
$this->output('New Min Price: '.$minPrice);
$this->output('New Hash Code: '.$hashcode);
}
usleep(2000);//0.002 seconds
}
$this->postlog("cron Complete.....");
}
public function updateQuantityInPackage() {
/**
* Fix to add Quantity In Package Field with Default Value since Magneto didn't set it
*/
$this->postlog("Started.....");
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('quantityinpackage');
$counter = 0;
foreach ($productCollection as $product) {
//print_r($product);
$counter++;
$this->output('Counter: '. $counter);
//$q = $product->getQunantityinpackage();
$q = $product->getData('quantityinpackage');
$this->output('Current Quantity: '.$q);
if ($q == '') {
$product->setData('quantityinpackage', '1')->getResource()->saveAttribute($product, 'quantityinpackage');
// Update products that have no value only.
usleep(2000);//0.002 seconds
$q = $product->getData('quantityinpackage');
$this->output('New Quantity: '.$q);
}
}
$this->postlog("Complete.....");
}
}
<?xml version="1.0"?>
<!-- html/app/etc/modules/Overnight_Pricer.xml -->
<config>
<modules>
<Overnight_Pricer>
<active>true</active>
<codePool>local</codePool>
</Overnight_Pricer>
</modules>
</config>
<?php
// Put in html/app/code/local/Overnight/Pricer/tester.php
ignore_user_abort(true);//if caller closes the connection (if initiating with cURL from another PHP, this allows you to end the calling PHP script without ending this one)
set_time_limit(0);
ini_set('mysql.connect_timeout', 3600);
ini_set('default_socket_timeout', 3600);
//echo ini_get("memory_limit")."\n";
ini_set("memory_limit","768M");
//echo ini_get("memory_limit")."\n";
require_once '../../../../Mage.php';
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app("default");
Mage::app ()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$obj = Mage::getModel('overnight_pricer/observer');
$obj->echoout=true;
$obj->updateCost();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment