
Originally Posted by
jsarwar
Model number is as item number or SKU which is always different than UPC/barcode of products. I already tried to work by changing EP4_DB_FILTER_KEY it did not work therefore I seek for help in this forum. Can you please provide the codes changes or updated file to work with upc?
Thank you.
Apparently, I had started the development of considering an alternate primary key, but had not completed it. I am incorporating the below changes (outside of specific code for products_upc) into the distribution to allow further use/development of an alternate primary_key. Note that I may have missed something and would appreciate input on improvement. I do know that for example the detailed attributes handler still uses the v_products_id column primarily for import although there are messages that relate to the primary key being something else as well...
Anyways, I am providing the needed file changes in reverse order from the bottom of the file to the top so that regardless additional changes personally made at least there is a chance that the instruction can be followed. I am using 4.0.37.10 against which to reference.
So starting in admin/easypopulate_4_import.php at line 1647 adding the code in green:
Code:
if (!in_array($chosen_key, array('v_products_id', 'v_products_model'))) { $chosen_key_sub = $chosen_key;
if (strpos($chosen_key_sub, 'v_') === 0) {
$chosen_key_sub = substr($chosen_key_sub, 2);
}
$sql = $db->bindVars($sql, ':' . $chosen_key_sub . ':', (!empty(${$chosen_key}) ? ${$chosen_key} : ''), $zc_support_ignore_null);
}
$result = ep_4_query($sql);
Line 1601:
Code:
if (!in_array($chosen_key, array('v_products_id', 'v_products_model'))) { $chosen_key_sub = $chosen_key;
if (strpos($chosen_key_sub, 'v_') === 0) {
$chosen_key_sub = substr($chosen_key_sub, 2);
}
$sql = $db->bindVars($sql, ':' . $chosen_key_sub . ':', (!empty(${$chosen_key}) ? ${$chosen_key} : ''), $zc_support_ignore_null);
}
$result = ep_4_query($sql);
Line 1190:
Code:
if (!in_array($chosen_key, array('v_products_id', 'v_products_model'))) { $chosen_key_sub = $chosen_key;
if (strpos($chosen_key_sub, 'v_') === 0) {
$chosen_key_sub = substr($chosen_key_sub, 2);
}
$sql = $db->bindVars($sql, ':' . $chosen_key_sub . ':', (!empty(${$chosen_key}) ? ${$chosen_key} : ''), $zc_support_ignore_null);
}
$result = ep_4_query($sql);
Line 269:
Code:
if (ep4_field_in_file($chosen_key) && !in_array($chosen_key, array('v_products_id', 'v_products_model'))){ if (!in_array($chosen_key, array('v_products_id', 'v_products_model'))) {
$chosen_key_sub = $chosen_key;
if (strpos($chosen_key_sub, 'v_') === 0) {
$chosen_key_sub = substr($chosen_key_sub, 2);
}
$sql = $db->bindVars($sql, ':' . $chosen_key_sub . ':', $items[$filelayout[$chosen_key]], $zc_support_ignore_null);
}
}
$result = ep_4_query($sql);
In admin/easypopulate_4_import.php add the notifier that will be needed to execute the code by making the following change(s) of adding the code that is in green (with the switch statement beginning at/near line 98):
Code:
switch (EP4_DB_FILTER_KEY) {
case 'products_model':
$chosen_key = 'v_products_model';
$chosen_key_sql = "
p.products_model = :products_model:";
$chosen_key_sql_limit = " WHERE (products_model = :products_model:) LIMIT 1";
break;
case 'blank_new':
case 'products_id':
$chosen_key = 'v_products_id';
$chosen_key_sql = "
p.products_id = :products_id:";
$chosen_key_sql_limit = " WHERE (products_id = :products_id:) LIMIT 1";
break;
default:
$chosen_key = 'v_products_model';
$chosen_key_sql = "
p.products_model = :products_model:";
$chosen_key_sql_limit = " WHERE (products_model = :products_model:) LIMIT 1";
$zco_notifier->notify('EP4_IMPORT_DEFAULT_EP4_DB_FILTER_KEY_DATA', EP4_DB_FILTER_KEY, $chosen_key, $chosen_key_sql, $chosen_key_sql_limit);
break;
}
$zco_notifier->notify('EP4_IMPORT_AFTER_EP4_DB_FILTER_KEY');
In admin/includes/functions/extra_functions/easypopulate_4_functions.php
beginning at line 494 where the function ep_4_remove_product is initially defined, modify it to include the below content in green:
Code:
function ep_4_remove_product($product_model) { global $db, $ep_debug_logging, $ep_debug_logging_all, $ep_stack_sql_error, $zco_notifier, $zc_support_ignore_null;
$project = PROJECT_VERSION_MAJOR.'.'.PROJECT_VERSION_MINOR;
$ep_uses_mysqli = ((PROJECT_VERSION_MAJOR > '1' || PROJECT_VERSION_MINOR >= '5.3') ? true : false);
$sql = "SELECT p.products_id FROM ".TABLE_PRODUCTS . " p";
switch (EP4_DB_FILTER_KEY) {
case 'products_model':
$sql .= " WHERE p.products_model = :products_model:";
$sql = $db->bindVars($sql, ':products_model:', $product_model, $zc_support_ignore_null);
break;
case 'blank_new':
case 'products_id':
$sql .= " WHERE p.products_id = :products_id:";
$sql = $db->bindVars($sql, ':products_id:', $product_model, 'integer');
break;
default:
$sql_add = " WHERE p.products_model = :products_model:";
$zco_notifier->notify('EP4_FUNCTION_REMOVE_PRODUCT', $product_model, $sql_add);
$sql .= $sql_add;
unset($sql_add);
$sql = $db->bindVars($sql, ':products_model:', $product_model, $zc_support_ignore_null);
In admin/includes/modules/easypopulate_4_featured_ep.php at line 36:
Code:
$sql = $db->bindVars($sql, ':products_model:', $items[$filelayout['v_products_model']], $zc_support_ignore_null); $sql = $db->bindVars($sql, ':products_id:', $items[$filelayout['v_products_id']], $zc_support_ignore_null);
if (!in_array($chosen_key, array('v_products_id', 'v_products_model'))) {
$chosen_key_sub = $chosen_key;
if (strpos($chosen_key_sub, 'v_') === 0) {
$chosen_key_sub = substr($chosen_key_sub, 2);
}
$sql = $db->bindVars($sql, ':' . $chosen_key_sub . ':', $items[$filelayout[$chosen_key]], $zc_support_ignore_null);
}
$result = ep_4_query($sql);
The above changes are captured at: mc12345678/EasyPopulate-4.0 at v4.0.37.11 (github.com)
Create a new observer file:
admin/includes/classes/observers/class.easypopulate_4_default_ep4_db_filter_key_observer.php
with the following contents:
Code:
<?php/* This is an observer class to take action when the ep4_db_filter_key is processed/determined.
It is expected to set the variables needed within EP4 to support an alternate primary key.
*/
class ep4_db_filter_key extends base
{
public function __construct()
{
$attachThis = array();
$attachThis[] = 'EP4_MODULES_FILELAYOUT_CHOSEN_KEY';
$attachThis[] = 'EP4_IMPORT_DEFAULT_EP4_DB_FILTER_KEY_DATA';
$attachThis[] = 'EP4_FUNCTION_REMOVE_PRODUCT';
$this->attach($this, $attachThis);
}
public function updateEp4ImportDefaultEp4DbFilterKeyData(&$callingClass, $notifier, $ep4_db_filter_key, &$chosen_key, &$chosen_key_sql, &$chosen_key_sql_limit)
{
if ($ep4_db_filter_key == 'products_upc') {
$chosen_key = 'v_products_upc';
$chosen_key_sql = "
p.products_upc = :products_upc:";
$chosen_key_sql_limit = " WHERE (products_upc = :products_upc:) LIMIT 1";
}
}
public function updateEp4ModulesFilelayoutChosenKey(&$callingClass, $notifier, $emptyArray, &$chosen_key)
{
if (EP4_DB_FILTER_KEY == 'products_upc') {
$chosen_key = 'v_products_upc';
}
}
public function updateEp4FunctionRemoveProduct(&$callingClass, $notifier, $key_value, &$sql_add)
{
if (EP4_DB_FILTER_KEY == 'products_upc') {
global $chosen_key, $chosen_key_sql, $zc_support_ignore_null;
if (in_array($chosen_key, array('v_products_id', 'v_products_model'))) {
return;
}
$sql_add = $chosen_key_sql;
$chosen_key_sub = $chosen_key;
if (strpos($chosen_key_sub, 'v_') === 0) {
$chosen_key_sub = substr($chosen_key_sub, 2);
}
$sql_add = $db->bindVars($sql_add, ':' . $chosen_key_sub . ':', $key_value, $zc_support_ignore_null);
}
}
}
Then add an autoloader to implement the above code:
admin/includes/auto_loaders/easypopulate_4_ep4_db_filter_key.php
Code:
<?php
/* loads and instantiates observer class within the admin file structure
*/
$autoLoadConfig[0][] = array(
'autoType' => 'class',
'loadFile' => 'observers/class.easypopulate_4_default_ep4_db_filter_key_observer.php',
'classPath' => DIR_WS_CLASSES,
);
$autoLoadConfig[180][] = array(
'autoType' => 'classInstantiate',
'className' => 'ep4_db_filter_key',
'objectName' => 'ep4_db_filter_key_obs',
);
Then to possibly "pull it together" in:
admin/includes/extra_configures/
create a file, perhaps named: easypopulate_4_ep4_db_filter_key.php
Code:
<?php
/* This hard codes the filter_key designation which will override the database setting for the same variable.
The intent is to force EP4 to use the following EP4_DB_FILTER_KEY setting.
Doing so is intended to treat the defined variable as the primary key when exporting and importing data.
Upon successful use, the associated database field could be updated to recognize this variable. */
define('EP4_DB_FILTER_KEY', 'products_upc');
While I have not tested the above, it appears that it will have the desired effect. What it will do, by the "last" file is assign the filter key (primary key) to be products_upc which is then used/evaluated in the import process. Because that key doesn't exist within the basic structure, the default code is executed. Initially there are default values applied, but then a notifier is called to allow some modification at that point.
Within the observer, the suggested key is evaluated and if it meets the desired value, the variables $chosen_key, $chosen_key_sql, and $chosen_key_sql_limit are each set to what appear to be valid values for this situation where the upc is expected to be either actually or at least desired to be handled as a unique key/value... If the database does not contain unique data, then importing data matching a key that is non-unique will cause all such similar to be updated and not just the first one found, this is along the lines of having linked product in multiple categories.
If that seems to work as desired (again note that only able to update based on products_upc if the admin/includes/extra_configures file is still in place and/or the define is "active"...), then can update the configuration key to have an additional value available from which to select so that you can better control the primary key that is being used. To add the products_upc option to the configuration selector run the following sql:
Code:
UPDATE configuration SET set_function = 'zen_cfg_select_option(array(\'products_model\', \'products_id\', \'blank_new\', \'products_upc\'),' WHERE configuration_key = 'EP4_DB_FILTER_KEY';
The above sql statement doesn't update the configuration_description text to include information about what that selection does, but understand that there is no code currently incorporated about supporting a "blank" value for the products_upc when products_upc is set as the primary key...
Bookmarks