简体   繁体   中英

Access Woocommerce Session data

I'm trying to access a value in the Woocommerce Session Object , without any result.

Having this session in WC:

WC_Session_Handler Object
(
    [_cookie:protected] => wp_woocommerce_session_b520c2589cabce0f648a4494ecfed80e
    [_session_expiring:protected] => 1524665732
    [_session_expiration:protected] => 1524669332
    [_has_cookie:protected] => 1
    [_table:protected] => wp_woocommerce_sessions
    [_customer_id:protected] => 5449e37e9213b75fddd980f24c9b93d3
    [_data:protected] => Array
        (
            [cart] => a:0:{}
            [cart_totals] => a:15:{s:8:"subtotal";i:0;s:12:"subtotal_tax";i:0;s:14:"shipping_total";i:0;s:12:"shipping_tax";i:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";i:0;s:12:"discount_tax";i:0;s:19:"cart_contents_total";i:0;s:17:"cart_contents_tax";i:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";i:0;s:7:"fee_tax";i:0;s:9:"fee_taxes";a:0:{}s:5:"total";i:0;s:9:"total_tax";i:0;}
            [applied_coupons] => a:0:{}
            [coupon_discount_totals] => a:0:{}
            [coupon_discount_tax_totals] => a:0:{}
            [removed_cart_contents] => a:1:{s:32:"ee0d6c58f0f8345eefda7e85c449b783";a:10:{s:3:"key";s:32:"ee0d6c58f0f8345eefda7e85c449b783";s:10:"product_id";i:10758;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";s:1:"1";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:1:{i:13;d:7.1229508196721322832445366657339036464691162109375;}s:5:"total";a:1:{i:13;d:7.1229508196721322832445366657339036464691162109375;}}s:13:"line_subtotal";d:32.37704918032786594039862393401563167572021484375;s:17:"line_subtotal_tax";d:7.12000000000000010658141036401502788066864013671875;s:10:"line_total";d:32.37704918032786594039862393401563167572021484375;s:8:"line_tax";d:7.12000000000000010658141036401502788066864013671875;}}
            [customer] => a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:2:"BO";s:7:"country";s:2:"IT";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:2:"BO";s:16:"shipping_country";s:2:"IT";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:1:"1";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}
            [wc_notices] => 
            [shipping_for_package_0] => a:2:{s:12:"package_hash";s:40:"wc_ship_99615497844192df6a7ff67c2bfcf000";s:5:"rates";a:1:{s:51:"tree_table_rate:e1027cf7_spedizione_standard_italia";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:51:"tree_table_rate:e1027cf7_spedizione_standard_italia";s:9:"method_id";s:15:"tree_table_rate";s:11:"instance_id";i:0;s:5:"label";s:26:"Spedizione standard Italia";s:4:"cost";s:5:"11.89";s:5:"taxes";a:1:{i:13;d:2.61580000000000012505552149377763271331787109375;}}s:12:"*meta_data";a:0:{}}}}
            [previous_shipping_methods] => a:1:{i:0;a:1:{i:0;s:51:"tree_table_rate:e1027cf7_spedizione_standard_italia";}}
            [shipping_method_counts] => a:1:{i:0;i:1;}
            [chosen_shipping_methods] => 
        )

    [_dirty:protected] => 1
)

I need to get this value:

is_vat_exempt

Consulting the documentation I've found the function:

get( $key, $default = null ) 

What I'm doing wrong? I will appreciate any help.

Thank you

There is 2 ways:

1) From your WC_Session_Handler Object, you will use get_session_data() method on it:

$session = new WC_Session_Handler();

$session_data = $session->get_session_data();

$customer = maybe_unserialize( $session_data['customer'] );

$is_vat_exempt = $customer['$is_vat_exempt'];

if( $is_vat_exempt ) {
    // Do something
} else {
    // Do something else
}

2) Or you can use directly the WC_Session live object with the get() method this way:

$customer = WC()->session->get('customer');

$is_vat_exempt = $customer['$is_vat_exempt'];

if( $is_vat_exempt ) {
    // Do something
} else {
    // Do something else
}

Both ways works…

I think you are looking for this...

// Set the customer ID to the current session
$customer = new WC_Customer( 0, true );

// Use the class method to determine vat status
$vat_exempt = $customer->is_vat_exempt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM