Call function/add own code after Order in Opencart - php

i'm working on Open Cart 3.0.2.0.
I want to call my own function after someone buy a product from my store. Where i can call this function? I tried to put the test code in catalog/model/checkout/order.php in addOrder and addOrderHistory methods, also in catalog/controller/chekout/confirm.php but it doesn't work.. Can anyone know in which file and method i should put my own code?
There is piece of my test code:
<?php
class ModelCheckoutOrder extends Model {
public function addOrder($data) {
//TEST
file_put_contents('my_directory/test.txt', 'test');
...
Edit: I know that in older versions was Confirm method i Checkout Model, and there u could pass your code, but it disappeared in new versions.
I can put my code in catalog/controller/success.php, but i need to get and pass informations about all ordered products. Any ideas?
Best regards.

I found the answer few days ago. The first option is get order id from session in "success.php" file and work with it.
Or (second option) you can edit files in the system/storage/modification !

Related

cake php calling a function, but the controller wont execute it

i'm from codeIgniter , but for an internship I have to debug an ongoing website using cakePHP.
I'm familiar with mvc but currently stuck and unable to find any solution even after searching.
What I want is to be able to call a function in a controller (easy task with codeIgniter) ,but during the debug I noticed that the controller was called, but not the function:
<?php
App::uses('AppController', 'Controller');
class FournisseursController extends AppController { //debug stop here
public $helpers = array('Html', 'Form');
public function index() {
$this->set('Personnes', $this->Fournisseur->find('all'));
}
public function addFournisseur() {
//contain some code , but not usefull for this problem
}
}
}
?>
after the 2 first line , the associated view is displayed (it's a weird concept to always have a view displayed, but I guess cake php work this way).
and debug dont even start on the targeted function.
I call it from a view:
<form action="../../Fournisseurs/addFournisseur" method="post">
<input type="submit">
</form>
I know it's not really good to call a controller from view , but in that case I need the user to enter some data to send it in a database, I dont know any other way.
I already read the doc concerning controller and it didnt help.
One last thing , if I change the name of the function in the controller , the controller will not be called : an error message explain the the method dont exist (so for me the controller know I want to call the function, because the controller need it to run).
it's been a day since i'm stuck on this and i'm pretty sure it's a stupid mistake.
thanks for reading this even if you dont answer , and sorry if I made any english mistake, it's my first post and i'm not a native speaker.
I was only relying on Xdebug ,despite the breakpoint it was not stopping in the function ,so I thought it was not called (the function and the code inside it), but with the help of the debug($data) function the breakpoint worked and I was able to keep debuging in the function.
I dont know why Xdebug acted this way ,but I will not rely only on it now.
thanks to everyone who helped, I can keep going now!
You can also use Form helper like below, where you can define the contoller and action name:
<?php echo $this->Form->create('Fournisseur', array('url' => array('controller' => 'Fournisseurs', 'action' => 'addFournisseur'))); ?>
// Form elements
<?php echo $this->Form->end(); ?>

Magento2 - setData() For Added Fields

I have added a field(key) in table(quote_item) through module installation(InstallSchema) and in Api Code I am using the following code to add value for that. But its not adding the value to that field in DB.
//cartItemObject \Magento\Quote\Model\Quote\Item
$cartItemObject->setData("key", "value");
Can anyone please help me on this?
Before making the setData, did you start the model? If you are using factory you should do as follows:
$cartItemObject=$this->model->create(); // after this you would have access to the following methods getData(), setData(), etc
In any case if it still does not work I advise you to review this information for handling model in magento 2

OpenCart: Get $this->session->data['order_id'] in module before ControllerCheckoutSuccess class unsets it

I am developing an OpenCart module and struggling with getting the order_id by using $this->session->data['order_id'].
My module is working fine if I comment out the part which unsets the order_id session variable in ControllerCheckoutSuccess class.
How can I ensure that I get the order_id before ControllerCheckoutSuccess unsets it?
Edit1: I need the order id to extract details like customer name, total amount, subtotal etc for that particular order. Is there some other method by which I can get the order id?
Edit2: Added this code in 'store/catalog/controller/module/myModule.php' in index() function. Using it to store order_id in temp session variable and use it on order success -
if (isset($this->session->data['temp_order_id'])
&& isset($this->request->get['route'])
&& $this->request->get['route'] == 'checkout/success')
{
// perform custom action
unset($this->session->data['temp_order_id']);
}
elseif (isset($this->session->data['order_id']))
{
$this->session->data['temp_order_id'] = $this->session->data['order_id'];
}
Please help!
In catalog/controller/checkout/success.php file, add:
$this->session->data['temp_order_id'] = $this->session->data['order_id'];
After:
if (isset($this->session->data['order_id'])) {
Then call your custom module or call your custom module before checkout success page controller is called.
Couldn't find any solution for this so sorted it by dynamically adding code (as mentioned by Sankar V) using vQmod. However, it adds a dependency that vQmod should be installed already.
Hope it helps someone facing similar problem.

wordpress update category function

I am looking for a function that will fire a php file when a category is updated. I don't really want to use a plugin, just want a function to go in functions.php. Can anyone point me in the right direction?
Thanks in advance.
Seems there was a function called "edited_category" but this seems to have been renamed to edited_{$taxonomy} .But I can find no documentation on how to use this. Anyone got any Ideas?
You can use edit_category hook
Runs when a category is updated/edited, including when a post or
blogroll link is added/deleted or its categories are updated (which
causes the count for the category to update). Action function
arguments: category ID.
You can add the hook in your functions.php file as given below
add_action ( 'edited_category', 'update_category_function');
function update_category_function( $category_id )
{
// ...
}

Get ID of the last product added to the cart in magento

I am trying to get the ID of the product that was most recently added to a user’s cart. A quick google search revealed this function
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
which is also used in Mage_Checkout_Block_Cart_Crosssell. When I try calling the function in my own controller however, it returns nothing.
I have tried to instantiate a core session via
Mage::getSingleton('core/session', array('name'=>'frontend'))
however this approach does not seem to work. I also tried to create the Crossell block and making the protected method that wraps around getLastAddedProductId function public however that returns null just like it does when I try calling it on its own.
Is there something I have to call or instantiate in order to use this function? Here’s my source listing for reference.
class Mymodule_Addcartaftermath_ItemaddedController extends Mage_Core_Controller_Front_Action {
public function generatemessageAction() {
$parameters = $this->getRequest()->getParams();
if (isset($parameters['ajax_call'])) {
$latest_item_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$response = array('response' => $latest_item_id);
echo json_encode($response);
} else {
$this->_redirect('/');
}
}
}
I tried poking through the source code, particularly the checkout/model/session.php file in the core and I cannot seem to find the definition of the function. I also looked at it’s parent’s class definition but could not find it there either.
If this method is not available to me is there another way of retrieving the most recent item added? I know that the items are added sequentially and I could perhaps just get the last item of the list of items from the cart however this would not work in the case where the user adds the same item to the cart essentially increasing the quantity rather than actual item itself (e.g. the user adds a laptop the cart when there already is one)
The call to
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
Is actually clearing the session variable after it is read. Magento uses magic methods extensively. In this case you are using the __call magic method which in turn uses the getData() method. In Mage_Core_Model_Session_Abstract_Varien you will see that they override the default behaviour of getData() to expect the second parameter to be a boolean (The first parameter to getData is the key name for the value you are looking for). That boolean is a flag telling the session to clear the variable after reading.
You could always listen for the checkout_cart_product_add_after event and add the item to your own variable in the session. That event is actually fired on the line before setLastAddedProductId() is called.
try to grep the variable you are looking for. As they are coming from magic methods then its hard to find the exact function you are after so it's easier to see the places where data gets set than where it is used
grep '>setLastAddedProductId' app/code -rsn
to see where the product id gets set to that session variable
app/code/core/Mage/Checkout/Model/Cart.php:255: $this->getCheckoutSession()->setLastAddedProductId($product->getId());
and then you can ask this variable (if it is set else empty())
Mage::getSingleton('checkout/session')->getLastAddedProductId();
and you can see all the things that are in checkout/session and verify if the data is there.
var_dump(Mage::getSingleton('checkout/session'));
Haven't a clue why it works this way but it works for me in Magento 1.6...
<?php
require_once ( "app/Mage.php" );
umask(0);
Mage::app("default");
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$lastadded = $session->getData("last_added_product_id");
print_r($lastadded);
Apparently you have to instantiate the core/session and then the checkout/session. I've tried it every other way but this is the only way I've found it to work. Perhaps someone can explain why it works this way. Hope this helps you out!

Categories