Prestashop: Get id of parent category - smarty

I've faced the problem while coding my very first prestashop template. So what I want to do is to get the list of all sub categories which are under the same parent category. So running foreach loop I need to send parent category id.
In the internet there's not so much information about prestas' coding, modules or stuff, but I found two possible solutions.
One was: {$product->id_category_default} but that works only inside a product page (?) Maybe I'm wrong and it's possible to get a solution with this?
Another way sounds bit desperate: {$cookie->last_visited_category}
problem with this one, that it works only if you come from parent category directly, but fails when followed by direct link or any other way.
Also I was thinking of writing php function, which sends MySQL query and returns parent category id, but is this optimal way to do? Also I haven't tried writing my own php functions in smarty, seems it woks in some different way than just calling function.
So to conclude, do anyone knows how to get parent category id without separate php function and if it's so desperate, where and how to define your own php functions for smarty and how to call them.
I'm working with prestashop 1.4.
Thank you for your attention.

I've used this code in a module, you might need to adapt it but basically, the id of the parent category is stored in the database.
$parentCategoryList = array(2, 3, 4, 5);
if(!in_array($id_category, $parentCategoryList)){
$parentCategory = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT DISTINCT c.id_parent
FROM '._DB_PREFIX_.'category c
WHERE c.id_category = '.(int)($id_category)
);
$id_category_parent = $parentCategory[0]['id_parent'];
}
else{
$id_category_parent = $id_category;
}
In my code, the $parentCategoryList stores the id of my main categories (change it to your values). Then it checks if the category you're viewing ($id_category) is a main category. If not, it looks for the parent category in the database.
I am not really happy about the use of an array to store manually the main categories, but it works.
Hope this helps!

Related

Magento - Get attribute options value and quantity

Hello and good day to all the members of this great community. I'm still new in PHP and especially in Magento.
I'm not posting, waiting for answers, and leaving without replying back. This is a learning process. I hope to get a great support from all of you.
I have a product. I did create custom option for the product, that is an attribute named "a_size". The attribute has value of S, M and L. Each of the value has quantity.
In the single product view, I would like to call all the available size. That is the size (S, M, or L) that has quantity more than 0. I just want to show the available size, not how much the size left.
Can anybody guide me? I'm using Magento 1.7.x and as far for this 2 weeks, I did try pretty many of suggested answers from the community thru the search function.
The replies will be much appreciated. Thank you.
There are a few things to try.
Firstly check that when you set up your new attribute in the Magento Admin (Catalog->Attributes->Manage Attribute) that in the Frontend Properties box you have set Visible on Product View Page on Front-end to yes.
To get size values I use this code:
$cabac_sizeAttribute = $_product->getAttributeText("a_size");
but I have other code for getting attribute values that goes like this:
$_product_helper = Mage::helper('catalog/output');
$temp = $_product_helper->productAttribute($_product, $_product->getASize(), 'a_size');
I think it is related to the type of attribute: text, dropdown, multiselect etc so try both and see how you get on. But really the function productAttribute() is just applying formatting. You can read the function in the file app/core/Mage/Catalog/Helper/Output.php
Also, I wonder, if you have set up a configurable product and you are on the product view page then you will be viewing the configurable product. That product won't have an a_size value: you are trying to access the a_size attribute of the simple products that make up the configurable product, yes? Everything I wrote above is (I think) correct but to get the attribute of the simple products that are part of a configured product you should study the code in the function getJsonConfig() of the file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
And in particular to these lines:
//file: file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
//class: Mage_Catalog_Block_Product_View_Type_Configurable
//function: getJsonConfig()
foreach ($this->getAllowProducts() as $product) {
$productId = $product->getId();
foreach ($this->getAllowAttributes() as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());
Being careful about variable naming: $product is local here, I suggest changing it, and about $this - but if you are in a .phtml of the product view for configurables then I think your $this is already Mage_Catalog_Block_Product_View_Type_Configurable
Welcome to Magento coding. You are doing well; it is a long but rewarding path. (hints: local.xml is your vital friend and so is Alan Storm if you haven't come across his content yet.)
[Additionally, (welcome to Magento) I think you are trying to say eg S and L are out of stock and M is in stock but actually the function getAllowProducts() will disallow a product with zero stock and exclude it from the returned object. You will need to use
$allProducts = $this->getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->getProduct());
(taken from function getAllowProducts() in file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php)
and then, if needed, check that each product is allowed to be shown eg status=ENABLED, and then check its stock level...
]
Malachy.
If you want to get the values of your drop down attribute use the following code
$_product->getASize();
and initially load the product object

Magento store view redirects not working in 1.8.0.0

I have a store I'm developing that has two store views, one for each language I'm supporting. The products and categories have localized url keys.
In 1.7.0.2 my language switcher would redirect correctly (for ones that had "Create Permanent Redirect" as well as ones with out) but after upgrading to 1.8.0.0, it did not work any more.
Problem
There is a regression in 1.8.0.0, they updated Mage_Core_Controller_Varien_Front to use ::_getRequestRewriteController() in ::dispatch() previously it used Mage_Core_Model_Url_Rewrite and now it uses Mage_Core_Model_Url_Rewrite_Request
On line 143 of app/code/core/Mage/Core/Model/Url/Rewrite/Request.php it has $stores = $this->_app->getStores(); which returns an array of store_id index stores, but it goes on to check if the store it goes on to check with $fromStore, which is a variable from the GET params, ___from_store, which contains the store views key (e.g. default, french, etc), obviously this has problems looking up using a alpha key in a numericly index array.
Solution
The offending line is again, #143 in app/code/core/Mage/Core/Model/Url/Rewrite/Request.php, by simpling changing it to $stores = $this->_app->getStores(false, true) it will work, as the 2nd parameter being true (defaults to false) returns the array with store view codes as the array key.
Hope this helps someone, and if anyone knows the best way to pass this on to the Magento developers, I would appreciate it, as I said, the issue reporting just posted to the home page, which doesn't seem to work.
Edit: I would also like to point out as 1.8.1.0 this is still an issue.

Request Variables in Joomla

I am new to Joomla. I am trying to write a module which would fetch and display the contents from database.
Now for fetching the contents from database, I need to get the current article id and category id of the current article.
I am able to get the id with article title append using JRequest::getVar('id');
ex : 16:abc-def-article-title.
I can sub-string the id out of it but I think there would be a better place of achieving it ?
And also I need to get the category id as well.
Also please let me know how can I see what all variables does JRequest::getVar(') holds.
Many Thanks !!
First, try extract article id by method getInt():
JRequest::getInt('id', 0);
It's more safely, then getVar().
Then, to get category id, you can use this code in module file:
jimport('joomla.application.component.model');
$articlesModel = JModel::getInstance('ContentModelArticle');
$articleId = JRequest::getInt('id', 0);
$article = $articlesModel->getItem($articleId);
$categoryId = $article->catid;
Using Joomla! built-in models instead direct SQL queries is best way to understanding Joomla architecture and writing safety applications that will be easily maintained in the future.
Note: code samples are actually only for Joomla 2.5 (probably and for 1.6, 1.7, but i don't shure).

Getting Popular Searches In Magento

I want to display the top 10 searches on my Magento store. Magento already stores searches made on the store under Admin > Catalog > Search Terms, so it's just a matter of getting it into my view. Does anyone know which helper or function I can access to get this list?
You have a setPopularQueryFilter method in the class Mage_CatalogSearch_Model_Mysql4_Query_Collection, after that you only have to set a limit I guess :)
Without trying, it should be something like that :
$searchCollectino=Mage::getModel('catalogsearch/query')->getCollection()
->setPopularQueryFilter()
->setPageSize($limit);
If anyone needs the Magento 2 solution:
inject the class \Magento\Search\Model\Query in your constructor and then retrieve the collection like so:
$collection = $this->query->getSuggestCollection()
This will return a collection of most popular search terms sorted by the popularity.

CakePHP, organize site structure around groups

So, I'm not quite sure how I should structure this in CakePHP to work correctly in the proper MVC form.
Let's, for argument sake, say I have the following data structure which are related in various ways:
Team
Task
Equipment
This is generally how sites are and is quite easy to structure and make in Cake. For example, I would have the a model, controller and view for each item set.
My problem (and I'm sure countless others have had it and already solved it) is that I have a level above the item sets. So, for example:
Department
Team
Task
Equipment
Department
Team
Task
Equipment
Department
Team
Task
Equipment
In my site, I need the ability for someone to view the site at an individual group level as well as move to view it all together (ie, ignore the groups).
So, I have models, views and controls for Depart, Team, Task and Equipment.
How do I structure my site so that from the Department view, someone can select a Department then move around the site to the different views for Team/Task/Equipment showing only those that belong to that particular Department.
In this same format, is there a way to also move around ignoring the department associations?
Hopefully the following example URLs clarifies anything that was unclear:
// View items while disregarding which group-set record they belong to
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id
http://www.example.com/Departments
// View items as if only those associated with the selected group-set record exist
http://www.example.com/Department/HR/Team/action/id
http://www.example.com/Department/HR/Task/action/id
http://www.example.com/Department/HR/Equipment/action/id
Can I get the controllers to function in this manner? Is there someone to read so I can figure this out?
Thanks to those that read all this :)
I think I know what you're trying to do. Correct me if I'm wrong:
I built a project manager for myself in which I wanted the URLs to be more logical, so instead of using something like
http://domain.com/project/milestones/add/MyProjectName I could use
http://domain.com/project/MyProjectName/milestones/add
I added a custom route to the end (!important) of my routes so that it catches anything that's not already a route and treats it as a "variable route".
Router::connect('/project/:project/:controller/:action/*', array(), array('project' => '[a-zA-Z0-9\-]+'));
Whatever route you put means that you can't already (or ever) have a controller by that name, for that reason I consider it a good practice to use a singular word instead of a plural. (I have a Projects Controller, so I use "project" to avoid conflicting with it.)
Now, to access the :project parameter anywhere in my app, I use this function in my AppController:
function __currentProject(){
// Finding the current Project's Info
if(isset($this->params['project'])){
App::import('Model', 'Project');
$projectNames = new Project;
$projectNames->contain();
$projectInfo = $projectNames->find('first', array('conditions' => array('Project.slug' => $this->params['project'])));
$project_id = $projectInfo['Project']['id'];
$this->set('project_name_for_layout', $projectInfo['Project']['name']);
return $project_id;
}
}
And I utilize it in my other controllers:
function overview(){
$this->layout = 'project';
// Getting currentProject id from App Controller
$project_id = parent::__currentProject();
// Finding out what time it is and performing queries based on time.
$nowStamp = time();
$nowDate = date('Y-m-d H:i:s' , $nowStamp);
$twoWeeksFromNow = $nowDate + 1209600;
$lateMilestones = $this->Project->Milestone->find('all', array('conditions'=>array('Milestone.project_id' => $project_id, 'Milestone.complete'=> 0, 'Milestone.duedate <'=> $nowDate)));
$this->set(compact('lateMilestones'));
$currentProject = $this->Project->find('all', array('conditions'=>array('Project.slug' => $this->params['project'])));
$this->set(compact('currentProject'));
}
For your project you can try using a route like this at the end of your routes.php file:
Router::connect('/:groupname/:controller/:action/*', array(), array('groupname' => '[a-zA-Z0-9\-]+'));
// Notice I removed "/project" from the beginning. If you put the :groupname first, as I've done in the last example, then you only have one option for these custom url routes.
Then modify the other code to your needs.
If this is a public site, you may want to consider using named variables. This will allow you to define the group on the URL still, but without additional functionality requirements.
http://example.com/team/group:hr
http://example.com/team/action/group:hr/other:var
It may require custom routes too... but it should do the job.
http://book.cakephp.org/view/541/Named-parameters
http://book.cakephp.org/view/542/Defining-Routes
SESSIONS
Since web is stateless, you will need to use sessions (or cookies). The question you will need to ask yourself is how to reflect the selection (or not) of a specific department. It could be as simple as putting a drop down selection in the upper right that reflects ALL, HR, Sales, etc. When the drop down changes, it will set (or clear) the Group session variable.
As for the functionality in the controllers, you just check for the Session. If it is there, you limit the data by the select group. So you would use the same URLs, but the controller or model would manage how the data gets displayed.
// for all functionality use:
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id
You don't change the URL to accommodate for the functionality. That would be like using a different URL for every USER wanting to see their ADDRESS, PHONE NUMBER, or BILLING INFO. Where USER would be the group and ADDRESS, PHONE NUMBER< and BILLING INFO would be the item sets.
WITHOUT SESSIONS
The other option would be to put the Group filter on each page. So for example on Team/index view you would have a group drop down to filter the data. It would accomplish the same thing without having to set and clear session variables.
The conclusion is and the key thing to remember is that the functionality does not change nor does the URLs. The only thing that changes is that you will be working with filtered data sets.
Does that make sense?

Resources