<?php
/**
*
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* It is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
*
* @category AskSheldon
* @package AskSheldon_Misc
* @copyright Copyright (c) 2016 Marcel Lange (https://www.ask-sheldon.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Marcel Lange <info@ask-sheldon.com>)
*
* Changes the sorting of asksheldon_size attribute options (sets sort order)
* call it with param 'iknowwhatido=yes' to make it work!
*
*/
require_once('../AskSheldon_Shell_Abstract.php');
class AskSheldon_Misc_Attribute_Option_Sorter extends AskSheldon_Shell_Abstract
{
const TARGET_ATTRIBUTE = 'asksheldon_size';
const REFERENCESTORE = 2;
private $_oWriteConnection = null;
private $_sOptionTable = null;
private $_sOptionLableTable = null;
/*
* Starter
* */
public function run()
{
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (!$this->getArg('iknowwhatido') || $this->getArg('iknowwhatido') != 'yes') {
echo $this->usageHelp();
echo "\n DEACTIVATED (call it with param '-iknowwhatido yes' to make it work!) \n";
return -1;
}
if (!$this->getArg('inputfile')) {
echo $this->usageHelp();
echo "\n You have to give me the right params master! \n";
return -1;
}
$rFile = fopen($this->getArg('inputfile'), 'r');
if ($rFile) {
$iCount = 1;
while (($aData = fgetcsv($rFile)) !== false) {
$sSizeDE = $aData[0];
$this->_setAttributeOptionSorting($sSizeDE, $iCount++);
}
}
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f update_size_attribute_option_order.php -iknowwhatido [options]
iknowwhatido lifeguard flag
inputfile CSV-file with size sorting as the first and only column without header line!!!
help This help
USAGE;
}
private function _setAttributeOptionSorting($sSize, $iPosition)
{
$oSelect = new Varien_Db_Select($this->_getWriteConnection());
$oSelect->from(
['eao' => $this->_getOptionTable()],
['option_id']
)
->join(
['eaov' => $this->_getOptionLabelTable()],
"eao.option_id = eaov.option_id AND eaov.store_id = " . self::REFERENCESTORE,
''
)
->where('eao.attribute_id = ?', $this->_getAttibuteId())
->where('eaov.value = ?', $sSize);
$aResult = $this->_getWriteConnection()->fetchCol($oSelect);
foreach ($aResult as $iOptionId) {
$this->_getWriteConnection()->update(
$this->_getOptionTable(),
['sort_order' => $iPosition],
$this->_getWriteConnection()->quoteInto('option_id = ?', $iOptionId)
);
}
}
/**
* Returs a write connection
* @return Varien_Db_Adapter_Pdo_Mysql
*/
private function _getWriteConnection()
{
if (is_null($this->_oWriteConnection)) {
$this->_oWriteConnection = Mage::getSingleton('core/resource')->getConnection('core_write');
}
return $this->_oWriteConnection;
}
/**
* @return string table name
*/
private function _getOptionTable()
{
if (is_null($this->_sOptionTable)) {
$this->_sOptionTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option');
}
return $this->_sOptionTable;
}
/**
* @return string table name
*/
private function _getOptionLabelTable()
{
if (is_null($this->_sOptionLableTable)) {
$this->_sOptionLableTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option_value');
}
return $this->_sOptionLableTable;
}
/**
* @return int ID of product attribute asksheldon_size
*/
private function _getAttibuteId()
{
return Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, self::TARGET_ATTRIBUTE)->getId();
}
}
$oOptionSorter = new AskSheldon_Misc_Attribute_Option_Sorter();
$oOptionSorter->run();