I'm actually using the Tools class in Prestashop 1.5 that allows me to display error messages to the front-end:
$this->errors[] = Tools::displayError( 'Fatal error!' );
Is there any function to display success messages the same way?
It seems that we can't use the l() function inside of an extended ModuleFrontController()
Any advice would be greatly appreciated.
The way you are displaying errors / success messages like below :
{if isset($success)}
<p class="success">{$success}</p>
{/if}
is good option. Please note that
Tools::displayError('Fatal error');
is not providing you any sort of styling for error messages, it just provides a way for translating errors at admin.
If you want your success messages should be translatable also then in your controller do it as followed :
$this->context->smarty->assign( 'success', 1 );
And then in your template file
{if isset($success)}
{l s='This is success message'}
{/if}
And if template file is in a module then use it as
{if isset($success)}
{l s='This is success message' mod='yourmodulename'}
{/if}
Hope this will help you.
Thank you
Found a solution but it's probably not the best:
Inside of my ModuleFrontController() class:
$this->context->smarty->assign( 'success', 'Success!' );
At the top of my module's template file:
{if isset($success)}
<p class="success">{$success}</p>
{/if}
It displays "Success!" as intented.
For success messages you can use:
$output = null;
$output .= $this->displayConfirmation('<message goes here!>');
For error messages you can use:
$output = null;
$output .= $this->displayError('<message goes here!>');
an in the end:
return $output;
or something like:
return $output.$this->displayForm();
It is works for me in PrestaShop 1.7
$this->errors[] = "Error message!";
$this->success[] = "Success message!";
Related
I have couple of text_area_field in my application which can be distinguished only by using the value inside it. How can I get a specific element using value?
Ex:
<div data-component="options">
<input data-component="text_area">Value 1</input>
<input data-component="text_area">Value 2</input>
<input data-component="text_area">Value 3</input>
<input data-component="text_area">Value 4</input>
I have tried
cy.get('[data-component="options"] [data-component="text_area"]').contains('Value 3').clear().type('Edited 3').
but it is giving the error :
Timed out retrying: Expected to find content: 'Value 3' within the element: but never did
Any help would be highly appreciated.
Check if the div allows typing. If yes, try the below code and see if that is working:
cy.get('[data-component="options"] [data-component="text_area"]').each(($div, i) => {
const valueText = Cypress.$($div).text();
console.log(valueText);
if(valueText === "Value 3") {
cy.wrap($div).clear().type("Edited 3");
}
})
You could also try using eq(), but if there are changes in selector with more options it may fail.
cy.get('[data-component="options"] > div').eq(2).then(($div)=>{
cy.wrap($div).contains("Value 3").clear().type("Edited 3");
})
You could possibly try this one too:
cy.get('[data-component="options"] > div').contains("Value 3").clear().type("Edited 3");
I think this will work .Try this code:
cy.get('[data-component="options"] > div').invoke('show').eq(2).then(($div)=>{
cy.wrap($div).contains("Value 3").clear().type("Edited 3");
})
Im struggling with custom module. I want to use $product variable in custom module template.
That works in every product page but not in category and home page gives me debug error
Notice: Undefined index: product
like
{$product.name}
When i wrap it like
{foreach from=$products item="product"}
{$product.name}
{/foreach}
then $product variables works but i want only to show variable matched to product.
Any ideas?
Ive implemented product functions from ps_specials with all needed interfaces like
$products = $this->getSpecialProducts();
$this->smarty->assign(
array(
'products' => $products,
));
return false;
Direct me how to get $product variable in home and category pages (in hooked custom template file from custom module in {hook h='displayProductListReviews' product=$product})
Edit.
My function looks right now
public function hookDisplayProductListReviews($params){
$templateFile = 'test.tpl';
$products = $this->getSpecialProducts($params['product']);
$this->context->smarty->assign(
array(
'products' => $products,
));
return $this->fetch('module:'.$this->name.'/'.$templateFile);
}
so far so good because with
{foreach from=$products item=product} {$product->name} {/foreach}
i can print $product->name but...
How to use it without foreach loop that print names of all products in gathered in function getSpecialProducts (from ps_specials module)?
You should do
public function hookDisplayProductListReviews($params){
$products = $this->getSpecialProducts($params['product']);
}
When you use {hook h='displayProductListReviews' product=$product})
product=$product will be in $params, you can use print_r($params);die(); to look what exactly is in that variable
I'm attempting to bind a HTML element which contains a string echoed via PHP so that I can use it with VueJS. Essentially what I am going to be doing is switching between GBP and USD depending on some php/mysql database queries (USD is default value). Here is a simplified example of what I have tried so far.
<div id="app">
<?php $string = 'GBP'; ?>
<!-- Hide this from the front end but bind to Vue somehow -->
<span v-el:currency style="display: none;"><?php echo $string; ?></span>
<p>Payment currency: {{ currency }}</p>
</div>
Of course I could just echo the php variable again, but the main reason I want to bind it to a VueJS element is so I can use the value of this element in my JS to do something like this...
if (this.currency === 'GBP') {
return "Paying in GBP";
} else {
return "Paying in USD";
}
Worth noting that I already have a fair bit of VueJS working within this #app so it's nothing to do with the configuration of Vue being wrong, more a case of just not knowing the correct way to approach the issue.
I would not interleave PHP and javascript inside a component.
Why don't you create a new script at the end with the variables you need?
<!-- bottom of the body -->
<script>var currency = <?php echo $yourVar; ?></script>
And then it will be a global variable and you just take it from there.
I started using Laravel a month ago. Trying to make a page with one form on it and I keep getting a "Controller method not found." error. There is only one form on the page and that method is being called fine and making the query that I want. It does redirect to the same page with that form, /dashboard/edit/module.
It would be awesome if I could see what method is not being found or where it is in my code. Any ideas how I can do this? Someone mentioned App::error or messing with Log::error in global.php but I can't get any more information shown.
Edit:
I found that the form action is only being found if I upload a video to the form. Other then that it gives me the same error. Here is the controller method of this form. The page with this form is /dashboard/edit/module and I would like to reload that page after this runs.
public function postSection(){
$path = public_path() . "/videos/";
$filename = uniqid() . '.mp4';
$file_path = $path . $filename;
$moduleID = Input::get('moduleID');
if(!Input::hasFile('video')){
return Redirect::back()->with('error', 'Must enter a valid video file.');
}else{
try {
Input::file('video')->move($path, $filename);
$section = New Section;
$section->module_id = $moduleID;
$section->video = $file_path;
$section->save();
//I was trying to use this instead of View::make to stop form resubmission
//return Redirect::to('/dashboard/edit/module')->with('moduleID', $moduleID);
return View::make('forms.editMod')->with('moduleID', $moduleID);
}catch ( Exception $e){
return Redirect::back()->with('error', 'Unable to save the video file.');
}
}
}
Edit: And my routes.php
//Route for the home page
Route::get('/', function(){ return View::make('home'); });
//Controllers
Route::controller('dashboard/edit', "ContentController");
Route::controller('user', "UserController");
Route::group(array('before'=> 'auth|dash'), function(){
//dashboard home
Route::get('/dashboard', function(){ return View::make('dashboard');});
//dashboard subs
Route::get('dashboard/modules', function(){ return View::make('forms.module');});
Route::get('dashboard/users', function(){ return View::make('users');});
Route::get('dashboard/reporting', function(){ return View::make('reporting');});
//user pages
Route::get('dashboard/register', function(){ return View::make('forms.register');});
Route::post('dashboard/edit/user', array('before'=>'csrfajax', function(){
$id = Input::get('userID');
$user = User::find($id);
return View::make('forms.editUser')->with('user', $user);
}));
});
And the form...
{{ Form::open(array('files' => true, 'action' => 'ContentController#postSection', 'id' => 'sectionform')) }}
<fieldset>
{{ Form::label('Video (mp4)') }}
{{ Form::hidden('moduleID', $module->id)}}
{{ Form::file('video', array('id' => 'video', 'class' => 'form-control')) }}<br>
{{ Form::submit('Save') }}
</fieldset>
{{ Form::close() }}
In routes.php, to reach this method you're using Route::controller, which results in RESTful routing. If the browser's HTTP request used the POST action, you'd correctly be routed to public function postSection(){}. But if the HTTP request used GET, which is the default for web browsing, especially when no data is being submitted by the browser, Laravel would be looking for a method called public function getSection(){}. This would explain why when you're uploading something (i.e. using POST), you reach the page, but otherwise you don't.
See the official documentation for the full low-down on implicit routing: http://laravel.com/docs/controllers#implicit-controllers
I am kind of lost on how to do this in my view.
I want to add the quantity. Right now, it adds but how do I get the input value into my ajaxlink?
My controller is using session to add.
<input id="quantity" type="text" value="1" class="span1">
<div id="cart-text">
<?php echo CHtml::ajaxLink(' Add ',
Yii::app()->createUrl('controller/basketAjax',array('id'=>$_GET['id'])),
array('success'=>'function(data){...
controller:
$session=new CHttpSession;
$session->open();
if (!isset(Yii::app()->session['cart']))
{
$quantity = 1;
$session->add('cart',array(
'product_id.'.$id=>array("id"=>$id,
'quantity'=>$quantity)
));
}
else
{
$cart = Yii::app()->session['cart'];
$array = $cart['product_id.'.$id];
if (isset($array)){
$array['quantity']=$array['quantity']+1;
} else {
$t = array('product_id.'.$id=>array("id"=>$id,'quantity'=>$quantity));
array_push($cart,$t);
}
$session->add('cart', $products);
}
you can do this by using keyup function of jquery. First set the id of link as "mylink". Compute the url by using createUrl.
<script type="text/javascript" >
$("#quantity").keyup(function(){
var url=<?php echo Yii::app()->createUrl('controller/basketAjax') ?>;
$("#mylink").attr("href","");
$("mylink").attr("href",url +$(this).val());
});
</script>
Now i explain whats happening above. first i catch the event keyup on input you are using. It will be called each time you press a key on input. Now url=<?php echo Yii::app()->createUrl('controller/basketAjax') ?>; this code is returning you the base url to your action without any parameters being passed.this line $("#mylink").attr("href",""); will set the href of your link to " "( means nothing). Now this line $("mylink").attr("href",url +$(this).val()); is appending the value of the input you are getting from the input.
Remember you have to put the separator in between like
$("mylink").attr("href",url+"/" +"id"+"/"+$(this).val());
Above i have assumed that the href in your case looks like "projectname/index.php/controller/action/id/something". Thats y i have used separators in between but you can customize it according to your needs.
ajaxLink will not work (from what I know), just build it the old fashion way with jquery. Your best solution is actually to put it in a form and submit the form. Something like
<?php
Yii::app()->clientScript->registerCoreScript('yiiactiveform');
$form=$this->beginWidget('CActiveForm', array('action'=>Yii::app()->createUrl('cart/addProduct')));?>
<input type="text" name="quantity" value="1">
<input type="hidden" name="Product_id" value="<?php echo $model->id;?>">
<?php echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('cart/addProduct','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data) {
if(data.status=="success"){
$("#formResult").html("form submitted successfully.");
}
else{
$.each(data, function(key, val) {
$("#user-form #"+key+"_em_").text(val);
$("#user-form #"+key+"_em_").show();
});
}
}',
)); ?>
<?php $this->endWidget(); ?><!-- .contact -->
Sorry for the indenting, hard to indent properly here.