name = 'massupdate'; $this->tab = 'Products'; $this->version = '1.1'; parent::__construct(); /* The parent construct is required for translations */ $this->page = basename(__FILE__, '.php'); $this->displayName = $this->l('Mass Update'); $this->description = $this->l('Perform mass updates on all your products (price, weight, and features)'); $this->confirmUninstall = $this->l('Aww... are you sure you want to delete me?'); } function install($try_again=true) { //if (!$this->addMassUpdateHook() || !parent::install() || !$this->registerHook('shippingCalculate') || !$this->registerHook('shippingCalculateDays')){ if (!parent::install()){ return false; } return (Configuration::updateValue('PS_MASSUPDATE_TITLE', array('1' => 'Mass Update')) ); } function uninstall() { if (parent::uninstall() == false) return false; return (Configuration::deleteByName('PS_MASSUPDATE_TITLE')); } function getConfig() { $currencies = Currency::getCurrencies(); $default_currency = Configuration::get('PS_CURRENCY_DEFAULT'); foreach ($currencies as $currency) { if ($currency['id_currency'] == $default_currency) $sign = $currency['sign']; } $weight_units = strtolower(Configuration::get('PS_WEIGHT_UNIT')); $main_product_fields = array( "price"=>array( "db_field"=>"price", "friendly"=>$this->l('Price'), "prefix"=> '(' . $sign . ')', ), "weight"=>array( "db_field"=>"weight", "friendly"=>$this->l('Weight') . " ($weight_units)", ), "quantity"=>array( "db_field"=>"quantity", "friendly"=>$this->l('Quantity'), ), //add more, eg: quantity, here. ); return $main_product_fields; } function updateProducts() { $language_id = Configuration::get('PS_LANG_DEFAULT'); $product_settings = $_POST['mup']; if(!is_array($product_settings)){ return false; } $main_fields = $this->getConfig(); foreach($product_settings as $product_id => $data){ $product_id = (int)$product_id; if(!$product_id)continue; $sql = "UPDATE "._DB_PREFIX_."product SET date_upd = NOW() "; $do_update = false; // we're updating the product id. check and update the main fields. foreach($main_fields as $field){ $update_value = trim(pSQL($data[$field['db_field']])); if($update_value){ // we've found a main field to update! $do_update = true; $sql .= ", `".$field['db_field']."` = '$update_value' "; } } if($do_update){ $sql .= " WHERE `id_product` = '$product_id' LIMIT 1"; //echo $sql ."
\n"; if(!Db::getInstance()->Execute($sql)){ // yer yer i know - dodgey - but this should never happen, all input is sanatised. // a "just in case" so we dont go and bork all our products if there's a coding error. echo "FAILED TO UPDATE: $sql "; echo mysql_error(); exit; } } // now we have to check the product features! trickeyness.. // we grab a list of their current features, if any... $product_features = Product::getFeaturesStatic($product_id); //print_r($product_features); $new_features = array(); $do_feature_update = false; if(isset($data['ff']) && is_array($data['ff'])){ foreach($data['ff'] as $feature_id => $feature_value){ $update_value = trim(pSQL($feature_value)); if($update_value){ //YEY! we can update this products feature value... $do_feature_update = true; $new_features[$feature_id] = $update_value; } } } //print_r($new_features); if($do_feature_update){ $product = new Product($product_id); $product->deleteFeatures(); foreach($new_features as $feature_id => $feature_value){ // add our new custom feature: //echo "Adding $feature_id as $feature_value
\n"; $id_value = $product->addFeaturesToDB($feature_id, 0, true, $language_id); $product->addFeaturesCustomToDB($id_value, $language_id, $feature_value); } } } return true; } function getContent() { $language_id = Configuration::get('PS_LANG_DEFAULT'); $this->_html = '

'.$this->displayName.'

'; /* Update the settings */ if (isset($_POST['mu_doupdate'])) { if (!$this->updateProducts()) $this->_html .= $this->displayError($this->l('An error occurred during product updating, please try saving again.')); else $this->_html .= $this->displayConfirmation($this->l('Your products have been successfully updated!')); } $this->_html .= '
'.$this->l('Mass Update Your Products').'
'; $sql = 'SELECT * FROM `'._DB_PREFIX_.'feature` f LEFT JOIN `'._DB_PREFIX_.'feature_lang` fl ON ( f.`id_feature` = fl.`id_feature` AND fl.`id_lang` = '.$language_id.')'; $feature_results = Db::getInstance()->ExecuteS($sql); // all products $sql = 'SELECT * FROM `'._DB_PREFIX_.'product` p LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`) WHERE pl.`id_lang` = '.$language_id.''; $all_products = Db::getInstance()->ExecuteS($sql); foreach($all_products as &$product)$product['db_price']=$product['price']; $all_products = Product::getProductsProperties($language_id,$all_products); $main_fields = $this->getConfig(); ob_start(); ?>

$f){ ?> $v){ ?> $f){ ?> $v){ // what value is in this feature? $feature_value = ''; foreach($product['features'] as $f){ if($f['id_feature']==$v['id_feature']){ $feature_value = $f['value']; } } ?>
Product




_html .= ob_get_clean(); $this->_html .= '
'; return $this->_html; } } ?>