Prestashop 1.4.3 I need to get a database value and use it in MailAlerts module - database

I have asked this question on the Prestashop forum, but haven't had a reply as of yet.
I need to be able to add a sagepay code to the new order email used in mailalert module.
What I have is;
// Filling-in vars for email
$template = 'new_order';
$subject = $this->l('New order');
$spvtxc = Db::getInstance()->ExecuteS("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '$order->id_cart'");
...
$templateVars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
...
'{sagepay_no}' => $spvtxc,
...
);
Every time i test a transaction the $spvtxc returns 'ARRAY'.
I have tried;
$spvtxc = '5';
As Expected this returns 5 as the sagepay number, so I'm confident the variables are being called and added to the email.
And I tried;
$spvtxc = Db::getInstance()->ExecuteS("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '2'");
So this should set $spvtxc to the value that is definatly there (i manually added it in the database), but this still returned 'ARRAY'.
If anyone can point out what I have missed, it is greatly appriceated.

As I was only needing to return a single value, I should have used getValue function instead of ExecuteS.
$spvtxc = Db::getInstance()->getValue("SELECT vendortxcode FROM `"._DB_PREFIX_."sagepay_transactions` WHERE id_cart = '$order->id_cart'");
This returned the value.

Related

Laravel 7: Showing error while passing multiple variable in str_replace

I'm facing error while passing multiple variable in str_replace function.
Error: Argument 1 passed to Xenon\LaravelBDSms\SMS::shoot() must be of the type string, null given, called in
Message Body:
Hello #name#,
Total Amount Purchased : #total#
Previous Due: #previous_due#
Deposit: #deposit#
Total Due: #total_due#
Controller:
$id = 1;
$sms_settings = SmsSetting::findOrFail($id);
if($sms_settings->order_create == 1){
$name = $request->name;
$previous_due = $customer->due;
$deposit = $request->deposit;
$total = $request->total;
$total_due = $request->total_due;
$msgs = $sms_settings->order_create_sms;
$msg = str_replace(array('#name#', '#total#','#previous_due#','#deposit#','#total_due#'), array($name,$previous_due, $deposit, $total, $total_due), $msgs);
$send= SMS::shoot($request->mobile, $msg);
}
Shoot Function:
public function shoot(string $number, string $text)
{
$this->sender->setMobile($number);
$this->sender->setMessage($text);
return $this->sender->send();
}
Here I'm using a Laravel Package for sending SMS to mobile number. How can I pass multiple variable in str_replace?
$request->mobile is null, confirm if you are passing the same in the request. Thats why the error.
Also use $request->validated('mobile'), that is safer.
str_replace seems to be fine. Take a look at Example, but Look at examples again, it might break if characters are overlapping with other arguments
I think the variable $msgs = $sms_settings->order_create_sms; contain empty that's why str_replace couldn't replace the data that you given so
$msg = str_replace(array('#name#', '#total#','#previous_due#','#deposit#','#total_due#'), array($name,$previous_due, $deposit, $total, $total_due), $msgs); , will return null.
I recommend checking $msgs again.
$msgs = $sms_settings->order_create_sms;
Make sure $msgs is not null place is_null($msgs) condition before feeding to str_replace
check more about str_replace: https://www.php.net/manual/en/function.str-replace.php

Laravel 5.4 - Array returning NULL instead of a numeric value

Here's a problem that is bothering me for a while.
I have a service provider to pass data to all views, everytime the sidebar is rendered. like this:
`
public function boot()
{
$userrole = array (DB::table('users')->where('id','=', Auth::id())->value('role'));
$menucase1 = [3,4,9,10];
$menucase2 = [1,2,3,10];
$menucase3 = [1,3,4,9,10];
$menucase4 = [4,9];
$commondata = compact('userrole','menucase1','menucase2','menucase3','menucase4');
view()->share('commondata', $commondata);
View::composer('sidebar', function ($view) {
$userrole = array (DB::table('users')->where('id','=', Auth::id())->value('role'));
$menucase1 = [3,4,9,10];
$menucase2 = [1,2,3,10];
$menucase3 = [1,3,4,9,10];
$menucase4 = [4,9];
$commondata = compact('userrole','menucase1','menucase2','menucase3','menucase4');
$view->with('commondata', $commondata);
});
}`
Doing a {{ dd($commondata) }} returns the correct values for the menucase arrays, but NULL for the $userrole
If i declare the same $userrole variable in a controller and call the variable in the view, the received data is correct.
Why is this happening?
Thanks in advance
Can't understand what are you actually trying to do.
If you want get user role as array, you can using pluck method:
$userRole = User::where('id', Auth::id())->pluck('role')->toArray();
But for current user you can just get the role
$userRole = [Auth::user()->role];
UPD: you also can do it in view without any sharing
{{ Auth::user()->role }}
If your user has many roles from a different table, and you have the relationship defined, you could do
$userrole = Auth::user()->roles->pluck('name');
//will return all the roles names in an array
//Replace name by the actual column you want from 'roles' table.

Drupal 7 - Why wont my AJAX code work

I know there are lots of tutorials on loads of sites (I have read most of them in the last few hours) but I still can not get the following code to work. Can anyone see what I am doing wrong?
The second select box does not update when I change a value in the first one and I don't get the blue spinning ajax circle next to the first select box when I make a change.
$form['age_range'] = array(
'#type'=>'select',
'#title'=>'Age Range',
'#options'=>$age_ranges,
'#ajax'=>array(
'event'=>'change',
'callback'=>'childcare_observations_ajax_set_eyfs_category_list',
'wrapper'=>'eyfs_category_wrapper',
'method'=>'replace',
)
);
$form['eyfs_category_wrapper'] = array('#prefix'=>'<div class="eyfs_category_wrapper">','#suffix'=>'</div>');
$form['eyfs_category_wrapper']['eyfs_category'] = array(
'#type'=>'select',
'#title'=>'EYFS Category',
'#options'=>array(1=>'one',2=>'Two'),
);
return $form;
}
function childcare_observations_ajax_set_eyfs_category_list($form,&$form_state){
$eyfs_category_options = array();
$eyfs_category_options[0] = "We just changed the list values";
$eyfs_category_options[1] = "We also added a second option";
$form['eyfs_category_wrapper']['eyfs_category'] = array(
'#type'=>'select',
'#title'=>'EYFS Category',
'#options'=>$eyfs_category_options,
);
return $form['eyfs_category_wrapper'];
}
The wrapper should be the id of the element to replace. you have it as a class for eyfs_category_wrapper
plus I'm not sure if 'eyfs_category_wrapper' is a valid element, normally I use a field set, or just add a prefix and suffix to my target field.

Use twitchAPI with cake

since a few hours i'm trying to implement the twitchAPI in my cake projet. a long time ago i made this little script in basic php.
$channelName = "gamespot";
$json_file = #file_get_contents("http://api.justin.tv/api/stream/list.json?channel={$channelName}", 0, null, null);
$json_array = json_decode($json_file, true);
#$json_array[0] && $json_array[0]['name'] == "live_user_{$channelName}";
#$title = $json_array[0]['channel']['status'];
#$game = $json_array[0]['meta_game'];
#$chanel_view = $json_array[0]['channel_count'];
#$totalchanelview = $json_array[0]['channel_view_count'];
but i don't know how to add this lines on my controller
For know i've just find this
public function twitch() {
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=manvsgame');
$twitch = json_decode($json);
$totalchanelview = $twitch[0]['channel_view_count'];
$this->set('twitch', 'totalchanelview');
}
but of course i've this error
Fatal error: Cannot use object of type stdClass as array in /Users/*/Desktop/Websites/**/app/Controller/UsersController.php on line 29
anyone can explain to me how i can use this API?
thanks in advance and have a nice day/night :)
okey first thanks to help me. i still have a little "logic problem"
my function is something like that:
public function twitch() {
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=gamespot');
$twitch = json_decode($json, true);
$this->set('json', $twitch);
}
but know, what can I write to my view to see my informations (like the title of my stream for exemple.
I test with
echo $twitch[0]['title']; (it's my line 1)
bit i've this error
Notice (8): Undefined variable: twitch [APP/View/Users/admin_dashboard.ctp, line 1]
$twitch = json_decode($json, true); // add true param
$twitch[0]['channel_view_count'];
adding true returns the data as an associated array instead

reading and updating record and doing addition

I have a field called force. Its by default a null field. I want to add 1 everytime I run an if block. here is my code sample
if($somecondition){
$array = array();
$array[] = $this->Model->read(null, 1);
$array['force']++;
$this->Model->updateAll(array('Model.complete' => 1, 'Model.force' => $array['force']),array('Model.completed IS NULL'));
}
I am getting an error of undefined variable $array. Not sure why.
it seems you are very new to Cake (and maybe even PHP)
if($somecondition){
$array = $this->Model->read('force',1);
if($array['Model']['force']===NULL)$array['Model']['force'] = 0;
$array['Model']['force']++;
$array['Model']['complete']=1;
$this->Model->save($array);
}
It you can, change the default value of 'force' to 0 in the DB, so you don't have to check for that here.
You should initialize $array['force'] to 0 before incrementing it.

Resources