how to get all attributes in prestashop - php

I need to get all attributes that exists in my Prestahop store not matter wich or what product is assigned I just want to generate a array of those attributes and then use them in ProductController.php class. I check the file classes/Product.php and a method called getDefaultAttribute($id_product, $minimumQuantity = 0) is present but I think this only works for attributes assigned to a specific product and not return all as I need. Any help? I'm newbie with Prestashop and need to learn a lot of things

Examine file classes/Attribute.php. Your may use static function Attribute::getAttributes($id_lang, $not_null = false). It return all attributes for a given language.

You can get all attributes by using this SQL query:
$atbt = "SELECT a.id_attribute_group , a.name
FROM " . _DB_PREFIX_ . "attribute_group_lang a
where id_lang='".$id_lang."'";
$res = Db::getInstance()->ExecuteS($atbt);
$product_result = array();
$i=0;
foreach ($res as $key => $row) {
$sql = 'SELECT pal.name AS label,pal.id_attribute AS value,0 AS status'
. ' FROM ' . _DB_PREFIX_ . 'attribute AS pa
RIGHT JOIN ' . _DB_PREFIX_ . 'attribute_group_lang AS pagl ON (pagl.id_attribute_group =pa.id_attribute_group)
LEFT JOIN ' . _DB_PREFIX_ . 'attribute_lang AS pal ON (pal.id_attribute =pa.id_attribute)
JOIN ' . _DB_PREFIX_ . 'product_attribute_combination AS pacl ON (pacl.id_attribute =pal.id_attribute)
JOIN ' . _DB_PREFIX_ . 'product_attribute AS pat ON (pat.id_product_attribute =pacl.id_product_attribute)
JOIN ' . _DB_PREFIX_ . 'product AS prot ON (prot.id_product =pat.id_product)
WHERE pagl.id_lang="'.$id_lang.'" AND pal.id_lang="'.$id_lang.'" AND pagl.id_attribute_group=' . $row['id_attribute_group'].' AND prot.id_category_default = "'.$id_category.'" GROUP BY pacl.id_attribute';
$result = Db::getInstance()->ExecuteS($sql);
}

Related

Read value of product's custom options in custom script of Opencart 2.x

Is is possible to read variable value of custom option using key value pair?
I am trying to read option value for a product in custom function.I am able to read value by referring it with array index like below
$option = array_filter($this->request->post['option']);
$product_serial_no = $option['93'] ;
I get magic array index $option[93] by reading variable value in Neatbean debug mode.
But option index is changing for different products thus I want to read value of variable 'Serial Number' using something like associative array.
Is it possible?
The options on product page look like below image.
when you are viewing the product page and say you select some of the options and click "add to cart" the post will carry this array
$_POST['option'] = array(93 => '22222') (as you have presented in your example)
the 93 is the $product_option_id and it allows you to pull all the information you need with this request:
$product_id = $this->request->post['product_id'];
foreach ($this->request->post['option']) as $product_option_id => $value) {
$option_query = $this->db->query("SELECT
po.product_option_id,
po.option_id,
od.name,
o.type
FROM " . DB_PREFIX . "product_option po
LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id)
LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id)
WHERE po.product_option_id = '" . (int)$product_option_id . "'
AND po.product_id = '" . (int)$product_id . "'
AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_query->row);
}
This info should be sufficient enough to do any manipulations with options.
if the option has type of select or radio you may want to query also the possible variations like this
//... put this inside the foreach loop, listed above right after print_r($option_query->row);
if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio') {
$option_value_query = $this->db->query("SELECT
pov.option_value_id,
ovd.name,
pov.quantity,
pov.subtract,
pov.price,
pov.price_prefix,
pov.points,
pov.points_prefix,
pov.weight,
pov.weight_prefix
FROM " . DB_PREFIX . "product_option_value pov
LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id)
LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id)
WHERE pov.product_option_value_id = '" . (int)$value . "'
AND pov.product_option_id = '" . (int)$product_option_id . "'
AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_value_query->row);
}
And in case you have a checkbox (like in the example above)
//...also place this in the loop foreach in the code above.
if ($option_query->row['type'] == 'checkbox' && is_array($value)){
foreach ($value as $product_option_value_id) {
$option_value_query = $this->db->query("SELECT
pov.option_value_id,
pov.quantity,
pov.subtract,
pov.price,
pov.price_prefix,
pov.points,
pov.points_prefix,
pov.weight,
pov.weight_prefix,
ovd.name
FROM " . DB_PREFIX . "product_option_value pov
LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (pov.option_value_id = ovd.option_value_id)
WHERE pov.product_option_value_id = '" . (int)$product_option_value_id . "'
AND pov.product_option_id = '" . (int)$product_option_id . "'
AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_value_query->row);
}
}
You can view this exact code in system/library/cart/cart.php
hope this helps.

How can I ignore excluded images in this query?

I have this query, it is not written by me and I'm not very good at PHP and Magento. I have this client who has several images in some products, lets say, there are 14 images for one product but half of them are excluded and they don't want to show them. So my query returns all pictures for each product. You can see the code below:
$_mediaGalleryData = $_read->fetchAll('SELECT
main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
`value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
`default_value`.`position` AS `position_default`,
`default_value`.`disabled` AS `disabled_default`
FROM `catalog_product_entity_media_gallery` AS `main`
LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value`
ON main.value_id=value.value_id AND value.store_id=' . $store_id . '
LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value`
ON main.value_id=default_value.value_id AND default_value.store_id=0
WHERE (
AND main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC
');
I have added value.disabled=0 like this:
WHERE (
value.disabled = 0
AND main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
But it did not work either. Any ideas on how can I work that out?
EDIT: So I changed my code to following but still getting the same result;
$_mediaGalleryData = $_read->fetchAll('SELECT
main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
`value`.`label`, `value`.`position`, `value`.`disabled`, `value`.`label` AS `label_default`,
`value`.`position` AS `position_default`,
`value`.`disabled` AS `disabled_default`
FROM `catalog_product_entity_media_gallery` AS `main`
INNER JOIN `catalog_product_entity_media_gallery_value` AS `value`
ON main.value_id=value.value_id AND value.store_id=' . $store_id . '
WHERE (
value.disabled = 0
AND main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
ORDER BY IF(value.position IS NULL, value.position, value.position) ASC
');

OpenCart, display the total number of products

I'm using OpenCart ecommerce, and I would like to see in the index of ecommerce the total number of products
I do not have (for now) access to the db opencart, so to understand what is the structure of the db I have referred to this image
And this is an example of a query that I'm trying to use to display the total number of products (the result is obviously not what I expect)
//Test Count Product
//$query_test = $db->query("SELECT " . DB_PREFIX . "product_description.name FROM " . DB_PREFIX . "product INNER JOIN " . DB_PREFIX . "product_description ON " . DB_PREFIX . "product.product_id = " . DB_PREFIX . "product_description.product_id");
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test as $row) {
$count_test++;
}
echo $count_test;
Try this:
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product");
echo $query->row['total'];
Nicolo,
Your code is also correct. For that you need to use this code
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test->rows as $row) {
$count_test++;
}
echo $count_test;
But, I will recommend Monkeyman's way to get product_totals but with some modification (Monkeyman's code will NOT work for multiple stores)
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product_to_store WHERE store_id = '" . (int)$this->config->get('config_store_id') . "'");
echo $query->row['total'];

Opencart use Category as parent for store ( multi store system )

Hy all,
I'm working on an multi store website.
The main store is this. The second store ( same db, multi store ) is this with uid 4
I've got for the second store an categorie with multiple childs, with grand childs and so on...
My question is very simple, how can i make the categorie ( with uid 355 ) the parent categorie for that store instead of uid 0
I'll use VQMOD later, but i'm now inserting code directly in the files, for fast programming / debugging
The file that i'm changing is in: [root]/catalog/module/category.php
I've changed this code:
public function getCategories($parent_id = 0) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
return $query->rows;
}
To this:
public function getCategories($parent_id = 0) {
if( $this->config->get('config_store_id') == 4 && $parent_id == 0 ){
$parent_id = 355;
echo "test1";
}
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
return $query->rows;
}
And that kinda works. At the home page, the wrong categorie menu list is gone ( there's no menu anymore ), and at an categorie list page like this one there are the right categorie's on the left.
If i delete the && $parent_id == 0 part, it goes wrong at both the page's.
Does anybody got an clue of how to fix it? Any help would be apriciated. If you need more info, i will give it to you.
-ps- sorry for my bad english
Alright, after a lot of try's i searched the fora's of OpenCart better and came upon this thread:
OpenCart Fora Thread
The anser there is as follows:
There's a "top" flag you set to make it show up as a top level menu
So what i did is this:
I've edited the new main category ( with uid 355 ) and flagged it as top, that didn't helped, but i left it on.
Then i've edited all the direct childs of the main category and flagged them as top and foila, there they are. the problem was solved.
So with my edit to the main core, and the flagging of the categorie's, the problem is now solved

Opencart: Product Name Print Several times, How to fix.?

i had added the following Opencart module for my order report list...
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=3597&filter_search=order%20list%20filter%20model&page=4
I have problems with the column "Products". If there are more than one option the products name prints several times. So if I got a product with three options the product name prints three times. Is there any way to fix this problem?
i want print product name and model number only once, any idea.?
i will attach the results what i got now...
this is my sql query...
public function getOrders($data = array()) {
$sql = "select o.order_id,o.email,o.telephone,CONCAT(o.shipping_address_1, ' ', o.shipping_address_2) AS address,CONCAT(o.firstname, ' ', o.lastname) AS customer,o.payment_zone AS state,o.payment_address_2 AS block, o.payment_address_1 AS address,o.payment_postcode AS postcode,(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS status,o.payment_city AS city,GROUP_CONCAT(pd.name) AS pdtname,GROUP_CONCAT(op.model) AS model,o.date_added,sum(op.quantity) AS quantity,GROUP_CONCAT(opt.value ) AS options, GROUP_CONCAT(opt.order_product_id ) AS ordprdid,GROUP_CONCAT(op.order_product_id ) AS optprdid, GROUP_CONCAT(op.quantity) AS opquantity from `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op ON (op.order_id = o.order_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id = op.product_id and pd.language_id = '" . (int)$this->config->get('config_language_id') . "') LEFT JOIN " . DB_PREFIX . "order_option opt ON (opt.order_product_id = op.order_product_id) ";
Product Name = GROUP_CONCAT(pd.name) AS pdtname,
Add DISTINCT
GROUP_CONCAT(DISTINCT pd.name)

Categories