Last active
August 16, 2020 14:24
-
-
Save gartes/046f0f4a6dae64465060cecaaa0ee83f to your computer and use it in GitHub Desktop.
Обновление кодов для товара
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Joomla\CMS\Factory; | |
/** | |
* @package ${NAMESPACE} | |
* @subpackage | |
* | |
* @copyright A copyright | |
* @license A "Slug" license name e.g. GPL2 | |
* @since 3.9 | |
*/ | |
class correcting | |
{ | |
/** | |
* @var \Joomla\CMS\Application\CMSApplication|null | |
* @since 3.9 | |
*/ | |
private $app; | |
/** | |
* @var \JDatabaseDriver|null | |
* @since 3.9 | |
*/ | |
private $db; | |
public static $instance; | |
/** | |
* helper constructor. | |
* @throws Exception | |
* @since 3.9 | |
*/ | |
public function __construct($options = array()) | |
{ | |
$this->app = Factory::getApplication(); | |
$this->db = Factory::getDbo(); | |
#Process | |
$this->ProcessDuplicatesProductCode(); | |
// die(__FILE__ .' '. __LINE__ ); | |
return $this; | |
}#END FN | |
/** | |
* Обработка дубликатов кодов товара | |
* @since 3.9 | |
*/ | |
private function ProcessDuplicatesProductCode(){ | |
$query = $this->db->getQuery(true) ; | |
$table = '#__jshopping_products' ; | |
$selectArr = [ | |
$this->db->quoteName('product_id' ), | |
$this->db->quoteName('parent_id' ), | |
$this->db->quoteName( 'product_ean'), | |
$this->db->quoteName('name_ru-RU' ), | |
]; | |
$whereArr = [ | |
$selectArr[1] . ' = 0' , | |
$selectArr[0] . '<>' . $selectArr[2] | |
]; | |
$query->select($selectArr) ; | |
$query->from($this->db->quoteName( $table ) ) ; | |
$query->where($whereArr) ; | |
$this->db->setQuery($query); | |
$res = $this->db->loadObjectList('product_id') ; | |
if( !count( $res ) ) return true; #END IF | |
$this->app->enqueueMessage('<b>Обновление кодов товара</b>' . PHP_EOL ); | |
try | |
{ | |
foreach ($res as $object ){ | |
$old_ean = $object->product_ean ; | |
$object->product_ean = str_pad( $object->product_id, strlen($object->product_id) + 3, '0', STR_PAD_LEFT); | |
if( true ) | |
{ | |
$this->app->enqueueMessage( $object->{'name_ru-RU'} .' ('.$old_ean.') => ' . $object->product_ean . PHP_EOL ); | |
}#END IF | |
echo'<pre>';print_r( $object );echo'</pre>'.__FILE__.' '.__LINE__; | |
// $this->db->updateObject( $table , $object, 'product_id') ; | |
} | |
// throw new Exception('Code Exception '.__FILE__.':'.__LINE__) ; | |
} | |
catch (Exception $e) | |
{ | |
// Executed only in PHP 5, will not be reached in PHP 7 | |
echo 'Выброшено исключение: ', $e->getMessage(), "\n"; | |
echo'<pre>';print_r( $e );echo'</pre>'.__FILE__.' '.__LINE__; | |
die(__FILE__ .' '. __LINE__ ); | |
} | |
echo'<pre>';print_r( $res );echo'</pre>'.__FILE__.' '.__LINE__; | |
} | |
} | |
new correcting(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment