Options on the order page opencart 2.3 - opencart

I'm trying to make editing options on the checkout page.
Added function by analogy with quantity
public function update_two($cart_id, $option = array()) {
$this->db->query("UPDATE " . DB_PREFIX . "cart SET option = '" . $this->db->escape(json_encode($option)) . "' WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");
}
Then there is an error
Uncaught exception 'Exception' with message 'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option = '{\"232\":[\"23\"],\"228\":[\"19\"]}' WHERE cart_id = '198' AND custo' at line 1<br />Error No: 1064<br />
UPDATE: line ~ 283
public function updatetwo($cart_id, $option = array()) {
$this->db->query("UPDATE " . DB_PREFIX . "cart SET `option` = '" . $this->db->escape(serialize(json_encode($option))) . "' WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");
}
Line ~ 50 : $options_alt = unserialize($cart['option']);
foreach (json_decode($options_alt) as $product_option_id => $value) {
If anyone needs - everything works

Related

-E parameter exclude the string from result

We are trying to create regex for the following string:-
$last_id . ' where ticket_id=' . $this->getId() {
to test our regex we wrote the following bash one line:-
echo " $last_id . ' where ticket_id=' . $this->getId() {"| grep -i -E ".*(:?.*\$*.WHERE.*) " --color=auto;
The result is:-
. ' where ticket_id=' . ->getId() {
expected result:-
. ' where ticket_id=' . $this->getId() {
But the -E parameter excludes $this. Please help we are stuck.
Your $this gets dropped out before reaching grep, because it's being expanded by shell as a variable named this.
See:
$ echo " $last_id . ' where ticket_id=' . $this->getId() {"
. ' where ticket_id=' . ->getId() {
To prevent parameter expansion in shell, just escape the $:
echo " \$last_id . ' where ticket_id=' . \$this->getId() {"
$last_id . ' where ticket_id=' . $this->getId() {

How to change the search query from AND to OR in Opencart?

Opencart: v.1.5.6.4
When someone searches for "iphone nikon" in Opencart's search input text field the results are:
"There is no product that matches the search criteria." (example - https://o-v156x.my-soul.net/index.php?route=product/search&search=iphone%20nikon)
What I want to achieve is to change the search query from AND to OR to get results:
iphone
Nikon D300
or all the products where these words appear in product's name.
I am not interested in live search (auto-complete or auto-suggest or auto-correct).
I am pretty sure that the changes I want to make (by using vQmod) are inside the file /catalog/model/catalog/product.php.
Where exactly is the code needed to be changed?
EDIT
catalog/model/catalog/product.php
Locate the function getProducts()
Find the following code block in relevant function:
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
and replace the code block with:
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" OR ", $implode) . "";
}
Find the following code inside the getTotalProducts() function
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
and replace with following
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" OR ", $implode) . "";
}
Hope it helps, CHEERS!
Look for the function called getProducts() in the file /catalog/model/catalog/product.php. Within this look for something similar to the following line which deals with the variable filter_name
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
You will have to change the AND to OR in the above statement, but remember if you make this change in the core any updates to OpenCart will change this back to what the update is.

How to REPLACE accent characters in Opencart for SEO url

First of all, I am not a programmer, but I am trying to do some mods in my e-shop running on opencart so please be patient with me :) I have read tons of forums about functions and changing characters, but still couldn't find the simple answer that would help me to get it done. I am trying to make get all characters to lowercase, spaces replaced with "-" and accents replaced with similar characters.
I think this is the piece of code in Opencart I need to modify, but the question is how:
if ($data['keyword']) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
if I change this:
. $this->db->escape($data['keyword']) . "'")
to this:
. $this->db->escape(strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $data['keyword']), '-')))
it does the part of the job, but the next challenge is to get all accents replaced. I have made a function.php with this code:
<?php
function accents($string)
{
//search
$dia = array('á', 'ä', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ĺ', 'ň', 'ó', 'ô', 'ŕ', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Ä', 'Č', 'Ď', 'É', 'Í', 'Ľ', 'Ĺ', 'Ň', 'Ó', 'Ô', 'Ř', 'Š', 'Ť', 'Ú', 'Ý', 'Ž', ' ','\'','%');
//replace
$nodia = array('a', 'a', 'c', 'd', 'e', 'e', 'i', 'l', 'l', 'n', 'o', 'o', 'r', 'r', 's', 't', 'u', 'u', 'y', 'z', 'A', 'A', 'C', 'D', 'E', 'I', 'L', 'L', 'N', 'O', 'O', 'R', 'S', 'T', 'U', 'Y', 'Z', '-','','');
return str_replace($dia, $nodia, $string);
}
?>
That should do the part of the trick, but I don't know how to make it work with the other part of the code.
This is the part of the product.php. The file where it needs to be aplicated..
This is the part of product.php Would you please advice me what exactly and where to put?
<?php
class ModelCatalogProduct extends Model {
public function addProduct($data) {
$this->event->trigger('pre.admin.product.add', $data);
$this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
$product_id = $this->db->getLastId();
if (isset($data['image'])) {
$this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
}
foreach ($data['product_description'] as $language_id => $value) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
}
if (isset($data['product_store'])) {
foreach ($data['product_store'] as $store_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "'");
}
}
if (isset($data['product_attribute'])) {
foreach ($data['product_attribute'] as $product_attribute) {
if ($product_attribute['attribute_id']) {
foreach ($product_attribute['product_attribute_description'] as $language_id => $product_attribute_description) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_attribute SET product_id = '" . (int)$product_id . "', attribute_id = '" . (int)$product_attribute['attribute_id'] . "', language_id = '" . (int)$language_id . "', text = '" . $this->db->escape($product_attribute_description['text']) . "'");
}
}
}
}
if (isset($data['product_option'])) {
foreach ($data['product_option'] as $product_option) {
if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
if (isset($product_option['product_option_value'])) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', required = '" . (int)$product_option['required'] . "'");
$product_option_id = $this->db->getLastId();
foreach ($product_option['product_option_value'] as $product_option_value) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");
}
}
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', value = '" . $this->db->escape($product_option['value']) . "', required = '" . (int)$product_option['required'] . "'");
}
}
}
if (isset($data['product_discount'])) {
foreach ($data['product_discount'] as $product_discount) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_discount['customer_group_id'] . "', quantity = '" . (int)$product_discount['quantity'] . "', priority = '" . (int)$product_discount['priority'] . "', price = '" . (float)$product_discount['price'] . "', date_start = '" . $this->db->escape($product_discount['date_start']) . "', date_end = '" . $this->db->escape($product_discount['date_end']) . "'");
}
}
if (isset($data['product_special'])) {
foreach ($data['product_special'] as $product_special) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_special['customer_group_id'] . "', priority = '" . (int)$product_special['priority'] . "', price = '" . (float)$product_special['price'] . "', date_start = '" . $this->db->escape($product_special['date_start']) . "', date_end = '" . $this->db->escape($product_special['date_end']) . "'");
}
}
if (isset($data['product_image'])) {
foreach ($data['product_image'] as $product_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($product_image['image']) . "', sort_order = '" . (int)$product_image['sort_order'] . "'");
}
}
if (isset($data['product_download'])) {
foreach ($data['product_download'] as $download_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
}
}
if (isset($data['product_category'])) {
foreach ($data['product_category'] as $category_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
}
}
if (isset($data['product_filter'])) {
foreach ($data['product_filter'] as $filter_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_filter SET product_id = '" . (int)$product_id . "', filter_id = '" . (int)$filter_id . "'");
}
}
if (isset($data['product_related'])) {
foreach ($data['product_related'] as $related_id) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
}
}
if (isset($data['product_reward'])) {
foreach ($data['product_reward'] as $customer_group_id => $product_reward) {
if ((int)$product_reward['points'] > 0) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_reward SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$customer_group_id . "', points = '" . (int)$product_reward['points'] . "'");
}
}
}
if (isset($data['product_layout'])) {
foreach ($data['product_layout'] as $store_id => $layout_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_layout SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout_id . "'");
}
}
if (isset($data['keyword'])) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($this->accents($data['keyword'])) . "'");
}
if (isset($data['product_recurrings'])) {
foreach ($data['product_recurrings'] as $recurring) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "product_recurring` SET `product_id` = " . (int)$product_id . ", customer_group_id = " . (int)$recurring['customer_group_id'] . ", `recurring_id` = " . (int)$recurring['recurring_id']);
}
}
$this->cache->delete('product');
$this->event->trigger('post.admin.product.add', $product_id);
return $product_id;
}
public function editProduct($product_id, $data) {
$this->event->trigger('pre.admin.product.edit', $data);
$this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['image'])) {
$this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
foreach ($data['product_description'] as $language_id => $value) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_store'])) {
foreach ($data['product_store'] as $store_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "'");
if (!empty($data['product_attribute'])) {
foreach ($data['product_attribute'] as $product_attribute) {
if ($product_attribute['attribute_id']) {
foreach ($product_attribute['product_attribute_description'] as $language_id => $product_attribute_description) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_attribute SET product_id = '" . (int)$product_id . "', attribute_id = '" . (int)$product_attribute['attribute_id'] . "', language_id = '" . (int)$language_id . "', text = '" . $this->db->escape($product_attribute_description['text']) . "'");
}
}
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_option'])) {
foreach ($data['product_option'] as $product_option) {
if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
if (isset($product_option['product_option_value'])) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', required = '" . (int)$product_option['required'] . "'");
$product_option_id = $this->db->getLastId();
foreach ($product_option['product_option_value'] as $product_option_value) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "', product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");
}
}
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', value = '" . $this->db->escape($product_option['value']) . "', required = '" . (int)$product_option['required'] . "'");
}
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_discount'])) {
foreach ($data['product_discount'] as $product_discount) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_discount['customer_group_id'] . "', quantity = '" . (int)$product_discount['quantity'] . "', priority = '" . (int)$product_discount['priority'] . "', price = '" . (float)$product_discount['price'] . "', date_start = '" . $this->db->escape($product_discount['date_start']) . "', date_end = '" . $this->db->escape($product_discount['date_end']) . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_special'])) {
foreach ($data['product_special'] as $product_special) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_special['customer_group_id'] . "', priority = '" . (int)$product_special['priority'] . "', price = '" . (float)$product_special['price'] . "', date_start = '" . $this->db->escape($product_special['date_start']) . "', date_end = '" . $this->db->escape($product_special['date_end']) . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_image'])) {
foreach ($data['product_image'] as $product_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($product_image['image']) . "', sort_order = '" . (int)$product_image['sort_order'] . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_download'])) {
foreach ($data['product_download'] as $download_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_category'])) {
foreach ($data['product_category'] as $category_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_filter WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_filter'])) {
foreach ($data['product_filter'] as $filter_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_filter SET product_id = '" . (int)$product_id . "', filter_id = '" . (int)$filter_id . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");
if (isset($data['product_related'])) {
foreach ($data['product_related'] as $related_id) {
$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_reward'])) {
foreach ($data['product_reward'] as $customer_group_id => $value) {
if ((int)$value['points'] > 0) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_reward SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$customer_group_id . "', points = '" . (int)$value['points'] . "'");
}
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "'");
if (isset($data['product_layout'])) {
foreach ($data['product_layout'] as $store_id => $layout_id) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_layout SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout_id . "'");
}
}
$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id . "'");
if ($data['keyword']) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($this->accents($data['keyword'])) . "'");
}
$this->db->query("DELETE FROM `" . DB_PREFIX . "product_recurring` WHERE product_id = " . (int)$product_id);
if (isset($data['product_recurring'])) {
foreach ($data['product_recurring'] as $product_recurring) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "product_recurring` SET `product_id` = " . (int)$product_id . ", customer_group_id = " . (int)$product_recurring['customer_group_id'] . ", `recurring_id` = " . (int)$product_recurring['recurring_id']);
}
}
$this->cache->delete('product');
$this->event->trigger('post.admin.product.edit', $product_id);
}
public function copyProduct($product_id) {
$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
if ($query->num_rows) {
$data = $query->row;
$data['sku'] = '';
$data['upc'] = '';
$data['viewed'] = '0';
$data['keyword'] = '';
$data['status'] = '0';
$data['product_attribute'] = $this->getProductAttributes($product_id);
$data['product_description'] = $this->getProductDescriptions($product_id);
$data['product_discount'] = $this->getProductDiscounts($product_id);
$data['product_filter'] = $this->getProductFilters($product_id);
$data['product_image'] = $this->getProductImages($product_id);
$data['product_option'] = $this->getProductOptions($product_id);
$data['product_related'] = $this->getProductRelated($product_id);
$data['product_reward'] = $this->getProductRewards($product_id);
$data['product_special'] = $this->getProductSpecials($product_id);
$data['product_category'] = $this->getProductCategories($product_id);
$data['product_download'] = $this->getProductDownloads($product_id);
$data['product_layout'] = $this->getProductLayouts($product_id);
$data['product_store'] = $this->getProductStores($product_id);
$data['product_recurrings'] = $this->getRecurrings($product_id);
$this->addProduct($data);
}
}
Since the insert query happens in the model you can call it by adding the function as a class method somewhere in the model file product.php:
public function accents($string) {
//search
$dia = array('á', 'ä', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ĺ', 'ň', 'ó', 'ô', 'ŕ', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Ä', 'Č', 'Ď', 'É', 'Í', 'Ľ', 'Ĺ', 'Ň', 'Ó', 'Ô', 'Ř', 'Š', 'Ť', 'Ú', 'Ý', 'Ž', ' ','\'','%');
//replace
$nodia = array('a', 'a', 'c', 'd', 'e', 'e', 'i', 'l', 'l', 'n', 'o', 'o', 'r', 'r', 's', 't', 'u', 'u', 'y', 'z', 'A', 'A', 'C', 'D', 'E', 'I', 'L', 'L', 'N', 'O', 'O', 'R', 'S', 'T', 'U', 'Y', 'Z', '-','','');
return str_replace($dia, $nodia, $string);
}
And then call it in the insert like this:
$this->db->escape($this->accents($data['keyword']))
Of course if you want to apply that preg_replace() as well this can get messy and you might want to think about formatting it before the insert in steps for clarity. You could add a block like this and then just leave the query alone - which I might add has the added benefit of not breaking extensions and vQmods which might depend on the default code syntax:
$data['keyword'] = $this->accents($data['keyword'];
$data['keyword'] = preg_replace('/[^a-zA-Z0-9]+/', '-', $data['keyword']);
$data['keyword'] = trim($data['keyword'], '-');
$data['keyword'] = strtolower($data['keyword']);
You could also try just converting to ascii with iconv() like this:
$this->db->escape(iconv("UTF-8","ASCII//TRANSLIT",$data['keyword']))
I've used the above function for some webservices calls that require ascii and it works for me.

Prestashop 1.6 Create standalone php file to list products

I'm trying to create a PHP File in order to display all my products in a XML format.
The PHP file is in the ROOT dir of the PS installation.
Could you please telle how to initialise PS without it adding html and body tags to the pages ?
I also want to get the PublicPrice et the PriceWithoutReduction of the products.
When I try this code I get a Fatal Error.
The URL : http://www.topludo.fr/xml_guide.php
Thanks for advance.
Pierre.
<?php
ini_set('display_errors', 'on');
header("Content-Type:application/xml");
include(dirname(__FILE__) . '/config/config.inc.php');
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);
echo '<?xml version="1.0" encoding="UTF-8"?>' . chr(10);
echo '<catalogue lang="FR" date="' . date('Y-m-d H:i') . '" GMT="+1" version="2.0">' . chr(10);
$id_lang = 1;
$front = false;
// Requête identifiant les produits disponibles dans le catalogue
echo $sql = 'SELECT p.*, pl.* , m.`name` AS manufacturer_name FROM `'._DB_PREFIX_.'product` p '.
' LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`)'.
' LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)'.
' WHERE pl.`id_lang` = '.(int)$id_lang.' ORDER BY name ASC';
$rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
foreach ($rq as &$row)
$row = Product::getTaxesInformations($row);
$products_query = $rq;
$product_num = 0;
foreach($products_query as $key => $products) {
$product = new Product($products['id_product']);
$product_num++;
$categories = Product::getProductCategoriesFull($product->id, $id_lang);
$category = end($categories);
//$reducPrice = Product::getPriceStatic($products['id_product'], true, null, 0);
echo $product->getPublicPrice();
$fullPrice = ceil($product->getPriceWithoutReduct());
if ($fullPrice < $reducPrice) {
$discount_price = '';
$regular_price = $fullPrice;
$sale = 0;
} else {
$discount_price = $reducPrice;
$regular_price = $fullPrice;
$sale = 2;
}
$cover_id = $product->getCover($product->id); // L'image du produit
$link = new Link();
$cover_image_link = $link->getImageLink('large-default', $cover_id['id_image']);
$sp = SpecificPrice::getByProductId($product->id); // Pour les dates de reduc
$manufacturer = Manufacturer::getNameById($product->id_manufacturer); // Le fabricant
echo '<product place="' . $product_num . '">' . "\n";
echo '<merchant_category><![CDATA[' . $category['name'] . ']]></merchant_category>' . chr(10);
echo '<offer_id><![CDATA[' . $product->id . ']]></offer_id>' . chr(10);
echo '<name><![CDATA[' . $product->name[1] . ']]></name>' . chr(10);
echo '<description><![CDATA[' . substr(strip_tags(str_replace(array('<BR>', '<br>'), "</P>\n<P>", html_entity_decode($products['description']))), 0, 245) . '...]]></description>' . chr(10);
echo '<regular_price currency="EUR">' . $regular_price . '</regular_price>' . chr(10);
echo '<product_url><![CDATA[' . $product->getLink() . ']]></product_url>' . chr(10);
echo '<image_url><![CDATA[' . $cover_image_link . ']]></image_url>' . chr(10);
echo '<discount_price currency="EUR">' . $discount_price . '</discount_price>' . chr(10);
echo '<price_discounted_from><![CDATA[' . substr($sp[0]['from'], 0, 16) . ']]></price_discounted_from>' . chr(10);
echo '<price_discounted_until><![CDATA[' . substr($sp[0]['to'], 0, 16) . ']]></price_discounted_until>' . chr(10);
echo '<sales>' . $sale . '</sales>' . chr(10);
echo '<delivery currency="EUR">FR;0;</delivery>' . chr(10);
echo '<manufacter>'.''.'</manufacter>' . chr(10);
echo '<brand><![CDATA[' . $manufacturer . ']]></brand>' . chr(10);
echo '<model_number><![CDATA[' . $product->reference . ']]></model_number>' . chr(10);
echo '<manufacturer_product_id><![CDATA[]]></manufacturer_product_id>' . chr(10);
echo '<ean13>'.$product->ean13.'</ean13>' . chr(10);
echo '<guarantee unit="year">1</guarantee>' . chr(10);
echo '<used>0</used>' . chr(10);
echo '<used_condition><![CDATA[]]></used_condition>' . chr(10);
echo '<update_date><![CDATA[' . substr($product->date_upd, 0, 16) . ']]></update_date>' . chr(10);
echo '<promo_text><![CDATA[]]></promo_text>' . chr(10);
echo '<offer_valid_from><![CDATA[' . substr($sp[0]['from'], 0, 16) . ']]></offer_valid_from>' . chr(10);
echo '<offer_valid_until><![CDATA[' . substr($sp[0]['to'], 0, 16) . ']]></offer_valid_until>' . chr(10);
echo '<size unit="cm"></size>' . chr(10);
echo '<weight unit="kg">0.00</weight>' . chr(10);
echo '<color><![CDATA[]]></color>' . chr(10);
echo '</product>';
flush();
}
echo '</catalogue>';
To initialize PrestaShop use:
require_once __DIR__.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.inc.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'init.php';
Instead of using header to set your content type to xml and then echo your tags, I recommend using SimpleXML. It's much easier to manipulate your XML and it will result in easier to read code. After you create your SimpleXMLElement and add to it all your product properties as children, you can use asXML method to get the XML string or you can save the XML to a file by passing a file name argument to asXML.

How to add an orderby to a typo3 flow queryBuilder query?

This is my code, all I did to the exeisting working code is add the orderby:
$queryBuilder->select('pa1')
->from('\SeeThroughWeb\Shop\Domain\Model\ProductArticle', 'pa1')
->join('pa1.productPrices', 'pp1')
->join('pa1.product', 'p')
->where('pp1.salePrice IN (' . $subQueryBuilder . ') AND pa1.status = ' . \SeeThroughWeb\Shop\Domain\Model\ProductArticle::STATUS_ACTIVE . ' AND (pa1.stock > 0 OR pa1.displayOutOfStock = 1) AND p.status = ' . \SeeThroughWeb\Shop\Domain\Model\Product::STATUS_ACTIVE . ' AND p.isFeatured = 1 AND p.deleted = 0')
->groupBy('p')
->orderBy('p.isgiftcard', 'ASC');
$result = $query->execute();
doesn't seem to work, it gives me the exception:
MetaDataController.php line 176
What am I doing wrong?