protected function generateProductsData()
    {
        $delimiter = ';';
        $titles = array();
        $id_lang = $this->use_lang;
        $new_path = new Sampledatainstall();
        $f = fopen($new_path->sendPath() . 'output/products.vsc', 'w');
        foreach ($this->product_fields as $field => $array) {
            $titles[] = $array['label'];
        }
        fputcsv($f, $titles, $delimiter, '"');
        $products = Product::getProducts($id_lang, 0, 0, 'id_product', 'ASC', false, true);
        foreach ($products as $product) {
            $line = array();
            $p = new Product($product['id_product'], true, $id_lang, 1);
            foreach ($this->product_fields as $field => $array) {
                $line[$field] = property_exists('Product', $field) && !is_array($p->{$field}) && !Tools::isEmpty($p->{$field}) ? $p->{$field} : '';
            }
            $cats = $p->getProductCategoriesFull($p->id, 1);
            $cat_array = array();
            foreach ($cats as $cat) {
                $cat_array[] = $cat['id_category'];
            }
            $line['categories'] = implode(',', $cat_array);
            $line['price_tex'] = $p->getPrice(false);
            $line['price_tin'] = $p->getPrice(true);
            $line['upc'] = $p->upc ? $p->upc : '';
            $line['features'] = '';
            $features = $p->getFrontFeatures($id_lang);
            $position = 1;
            $devider = '';
            foreach ($features as $feature) {
                $sql = 'SELECT `id_feature`
						FROM ' . _DB_PREFIX_ . 'feature_lang
						WHERE `name` = "' . pSql($feature['name']) . '"';
                $sql1 = 'SELECT `id_feature_value`
						FROM ' . _DB_PREFIX_ . 'feature_value_lang
						WHERE `value` = "' . pSql($feature['value']) . '"';
                $id_feature = Db::getInstance()->getValue($sql);
                $id_feature_value = Db::getInstance()->getValue($sql1);
                $line['features'] .= $devider . $id_feature . ':' . $id_feature_value . ':' . $position;
                $devider = ',';
                $position++;
            }
            $specificPrice = SpecificPrice::getSpecificPrice($p->id, 1, 0, 0, 0, 0);
            $line['reduction_price'] = '';
            $line['reduction_percent'] = '';
            $line['reduction_from'] = '';
            $line['reduction_to'] = '';
            if ($specificPrice) {
                if ($specificPrice['reduction_type'] == 'amount') {
                    $line['reduction_price'] = $specificPrice['reduction'];
                } elseif ($specificPrice['reduction_type'] == 'percent') {
                    $line['reduction_percent'] = $specificPrice['reduction'];
                }
                if ($line['reduction_price'] !== '' || $line['reduction_percent'] !== '') {
                    $line['reduction_from'] = $specificPrice['from'];
                    $line['reduction_to'] = $specificPrice['to'];
                }
            }
            $tags = $p->getTags($id_lang);
            $line['tags'] = $tags;
            $link = new Link();
            $imagelinks = array();
            $images = $p->getImages($id_lang);
            foreach ($images as $image) {
                $imagelink = Tools::getShopProtocol() . $link->getImageLink($p->link_rewrite, $p->id . '-' . $image['id_image']);
                $this->copyConverFileName($imagelink);
                $imagelinks[] = $imagelink;
            }
            $line['image'] = implode(',', $imagelinks);
            $line['delete_existing_images'] = 0;
            $line['shop'] = 1;
            $warehouses = Warehouse::getWarehousesByProductId($p->id);
            $line['warehouse'] = '';
            if (!empty($warehouses)) {
                $line['warehouse'] = implode(',', array_map("{$this->getWarehouses}", $warehouses));
            }
            $values = array();
            $accesories = $p->getAccessories($id_lang);
            if (isset($accesories) && $accesories && count($accesories)) {
                foreach ($accesories as $accesorie) {
                    $values[] = $accesorie['id_product'];
                }
            }
            $line['accessories'] = $values ? implode(',', $values) : '';
            $values = array();
            $carriers = $p->getCarriers();
            if (isset($carriers) && $carriers && count($carriers)) {
                foreach ($carriers as $carrier) {
                    $values[] = $carrier['id_carrier'];
                }
            }
            $line['carriers'] = $values ? implode(',', $values) : '';
            $values = array();
            $customization_fields_ids = $p->getCustomizationFieldIds();
            if (class_exists('CustomizationField') && isset($customization_fields_ids) && $customization_fields_ids && count($customization_fields_ids)) {
                foreach ($customization_fields_ids as $customization_field_id) {
                    $cf = new CustomizationField($customization_field_id['id_customization_field'], $this->use_lang);
                    $values[] = $cf->id . ':' . $cf->type . ':' . $cf->required . ':' . $cf->name;
                }
            }
            $line['customization_fields_ids'] = $values ? implode(',', $values) : '';
            $values = array();
            $attachments = $p->getAttachments($this->use_lang);
            if (isset($attachments) && $attachments && count($attachments)) {
                foreach ($attachments as $attachment) {
                    $values[] = $attachment['id_attachment'];
                }
            }
            $line['attachments'] = $values ? implode(',', $values) : '';
            if (!property_exists('Product', 'base_price')) {
                // for versions < 1.6.0.13
                $line['base_price'] = !is_array($p->base_price) && !Tools::isEmpty($p->base_price) ? $p->base_price : '';
            }
            if (!$line[$field]) {
                $line[$field] = '';
            }
            fputcsv($f, $line, $delimiter, '"');
        }
        fclose($f);
    }
 protected function textRecord()
 {
     if (!($field_ids = $this->product->getCustomizationFieldIds())) {
         return false;
     }
     $authorized_text_fields = array();
     foreach ($field_ids as $field_id) {
         if ($field_id['type'] == Product::CUSTOMIZE_TEXTFIELD) {
             $authorized_text_fields[(int) $field_id['id_customization_field']] = 'textField' . (int) $field_id['id_customization_field'];
         }
     }
     $indexes = array_flip($authorized_text_fields);
     foreach ($_POST as $field_name => $value) {
         if (in_array($field_name, $authorized_text_fields) && !empty($value)) {
             if (!Validate::isMessage($value)) {
                 $this->errors[] = Tools::displayError('Invalid message');
             } else {
                 $this->context->cart->addTextFieldToProduct($this->product->id, $indexes[$field_name], Product::CUSTOMIZE_TEXTFIELD, $value);
             }
         } else {
             if (in_array($field_name, $authorized_text_fields) && empty($value)) {
                 $this->context->cart->deleteCustomizationToProduct((int) $this->product->id, $indexes[$field_name]);
             }
         }
     }
 }
Example #3
0
function textRecord(Product $product, Cart $cart)
{
    global $errors;
    if (!($fieldIds = $product->getCustomizationFieldIds())) {
        return false;
    }
    $authorizedTextFields = array();
    foreach ($fieldIds as $fieldId) {
        if ($fieldId['type'] == _CUSTOMIZE_TEXTFIELD_) {
            $authorizedTextFields[intval($fieldId['id_customization_field'])] = 'textField' . intval($fieldId['id_customization_field']);
        }
    }
    $indexes = array_flip($authorizedTextFields);
    foreach ($_POST as $fieldName => $value) {
        if (in_array($fieldName, $authorizedTextFields) and !empty($value)) {
            if (!Validate::isMessage($value)) {
                $errors[] = Tools::displayError('Invalid message');
            } else {
                $cart->addTextFieldToProduct(intval($product->id), $indexes[$fieldName], $value);
            }
        } elseif (in_array($fieldName, $authorizedTextFields) and empty($value)) {
            $cart->deleteTextFieldFromProduct(intval($product->id), $indexes[$fieldName]);
        }
    }
}
Example #4
0
 public function ajaxProcessUpdateCustomizationFields()
 {
     $errors = array();
     if ($this->tabAccess['edit'] === '1') {
         $errors = array();
         if (Tools::getValue('only_display') != 1) {
             if (!$this->context->cart->id || !($id_product = (int) Tools::getValue('id_product'))) {
                 return;
             }
             $product = new Product((int) $id_product);
             if (!($customization_fields = $product->getCustomizationFieldIds())) {
                 return;
             }
             foreach ($customization_fields as $customization_field) {
                 $field_id = 'customization_' . $id_product . '_' . $customization_field['id_customization_field'];
                 if ($customization_field['type'] == Product::CUSTOMIZE_TEXTFIELD) {
                     if (!Tools::getValue($field_id)) {
                         if ($customization_field['required']) {
                             $errors[] = Tools::displayError('Please fill in all the required fields.');
                         }
                         continue;
                     }
                     if (!Validate::isMessage(Tools::getValue($field_id))) {
                         $errors[] = Tools::displayError('Invalid message');
                     }
                     $this->context->cart->addTextFieldToProduct((int) $product->id, (int) $customization_field['id_customization_field'], Product::CUSTOMIZE_TEXTFIELD, Tools::getValue($field_id));
                 } elseif ($customization_field['type'] == Product::CUSTOMIZE_FILE) {
                     if (!isset($_FILES[$field_id]) || !isset($_FILES[$field_id]['tmp_name']) || empty($_FILES[$field_id]['tmp_name'])) {
                         if ($customization_field['required']) {
                             $errors[] = Tools::displayError('Please fill in all the required fields.');
                         }
                         continue;
                     }
                     if ($error = ImageManager::validateUpload($_FILES[$field_id], (int) Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE'))) {
                         $errors[] = $error;
                     }
                     if (!($tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES[$field_id]['tmp_name'], $tmp_name)) {
                         $errors[] = Tools::displayError('An error occurred during the image upload process.');
                     }
                     $file_name = md5(uniqid(rand(), true));
                     if (!ImageManager::resize($tmp_name, _PS_UPLOAD_DIR_ . $file_name)) {
                         continue;
                     } elseif (!ImageManager::resize($tmp_name, _PS_UPLOAD_DIR_ . $file_name . '_small', (int) Configuration::get('PS_PRODUCT_PICTURE_WIDTH'), (int) Configuration::get('PS_PRODUCT_PICTURE_HEIGHT'))) {
                         $errors[] = Tools::displayError('An error occurred during the image upload process.');
                     } elseif (!chmod(_PS_UPLOAD_DIR_ . $file_name, 0777) || !chmod(_PS_UPLOAD_DIR_ . $file_name . '_small', 0777)) {
                         $errors[] = Tools::displayError('An error occurred during the image upload process.');
                     } else {
                         $this->context->cart->addPictureToProduct((int) $product->id, (int) $customization_field['id_customization_field'], Product::CUSTOMIZE_FILE, $file_name);
                     }
                     unlink($tmp_name);
                 }
             }
         }
         $this->setMedia();
         $this->initFooter();
         $this->context->smarty->assign(array('customization_errors' => implode('<br />', $errors), 'css_files' => $this->css_files));
         return $this->smartyOutputContent('controllers/orders/form_customization_feedback.tpl');
     }
 }
 public function hookDisplayHeader()
 {
     //die('header');
     $this->context->controller->addCSS($this->_path . 'css/nmcustomgift.css', 'all');
     //$this->context->cart->deleteProduct(19);
     //$this->context->cart->update();
     //$produit = new Product(19, true, (int)($this->context->cookie->id_lang));
     //print_r( Product::getDefaultAttribute( 19,0 ) ); die();
     /*
     		if( isset( $_GET['test'] ) )
     		{
     			$produit = new Product(19, true, (int)($this->context->cookie->id_lang));
     			$fields = $produit->getCustomizationFieldIds();
     			print_r( $fields );
     			$index = $fields[0]['id_customization_field'];
     			
     			$this->context->cart->addTextFieldToProduct(19, $index, Product::CUSTOMIZE_TEXTFIELD, 'Wazzaaa');
     			$customization = $this->context->cart->getProductCustomization(19);
     			
     			//print_r( $customization );
     			
     			Db::getInstance()->execute('UPDATE ps_customization SET in_cart = 1 WHERE id_customization = ' .$customization[0]['id_customization']);
     			
     			$this->context->cart->updateQty(1, 19, null, $customization[0]['id_customization']);                
     			$this->context->cart->update();
     			
     		}*/
     if (isset($_GET['saveCustomGift'])) {
         $produit = new Product(Configuration::get('CUSTOMGIFT_PRODUCTID'), true, (int) $this->context->cookie->id_lang);
         $fields = $produit->getCustomizationFieldIds();
         //print_r( $fields );
         $index = $fields[0]['id_customization_field'];
         $customization = $this->context->cart->getProductCustomization(Configuration::get('CUSTOMGIFT_PRODUCTID'));
         if (!empty($customization)) {
             $sql = 'UPDATE ps_customized_data SET value = "' . addslashes($_GET['saveCustomGift']) . '" WHERE id_customization = ' . $customization[0]['id_customization'];
             //echo $sql; die();
             Db::getInstance()->execute($sql);
         } else {
             $this->context->cart->deleteProduct(Configuration::get('CUSTOMGIFT_PRODUCTID'));
             $this->context->cart->addTextFieldToProduct(Configuration::get('CUSTOMGIFT_PRODUCTID'), $index, Product::CUSTOMIZE_TEXTFIELD, $_GET['saveCustomGift']);
             $customization = $this->context->cart->getProductCustomization(Configuration::get('CUSTOMGIFT_PRODUCTID'));
             //print_r( $customization );
             Db::getInstance()->execute('UPDATE ps_customization SET in_cart = 1 WHERE id_customization = ' . $customization[0]['id_customization']);
             $this->context->cart->updateQty(1, Configuration::get('CUSTOMGIFT_PRODUCTID'), null, $customization[0]['id_customization']);
             $this->context->cart->update();
         }
         /*
         $customization = $this->context->cart->getProductCustomization(19);
         $sql = 'UPDATE ps_customized_data SET value = "'.addslashes($_GET['saveCustomGift']).'" WHERE id_customization = ' .$customization[0]['id_customization'];
         //echo $sql; die();
         Db::getInstance()->execute($sql);
         */
     }
     /*
     		$produit = new Product(19, true, (int)($this->context->cookie->id_lang));
     		$fields = $produit->getCustomizationFieldIds();
     		$index = $fields[0]['id_customization_field'];
     $products = $this->context->cart->getProducts() ;
     		//print_r( $products ); die();
     		foreach( $products as $p )
     		{
     			//echo $p['id_product'];
     			if( $p['reference'] == 'Voeux2015' )
     			{
     				$custom = $this->context->cart->getProductCustomization( $p['id_product'] );
     				if( empty( $custom ) )
     				{
     					print_r( $custom ); die();
     					
     					$this->context->cart->addTextFieldToProduct( $p['id_product'],$index,Product::CUSTOMIZE_TEXTFIELD,'test8'  );  
     					//$this->context->cart->updateQty(1, $p['id_product'],null,null,'down');
     					$this->context->cart->updateQty(2, $p['id_product'], null, 15);
     					
     				}
     				//print_r( $this->context->cart->getProductCustomization( $p['product_id'] ) ); 
     			}
     			print_r( $this->context->cart->getProductCustomization( $p['id_product'] ) ); 
     		} 
     		//die();
     $products = $this->context->cart->getProducts() ;
     */
 }