hook_commerce_checkout_complete not called - drupal-7

I'm trying to execute some business logic after the checkout process in Drupal 7 with Drupal commerce module. I've read on the documentation that I can use the hook hook_commerce_checkout_complete but it's not called
function api_manager_commerce_checkout_complete($order) {
$ow = entity_metadata_wrapper('commerce_order', $order);
foreach ($ow->commerce_line_items as $line_item) {
$sku = $line_item->commerce_product->sku->value();
$record = array(
'uid' => get_user_id(),
'sku' => $sku,
'token' => uniqid(),
);
drupal_write_record('api_manager_product_user', $record);
}
}
For your information, I've disabled 'payment' and 'billing information' in the checkout configuration

Whenever a new hook is implemented in Drupal you are required to clear your cache ( class ). Only then would that particular hook be available and fired when invoked.
If you are using devel module, you can check out if your hook is recognized by the system by Drupal by using module_implements function. Devel module gives you a convenient tool at http://www.mysite.com/devel/php to try out such snippets.
dpm(module_implements('commerce_checkout_complete'));
If your module's name is not listed as the output of the above function then it means your hook is not recognized. As mentioned earlier please clear your cache in such case.

Related

Where to put Event listener to detect afterSave of plugin table (CakePHP)

I'm on CakePHP 3.3.16
I've put this in the main app's Bootstrap.php:
Log::write('info','foo');
$contactTable = \Cake\ORM\TableRegistry::get('Contacts.contacts');
$contactTable->eventManager()
->on('Model.afterSave', ['priority' => 1],function($event, $entity)
{
pr( "afterSave - " );
Log::write('info','afterSave');
});
I do get "foo" in the lofs, and yet when I save the plugin model in question, there's nothing in the logs. I've tried with and without priority.
Do I need to update CakePHP? I think a method has been depracated? The CakePHP event docs said to place this code in Bootstrap.php, so I'm confused...

integrate rollbar to my Cakephp project

i wan't to integrate Rollbar to my cakephp project but i dont know where to include the code referencing to rolbar in my app ?
i have use this code
<?php
use \Rollbar\Rollbar;
// Installs global error and exception handlers
$config = array(
// required
'access_token' => 'MY_ACCESS_TOKEN',
// optional - environment name
'environment' => 'production',
);
Rollbar::init($config);
But it works only for the page that i have added to, so please help how can i configure rollbar for cakephp application.
I should put this code in bootstrap.php
use \Rollbar\Rollbar;
// Installs global error and exception handlers
$config = array(
// required
'access_token' => 'ACCESS_TOKEN',
// optional - environment name
'environment' => 'production',
// optional - path to directory your code is in. Used for linking stack traces.
'root' => '/Users/brian/www/myapp'
);
Rollbar::init($config);

CakePHP 3.x use Plugin in a Plugin

Trying to use the Search Plugin from friendsofcake in my cakedc users plugin.
I used everthing like before (in my normal users/index.ctp it worked) and just added my custom index.ctp to the cakedc users controller, like so:
public function initialize()
{
parent::initialize();
$this->loadComponent('Search.Prg', [
'actions' => ['index']
]);
}
public function index()
{
$this->viewBuilder()->layout('backend');
$query = $this->Users
// Use the plugins 'search' custom finder and pass in the
// processed query params
->find('search', $this->Users->filterParams($this->request->query))
// You can add extra things to the query if you need to
->contain(['Skills'])
->where(['firstname IS NOT' => null]);
$this->set('users', $this->paginate($query));
}
But I am getting the error
Unknown method "filterParams"
Any ideas?
filterParams() was a method of the CakeDC search plugin. It's not available in the FoC plugin nor it still is in the CDC plugin. The FoC Search is not a drop in replacement but a completely different implementation. I've worked on both and I prefer FoC search because the code was written for Cake3 and is IMHO better than the other implementation that was "just" an upgrade from the Cake2 implementation.
I don't know from where you got filterParams() any way. I couldn't find it in the latest CDC docs nor the code. You might want to report it as a bug.

CakePHP: Reporting Failed Downloads with the Media View

I'm using CakePHP's Media view to force file downloads. My code is pretty much exactly like the example provided in the cookbook, which I'll paste here for your convenience:
<?php
class ExampleController extends AppController {
public function download () {
$this->viewClass = 'Media';
// Download app/outside_webroot_dir/example.zip
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip',
'path' => APP . 'outside_webroot_dir' . DS
);
$this->set($params);
}
}
In the database, I have a field that keeps track of how many times the file was downloaded. I'm looking for a way to make sure that this number is as accurate as possible, so if a user's download gets cancelled or times out, the number does not increment. Is there some way for CakePHP's Media view to report that the download was, indeed, successful?
Detecting when a file has finished downloading is no easy task. This is something that would be done on the client side with javascript, but browsers do not give you any hooks for that.
There is a pretty clever solution here (setting a cookie and then looking for it with javascript), but it only tells you when the download has started.

Cakephp - Having Issue when submitting form to plugin controller action

I am a 3+ years old in cakephp and facing a somewhat strange issue
with submitting a form to plugin controller's action (i am using
plugin first time). After trying different known things i am posting
this one.
Going straight into the matter here is the form in my "forum" plugin's search_controller.php's "index" view:
echo $form->create("Search", array('url'=>array('controller' =>
'search', 'action' => 'index','plugin'=>'forum'),
'id'=>'searchFormMain'));
<input type="text" name="data[Search][keyword]" style="width:357px; margin-left:9px;"><p><span id="searchButton"><input
type="image" src="/img/button_search.jpg" style="height:40px;width:
136px;border:0;" class="handcursor"></span></p>
</form>
As i am submitting this form to "index" action of search controller of
forum plugin, the following code does print nothing:
public function index($type='') {
if(!empty($this->data)) {
pr($this->data);
die;
}
}
While if i try the same code within beforeFilter of the same
controller i.e. search_controller.php it works well and prints as
follows:
Array
(
[Search] => Array
(
[keyword] => Hello Forum
)
)
And finally here is the beforeFilter code (of search_controller.php):
public function beforeFilter() {
parent::beforeFilter();
if(!empty($this->data)) {
pr($this->data);
}
}
Fyi, it does not matter if i comment out "parent::beforeFilter();" or
even disable $uses of my controller (if they look doubtful to you)
the result is same i.e. the control is not going in to "index" action
in the case of form submit while is working fine in the case of page
call. The url/action to page is http://localhost.rfdf.org/forum/search/index.
If i call the url directly it loads the form fine but when i submit it, it
never gets into the "index" action of the controller thus no view
rendered.
If i try the same set of code out of "forum" plugin environment i.e. in normal application it works just fine
I have been trying to find a way out of this for last 3+ hours now but
no success. I would appreciate any help in solving this puzzle.
I got it, finally!
It was Securty compontent dropping the request into the blackHole whenever it failed to find a security token with the form data. I learned that "Security" component "interferes" with $form->create() method and places a token as a hidden field with each $form->create() call. On the form submit, just after beforeFilter and right before getting into the controller "action" it checks for this token and simply dies everything on a validation failure. Unfortunately there is no error message or entry to cake log.
In my case i had been creating my own custom tag and not with the help of $form->create method so no token was being generated which lead to all the pain.
I resolved it by placing
$this->Security->validatePost = false;
at the end of beforeFilter.
Thanks everyone!
Have you tried putting an else into that if(!empty($this->data)) and doing a pr() as it could be that your post is not empty.
Either that or the format of your url array is not correct.
From ln759, http://api.cakephp.org/view_source/router/#line-757
$defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');
So I guess you need plugin first?
Are you using ACL or any of the like? In the beforeFilter, do a pr of the request. See which action is being requested to make sure that the request is correct

Resources