Author Topic: Get cust data in lib/cart.php  (Read 421 times)

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Get cust data in lib/cart.php
« 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.

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1929
  • Karma: +295/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Get cust data in lib/cart.php
« Reply #1 on: April 30, 2021, 04:22:23 AM »
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

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Re: Get cust data in lib/cart.php
« Reply #2 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?

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1929
  • Karma: +295/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Get cust data in lib/cart.php
« Reply #3 on: April 30, 2021, 05:44:50 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

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Re: Get cust data in lib/cart.php
« Reply #4 on: April 30, 2021, 05:49:48 AM »
Got it, i will do some more testing.

Another question. I see there is
Code: [Select]
$this->registry->get('extensions')->hk_ProcessData($this, __FUNCTION__, ['total_text_id' => $extn]); within the cart.php too.

How do i use it?

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1929
  • Karma: +295/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Get cust data in lib/cart.php
« Reply #5 on: April 30, 2021, 05:53:28 AM »
Got it, i will do some more testing.

Another question. I see there is
Code: [Select]
$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

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Re: Get cust data in lib/cart.php
« Reply #6 on: April 30, 2021, 06:01:08 AM »
Code: [Select]
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.

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1929
  • Karma: +295/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Get cust data in lib/cart.php
« Reply #7 on: April 30, 2021, 06:03:47 AM »
Code: [Select]
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

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Re: Get cust data in lib/cart.php
« Reply #8 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?

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1929
  • Karma: +295/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Get cust data in lib/cart.php
« Reply #9 on: April 30, 2021, 06:14:55 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

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Re: Get cust data in lib/cart.php
« Reply #10 on: April 30, 2021, 09:07:07 AM »
Thanks abolabo.
about the ['total_text_id'=>$extn] ? What it is used for?

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1929
  • Karma: +295/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Get cust data in lib/cart.php
« Reply #11 on: April 30, 2021, 10:03:44 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

Offline yonghan79

  • Newbie
  • *
  • Posts: 31
  • Karma: +13/-0
    • View Profile
Re: Get cust data in lib/cart.php
« Reply #12 on: April 30, 2021, 08:51:26 PM »
Thanks abolabo. I will do some more testing to understand better.

 

Powered by SMFPacks Social Login Mod