News:

AbanteCart v1.4.2 is released.

Main Menu
support

Get cust data in lib/cart.php

Started by yonghan79, April 29, 2021, 08:42:19 PM

Previous topic - Next topic

yonghan79

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.

abolabo

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));
}




"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan79

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?

abolabo

Quote from: yonghan79 on April 30, 2021, 05:34:28 AM
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.
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan79

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?

abolabo

Quote from: yonghan79 on April 30, 2021, 05:49:48 AM
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 ....
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan79

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.

abolabo

Quote from: yonghan79 on April 30, 2021, 06:01:08 AM
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(){
}
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan79

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?

abolabo

Quote from: yonghan79 on April 30, 2021, 06:08:35 AM
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
}
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan79

Thanks abolabo.
about the ['total_text_id'=>$extn] ? What it is used for?

abolabo

Quote from: yonghan79 on April 30, 2021, 09:07:07 AM
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
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan79

Thanks abolabo. I will do some more testing to understand better.

Forum Rules Code of conduct
AbanteCart.com 2010 -