Skip to content

Instantly share code, notes, and snippets.

@gartes
Last active August 16, 2020 15:43
Show Gist options
  • Save gartes/ba8e504e9758ddfc451909af82fa3706 to your computer and use it in GitHub Desktop.
Save gartes/ba8e504e9758ddfc451909af82fa3706 to your computer and use it in GitHub Desktop.
Установить уникальне уоды для товаров JoomShopping
<?php
use Joomla\CMS\Factory;
/**
* @package correcting
* @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();
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( $this->db->updateObject( $table , $object, 'product_id') )
{
$this->app->enqueueMessage( $object->{'name_ru-RU'} .' ('.$old_ean.') => ' . $object->product_ean . PHP_EOL );
}#END IF
}
// 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__ );
}
}
}
#Auto Star
new correcting();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment