AbanteCart Community
AbanteCart Development => API Development => Topic started by: yonghan79 on April 29, 2021, 08:42:19 PM
-
Hi core devs,
I wonder if we can get/set the cust_data within the lib/cart.php via hook?
I want to add some info in add and buildProductDetails from admin side.
Thanks.
-
you should to find new instance defining of ACart class and recreate it with your additional data.
See https://github.com/abantecart/abantecart-src/blob/master/public_html/extensions/fast_checkout/storefront/controller/pages/checkout/fast_checkout.php#L53 as example.
Also do not forget you can replace ACart object inside Registry with your's own class that extends base ACart class inside init hook (for overriding some methods).
see https://github.com/abantecart/abantecart-src/blob/master/public_html/core/init.php#L524
Example:
public function onAHook_InitEnd(){
$registry = Registry::getInstance();
$session = $registry->get('session');
$session['cart']['some_custom_data'] = $some_data;
$registry->set('cart', new CustomACartClass($registry, $session));
}
-
Thanks abolabo, i will learn about them first. I will ask again if i don't understand.
I wonder if it's possible to change the protected cust_data become public cust_data in the cart.php?
-
Thanks abolabo, i will learn about them first. I will ask again if i don't understand.
I wonder if it's possible to change the protected cust_data become public cust_data in the cart.php?
see this https://github.com/abantecart/abantecart-src/blob/master/public_html/core/lib/cart.php#L88
cust_data becomes from constructor parameters. That's why i propose to reinitialize cart object inside registry.
-
Got it, i will do some more testing.
Another question. I see there is $this->registry->get('extensions')->hk_ProcessData($this, __FUNCTION__, ['total_text_id' => $extn]);
within the cart.php too.
How do i use it?
-
Got it, i will do some more testing.
Another question. I see there is $this->registry->get('extensions')->hk_ProcessData($this, __FUNCTION__, ['total_text_id' => $extn]);
within the cart.php too.
How do i use it?
no way. processData is a controller's call.
You should to override ACart with your class and override base methods
YourCart extends ACart ....
-
foreach ($total_extns as $extn) {
$sf_total_mdl = $this->load->model('total/'.$extn['key'], 'storefront');
/**
* parameters are references!!
*
* @var ModelTotalTotal $sf_total_mdl
*/
$sf_total_mdl->getTotal($total_data, $total, $taxes, $this->cust_data);
//trick to change data via hooks
$this->data = [
'total_key' => $extn['key'],
'total_data' => $total_data,
'total' => $total,
'taxes' => $taxes,
];
$this->registry->get('extensions')->hk_ProcessData($this, __FUNCTION__, ['total_text_id' => $extn]);
$total_data = $this->data['total_data'];
$total = $this->data['total'];
$taxes = $this->data['taxes'];
unset(
$this->data['total_key'],
$this->data['total_data'],
$this->data['total'],
$this->data['taxes']
);
$sf_total_mdl = null;
}
I see there is //trick to change data via hooks.
-
foreach ($total_extns as $extn) {
$sf_total_mdl = $this->load->model('total/'.$extn['key'], 'storefront');
/**
* parameters are references!!
*
* @var ModelTotalTotal $sf_total_mdl
*/
$sf_total_mdl->getTotal($total_data, $total, $taxes, $this->cust_data);
//trick to change data via hooks
$this->data = [
'total_key' => $extn['key'],
'total_data' => $total_data,
'total' => $total,
'taxes' => $taxes,
];
$this->registry->get('extensions')->hk_ProcessData($this, __FUNCTION__, ['total_text_id' => $extn]);
$total_data = $this->data['total_data'];
$total = $this->data['total'];
$taxes = $this->data['taxes'];
unset(
$this->data['total_key'],
$this->data['total_data'],
$this->data['total'],
$this->data['taxes']
);
$sf_total_mdl = null;
}
I see there is //trick to change data via hooks.
oh.. yes..
did you tried ?
public function onACart_ProcessData(){
}
-
I am testing the ACart_ProcessData but i don't know how to add data to that hook. What is the ['total_text_id' => $extn] used for?
Do you mind giving me an example?
-
I am testing the ACart_ProcessData but i don't know how to add data to that hook. What is the ['total_text_id' => $extn] used for?
Do you mind giving me an example?
public function onACart_ProcessData(){
$that = $this->baseObject;
//change $this->data property of ACart object is PUBLIC
// $that->data = ....your code here
}
-
Thanks abolabo.
about the ['total_text_id'=>$extn] ? What it is used for?
-
Thanks abolabo.
about the ['total_text_id'=>$extn] ? What it is used for?
see order_totals table in the database, column `key`.
Usually this key used for order recalculation process
-
Thanks abolabo. I will do some more testing to understand better.