OpenCart theme development: Price as number? - opencart

I need to display some products differently depending on their price. I hoped that I could simply check the value of the $price variable from within the relevant theme file(s), but $price contains a currency formatted string. And because OpenCart supports a variety of currency formats, there's no simple, robust way of converting price strings back into numbers.
I've looked in the product controller class, ControllerProductProduct. So far as I can tell, OpenCart does not expose a numeric price value to views. I could modify the controller class, but I'd rather not because it would complicate updates.
Have I overlooked something? Is there no easy way to perform a numeric comparison on a price from within an OpenCart theme?

Looking at v1.4.9.4 in product.php (ControllerProductProduct) I can see the following code that sets the formatted value of $price that you're talking about:
if ($discount) {
$price = $this->currency->format($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = $this->currency->format($this->tax->calculate($result['price'],$result['tax_class_id'], $this->config->get('config_tax')));
Why don't you change this to be the following...
if ($discount) {
$price_num = $this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax'));
$price = $this->currency->format($price_num);
} else {
$price_num = $this->tax->calculate($result['price'],$result['tax_class_id'], $this->config->get('config_tax'));
$price = $this->currency->format($price_num);
And then a few lines down from this, you can then pass on this $price_num value to the template by adding the following:
$this->data['products'][] = array(
'product_id' => $result['product_id'],
...
'price' => $price,
'price_num' => $price_num,
...
Should do what you need

Unfortunately the answer is no, OpenCart does not expose numeric price values to themes. You will have to modify core files, which Brad explains how to do.

Related

CakePHP reading Cookie with multiple dots

I am using CakePHP to develop a website and currently struggling with cookie.
The problem is that when I write cookie with multiple dots,like,
$this->Cookie->write("Figure.1.id",$figureId);
$this->Cookie->write("Figure.1.name",$figureName);`
and then read, cakePHP doesn't return nested array but it returns,
array(
'1.id' => '82',
'1.name' => '1'
)
I expected something like
array(
(int) 1 => array(
'id'=>'82',
'name'=>'1'
)
)
Actually I didn't see the result for the first time when I read after I write them. But from second time, result was like that. Do you know what is going on?
I'm afraid it doesn't look as if multiple dots are supported. If you look at the read() method of the CookieComponent (http://api.cakephp.org/2.4/source-class-CookieComponent.html#256-289), you see this:
277: if (strpos($key, '.') !== false) {
278: $names = explode('.', $key, 2);
279: $key = $names[0];
280: }
and that explode() method is being told to explode the name of your cookie into a maximum of two parts around the dot.
You might be best serializing the data you want to store before saving and then deserializing after reading as shown here: http://abakalidis.blogspot.co.uk/2011/11/cakephp-storing-multi-dimentional.html

Language selection add products in multistore Opencart

Please help.
Operated multistore with multiple languages​​. One shop in English and a second store in the Czech language.
I need to add a product in the store 1Only in English and shop 2 only in Czech language.
By default OC is fill in the details in all languages ​​allowed. I do not.
Please give to modify the code as admin / controller / catalog / product.php validateForm?
Thank you for your help.
Sorry for my English
You have to write condition for your store_ids and their corresponding language id's with in the foreach loop in validateForm function:
foreach ($this->request->post['product_description'] as $language_id => $value) {
if(//condition to check Czech storeid and Czech language id){
....
//validate conditions
....
}else{
....
//engish validate conditions
....
}
}

CakePHP 1.3: Way to removed a specific string from a text - PHP Function or Regular Expression

I migrated a News database into a CakePHP news site I am creating. I have a problem with displaying the text from those migrated news because in the text that was imported to DB there were HTML tags that controls the text within them.
Could anyone help me find a way to remove these texts without compromising the layout of those same news?
Basically, I would like to accomplish the following:
Create a ONE-Time Use only function that I can include in my ArticlesController
For example the function name would be function fixtext(){...}
When I call this function from lets say http://mydomain.com/articles/fixtext, all the affected rows in the Article.body column would be scanned and fixed.
The section of text I want to remove is font-size: 12pt; line-height: 115%;, which in within the <span>...</span> tag.
I had something in mind like this, but I am not sure how to implement it
function fixtext(){
$this->autoRender = 'FALSE';
$articles = $this->Article->find(
'all',
array(
'fields' => array(
'Article.body',
'Article.id'
),
'recursive' => -1
)
);
foreach($articles as $article){
// Per Dunhamzzz suggestion
$text = str_replace('font-size: 12pt; line-height: 115%;', '', $article['Article']['body']);
$this->Article->id = $article['Article']['id'];
$this->Article->saveField('Article.body', $text);
}
$this->redirect('/');
}
I am not sure how to approach this, and what is the best way.
Firstly, I would personally create a shell to accomplish this as it is a batch job and (depending on the amount of records involved) you may hit Apache's request timeout limit. Also, it's a good (fun) learning experience and the shell can be extended to perform future maintenance tasks.
Secondly, it is a bad idea to parse HTML using (greedy) regular expressions due to the fact it may be malformed. It is safer to use an HTML parser or using simple string replacements instead but, if it is a small regular string that can be pattern matched safely (ie. your not trying to remove the closing </span> tags), regular expressions can work.
Something like this (untested):
// app/vendors/shells/article.php
<?php
/**
* Maintenance tasks for Articles
*/
class Article extends Shell {
/**
* Clean HTML in articles.
*/
public function cleanHtml(){
// safety kill switch (comment before running)
$this->quit('Backup the `articles` table before running this!');
// this query will time out if you have millions of records
$articles = $this->Article->find('all', array(
'fields' => array(
'Article.name',
'Article.body',
'Article.id'
),
'recursive' => -1,
));
// loop and do stuff
foreach ($articles as $article) {
$this->out('Processing ' . $article['Article']['name'] . ' ... ');
$article['Article']['body'] = $this->_removeInlineStyles($article['Article']['body']);
$this->Article->id = $article['Article']['id'];
$saved = $this->Article->saveField('body', $article['Article']['body']);
$status = ($saved) ? 'done' : 'fail';
$this->out($status);
}
}
/**
* Removes inline CSS styles added by naughty WYSIWYG editors (or pasting from Word!)
*/
protected function _removeInlineStyles($html) {
$html = preg_replace('/ style="[^"']+"/gi', '', $html);
return $html;
}
}
You can use a simple str_replace() to cut that piece of text out.
foreach($articles as $article){
$this->Article->saveField(
'Article.body' => str_replace('font-size: 12pt; line-height: 115%;', '', $article['Article']['body']),
'Article.id' => $article['Article']['id']
);
}
This is pending the text is the same in each case, otherwise you will need something a bit more complicated with regular expressions (or maybe multiple str_replace() calls to remove each bad property).

Show Joomla module only in specific articles?

Is it possible to show modules in Joomla only in a specific article (not per menu item), but in standard module position?
For example somehow get the current article id in a template and insert the modules with according id suffix in the name?
I would advise you not to hardcode things like this in the template. My question is, why don't you want to use a menu item? You can create a hidden menu item for that article and use it, then assign the module to that menu item.
If you still want to do it without using a menu item, a possible workaround would be to use something like "mod_php" (some module that allows you to use php code) and do something more or less like this:
Create the module and assign it to a position that is not used anywhere (you can type whatever you want in the module position)
In your php module, put this code:
$option = JRequest::getVar( 'option', '' );
$view = JRequest::getVar( 'view', '' );
$id = JRequest::getInt( 'id', 0 );
if ( $option == "com_content" && $view == "article" && $id == YOUR_ARTICLE_ID ) {
$module = JModuleHelper::getModule('your_module_type', 'module_title');
if ( ! empty( $module ) ) {
$attribs = array( 'style' => 'xhtml' );
echo JModuleHelper::renderModule( $module, $attribs );
}
}
I'm sorry if the code snippet is not showing properly, but I hope you can read it ok. Just one thing, when you fill in the part saying 'your_module_type', don't include the "mod_" part of the name. For example, if you want to output a module of type "mod_article_list", you should write "article_list" in "your_module_type".
I hope it helps!

How to create threaded list in CakePHP?

I create table: id, name, thread_id
The mainly thread has thread_id = 0, but their children has theard_id = id of parent, and how is the best and simplest solution to create list with children, and looks like:
Category 1
Product 1
Product 2
Category 2
Product 3
etc...
Maybe You have better solution for such a list?
Sorry, for my english:)
I suspect that the easiest way may be to use Cake's own TreeBehavior. More on that at http://book.cakephp.org/view/91/Tree. I've never used it personally, but have heard good things. It should provide all of the tools (and instruction) you need.
One of easiest and efficient is to use Tree behavior as proposed by kicaj-pl.
But I suggest you to consider MultiTree Behavior. It's also using nested tree database model but allows you to create many trees with different root_id and independent left and right values (so update of one tree doesn't update any other).
Ah! I found this sentence: When You use find('threaded') you have to field 'parent_id' for creating structure like tree...
All works fine!
Thanks for replies, bye!
Try the MPPT logic for It. For that you need 3 fields in your database table viz parent_id , lft , rght. And to implement it using CakePHP, CakePHP already provided the function for it for that please refer http://book.cakephp.org/view/228/Basic-Usage :)
1.First, your model must use "Tree" behaviour in model file (Modelname.php - in my case Post.php)
public $actsAs = array('Tree');
2.Next you need to retrieve threaded results and pass them to a view (ModelnamesController.php - in my case PostsController.php).
$posts = $this->Post->find('threaded');
$this->set('posts', $posts);
3.Finally, here's a template you can use, that renders infinitely threaded list for the above results
<div id="posts_navi">
<? function renderPosts($postsArray, $tmpModel){
//set return for the first time
if(!isset($return)){ $return = ""; }
$return .= '<ul>';
//create list
foreach ($postsArray as $post){
$return .= '<li>';
if($post['Post']['content'] != null){
$return .= $tmpModel->link($post['Post']['title'], array('action' => 'view', $post['Post']['id']),array('escape'=>false));
}else{
$return .= $post['Post']['title'];
}
//if post has children, go deeper
if(!empty($post['children'])){
$return .= renderPosts($post['children'], $tmpModel);
}
$return .= '</li>';
}
$return .= '</ul>';
return $return;
} ?>
<? $tmpModel = $this->Html; // we have to pass html helper inside, I am not sure it this is best way but it works
echo renderPosts($posts, $tmpModel); //finally, we render the $result returned. ?>
</div>