Opencart event data missing identifier - php

Using opencart 3 and trying to make use of the events but I'm unable to find the identifier.
E.g trying to extend the returns form added an event:
$this->model_setting_event->addEvent('mail_account_return_after', 'catalog/model/account/return/addReturn/after', 'extension/module/return/returnAdd');
Controller:
class ControllerExtensionModuleReturn extends Controller {
public function returnAdd(&$route, &$args, &$output) {
print_r($args);
exit;
$args is missing the main return_id identifier:
Array
(
[0] => Array
(
[firstname] => Foo
[lastname] => Bar
[email] => test#gmail.com
[telephone] => 01234556789
[order_id] => 29
[date_ordered] => 2017-06-29
[product] => Canon EOS 5D
[model] => Product 3
[quantity] => 1
[return_reason_id] => 4
[opened] => 0
[comment] => Test comment
)
)
Tried with $this->db->getLastId() but this returns 0. Tested with other events and appears to be missing the main identifiers.
Where does opencart set what data is passed to the before/after events?

If I'm not mistaken you should be able to access the auto increment return_id in $output, which holds the output of method addReturn(), i.e., $this->db->getLastId();.
As to why calling getLastId() a second time from within your event doesn't work, that's a good question. I would assume there may be some other query happening in between - though by default I don't think there should be. Is it possible there is another trigger running a query before your event gets triggered?

Look in system/engine/loader.php at for the strings before and after. There are methods that do view, controller, configuration and language.

Related

Pulling two single variables from a Session Array

I have a session that is created and stored. On another page, I want to pull two variables from this session. One of the variables I want to echo as a welcome and the other I want to use in a query to update a mysql table. I just can't quite seem to figure out the correct wording as I have really not worked with sessions and someone else created the initial code. The session name is what is really giving me the trouble.
Here is the code for naming the session:
$directory = "http://$serverName/".$journalName['db'];
mysql_connect('server','user','pass');
mysql_select_db($journalName['db']);
ini_set('session.use_trans_sid', false);
ini_set('session.use_only_cookies', true);
session_name($journalName['db']);
session_set_cookie_params(6*60*60); // 6 hrs until login cookie expires
session_start();
It has the possibility of three different session names. If I do a print of the array (print_r($_SESSION);) here is what shows up:
Array ( [loggedin] => 1 [person] => 1 [account_id] => 1 [order_id] => [type] => individual [unlimited] => yes [status] => active [agreeterms] => no [bounced] => no [subscription_begin] => 0000-00-00 [subscription_end] => 0000-00-00 [contact_name] => Doe, Jane [email] => jane.doe#gmail.com [email_cc] => [institution] => IRA [net_addresses] => [department] => CELT [phone] => 123-456-9383 [address] => 303 S. Patterson Ave [city] => Butte [state] => ID [zip] => 12345 [country] => United States [tech_contact] => [tc_phone] => [tc_email] => [last_login] => 2014-12-15 13:20:24 [last_access] => 2012-03-28 13:45:23 [administrator] => yes [notes] => [last_reset] => 2011-11-19 13:36:59 [last_notified] => 2007-08-14 [combo] => no )
I want to pull the variable contact_name for display at the top of the page and I want to pull the variable email and use it to run and update query. Here is what I currently have to pull the contact_name:
<?php echo "<h2>Account Owner or Contact Name:" .$_SESSION[["contact_name"]]. ",</h2>"; ?>
It is not working at all. Sessions really have me flumoxed at the moment.
Is it possible that you just made a typo ? It seems $_SESSION[["contact_name"]] should be $_SESSION["contact_name"]. Without the extra pair of [] brackets.
And also add
session_start();
to the page, because if you don't the session will not be available
session_start — Start new or resume existing session.
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Searching through multiple fields in SphinxSearch (PHP)

I have the following code:
$this->api = new App_Other_SphinxSearch();
$this->api->SetServer($host, $port);
$this->api->SetConnectTimeout(1);
$this->api->SetArrayResult(true);
$results = $this->api->Query("#(title,content) test", 'members');
echo "<pre>";print_r($results);die;
According to their documentation, a syntax like #(field_1,field_2) query should return docs which match the string query in either field_1 or field_2.
The PHP SDK returns something entirely different:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => content
)
[attrs] => Array
(
[created] => 2
[content] => 7
)
[total] => 0
[total_found] => 0
[time] => 0.000
[words] => Array
(
[title] => Array
(
[docs] => 10
[hits] => 34
)
[content] => Array
(
[docs] => 34
[hits] => 139
)
[test] => Array
(
[docs] => 26
[hits] => 34
)
)
)
There is no matches key in the array, however it got some hits. I don't really understand why this is happening, especially because if I try the same query from the command line, everything works correctly.
Any help?
Edit: Querying like this works though: #* test. Not really what I want though, cause that searches in all fields.
[total_found] => 0 says there where no matches.
The words array, just tells you now many documents and how many times that word appears in ANY (and all) fields. (WITHOUT regard to your specific query)
Sphinx caches those word stats, which help it make quick sanity checks on queries. When the query runs it can end up not matching any documents (because only then does it apply the field level filters), even though the individual words are found.
That explains your misinpretation of the results, but not why getting the results you get.
You are entering a Extended Mode query (as evidenced by the link to the sphinx documentation) , but the sphinx API defaults to ALL query mode. Notice also that title and content are in the words array, so are being taken as plain keywords not as syntax.
So you need to include a
$this->api->SetMatchMode(SPH_MATCH_EXTENDED);
btw, #* test, works because the #* are simply ignored in ALL query mode.

How to retrieve custom attributes in Opencart?

I have defined some custom attributes and assigned them to products via the Opencart admin panel. I'm trying to retrieve these attributes upon checkout to adjust some shipping costs.
I'm currently working in the catalog/model/shipping/ups.php file in the getQuote function. I'm getting close with this:
print_r($this->cart->getProducts());
Which gives me the following product data:
Array
(
[59] => Array
(
[key] => 59
[product_id] => 59
[name] => My Product Name
[model] => ABC
[shipping] => 1
[image] => data/myproduct.jpg
[option] => Array
(
)
[download] => Array
(
)
[quantity] => 1
[minimum] => 1
[subtract] => 1
[stock] => 1
[price] => 29.99
[total] => 29.99
[reward] => 0
[points] => 0
[tax_class_id] => 0
[weight] => 3.75
[weight_class_id] => 5
[length] => 0.00
[width] => 0.00
[height] => 0.00
[length_class_id] => 3
)
)
However, no custom attributes are returned. I'm sure there is a nice way to pass the product ID to an attribute-getting function, but I can't find it. The Opencart docs are a little lacking for the development side and no luck on Google.
Usually I'd grep the hell out of the whole directory structure to find what I'm looking for, but shell access is disabled on this particular web host :(.
EDIT
I believe Attributes are a newer feature that is built into Opencart. Its like a custom field.
Anyway, on the admin side I went to Catalog > Attributes and created a custom attribute called "Shipping Box Type". Then, under a specific product I can set the attribute. See screenshot. My goal is to retrieve the value of "Shipping Box Type". Does that answer your question #Cleverbot? I'm sure I can do a custom db query to grab it, but theres got to be a built-in function to grab attributes?
Retrieve attributes for a given product_id
$this->load->model('catalog/product');
$attributes = $this->model_catalog_product->getProductAttributes(59);
print_r($attributes);
Output
Array
(
[0] => Array
(
[attribute_group_id] => 9
[name] => Shipping Fields
[attribute] => Array
(
[0] => Array
(
[attribute_id] => 16
[name] => Shipping Box Type
[text] => SM2
)
)
)
)
Taking it further, here is an example of looping through the products currently in the cart to see what attributes they have:
$this->load->model('catalog/product');
$cart_products = $this->cart->getProducts();
// get attributes for each product in the cart
foreach($cart_products as $product) {
$attributes = $this->model_catalog_product->getProductAttributes($product['product_id']);
print_r($attributes);
}
This is not the most intuitive feature in Opencart. First you have to go catalog>attributes>attribute groups and you have to create a group name that will be a title for your attribute. For this it might be "Shipping Specifications".
Then you need to go to catalog>attributes>attributes> and create a new attribute under "Shipping Specifications" named "Shipping Box Type".
Now you are ready to go to catalog>products and add the attribute to a product.
The catch is that it won't be displayed how you were hoping I think. It will show up under the specifications tab next to description on your products page. You have the easy option of changing "Specifications" to the Heading of your choice in catalog/view/*your_theme*/products/products.tpl or you can edit the same tpl file to change the container/format that the data output goes.

Paginate Nestoria Results using PHP

I'm using the Nestoria API to retrieve property results.
Everything is working quite well and one can return up to 50 properties using this method.
I would like to show 10 items at a time and allow for the user to paginate through them, but for some reason I'm having difficulty doing this.
The code snippet around the section that controls this is as follows:
$page = isset($_REQUEST["page"]) ? (int)$_REQUEST["page"] : 1;
$page = $page-1;
$pagination = new pagination;
$propertyResults = $pagination->generate($nestoria->decodedData->response->listings, 10);
foreach($propertyResults as $listing) {
//do stuff
}
A snippet of the data array would be:
Array
(
[0] => stdClass Object
(
[auction_date] =>
[property_type] => house
[summary] => Located in North Kingston a two double bedroom Victorian house presented in...
[title] => York Road, Kingston, KT2 - Reception
[updated_in_days] => 6.5
[updated_in_days_formatted] => this week
)
[1] => stdClass Object
(
[auction_date] =>
[property_type] => house
[summary] => Fine home was built about 50 years ago and enjoys one of the best locations...
[title] => Coombe Hill, KT2 - Conservatory
[updated_in_days] => 2.5
[updated_in_days_formatted] => this week
)
....
(sample cut down due to size of array elements)
Now I have been staring at this for way too long now and I've drawn a blank.
This code works correctly except if I try go to any other page other than 1, then the page doesn't finish loading, it just carries on until Firefox says: "The page isn't redirecting properly".
So basically, pagination is able to cut my data array up correctly, but is failing to "paginate" correctly.
Any help?
Turns out the problem with the redirect was actually an .htaccess issue which was using the $_GET["page"] variable and was therefore getting confused, so I renamed all references to the $_GET["page"] to $_GET["_page"] in this app.

CakePHP Form Helper and Datetime

I have used the form helper to create a date time selection and when I access the $this->data.
It looks like the following.
[Timetable] => Array
(
[event_id] => 133
[location_id] => 39
[start] => Array
(
[hour] => 09
[min] => 06
[day] => 11
[month] => 03
[year] => 2011
)
)
But I want this to look more like this...
[Timetable] => Array
(
[event_id] => 133
[location_id] => 39
[start] => 2011-03-11 09:06:00
)
Is there a way to transform it to this?
You can just rebuild the $this->data['Timetable']['start'] var in your controller like so:
$this->data['Timetable']['start'] = $this->data['Timetable']['start']['year']
.'-'.$this->data['Timetable']['start']['month']
.'-'.$this->data['Timetable']['start']['day']
.' '.$this->data['Timetable']['start']['hour']
.':'.$this->data['Timetable']['start']['min'];
Should work fine.
This is undocumented in the Cookbook, but the function the model uses to convert that array into a database-friendly format is Model::deconstruct($field, $data). You can use it as follows:
$startAsString = $this->Timetable->deconstruct(
'start', $this->data['Timetable']['start']
);
This solution has the benefit of not breaking the MVC abstraction by having your controller know anything about the structure of data submitted by a form or how the database stores it.
Since you're using the form helper to generate your code, you can't get it into a single index unless you change your form helper.
You're probably using
$this->Form->input('start')
Changing it to the below will make it come out the way you want.
$this->Form->input('start', array('type'=>'text'));
However, if you do this, you'll lose out on all the dropdowns that cake auto generates for you. Not a problem if you use a datepicker.

Categories