Avinash Gupta
Avinash Gupta

Reputation: 328

Get product value on cart

Array
(
    [1a0421df7401f1b79616141d5a4e223a] => Array
        (
            [rental_data] => Array
                (
                    [pickup_date] => 2017/10/02
                    [dropoff_date] => 2017/10/05
                    [rental_days_and_costs] => Array
                        (
                            [days] => 3
                            [hours] => 0
                            [booked_dates] => Array
                                (
                                    [formatted] => Array
                                        (
                                            [0] => 2017/10/02
                                            [1] => 2017/10/03
                                            [2] => 2017/10/04
                                        )

                                    [iso] => Array
                                        (
                                            [0] => 1506902400
                                            [1] => 1506988800
                                            [2] => 1507075200
                                        )

                                )

                            [cost] => 123.75
                            [due_payment] => 251.25
                        )

                    [max_hours_late] => 0
                )

            [product_id] => 181
            [variation_id] => 0
            [variation] => Array
                (
                )

            [quantity] => 1
            [line_total] => 123.75
            [line_subtotal] => 123.75
            [line_tax] => 0
            [line_subtotal_tax] => 0
            [line_tax_data] => Array
                (
                    [total] => Array
                        (
                        )

                    [subtotal] => Array
                        (
                        )

                )

            [data] => WC_Product_Redq_Rental Object
                (
                    [object_type:protected] => product
                    [post_type:protected] => product
                    [cache_group:protected] => products
                    [data:protected] => Array
                        (
                            [name] => Spelga House (sleeps 10)
                            [slug] => spelga-house-accomodation
                            [date_created] => WC_DateTime Object
                                (
                                    [utc_offset:protected] => 0
                                    [date] => 2016-02-06 10:36:40.000000
                                    [timezone_type] => 3
                                    [timezone] => Europe/London
                                )

                            [date_modified] => WC_DateTime Object
                                (
                                    [utc_offset:protected] => 0
                                    [date] => 2017-09-25 13:06:09.000000
                                    [timezone_type] => 3
                                    [timezone] => Europe/London
                                )

                            [status] => publish
                            [featured] => 
                            [catalog_visibility] => visible
                            [description] => A large detached, recently renovated high spec modern house, previously owned by the water board and maintains its characteristics. Spelga House has spectacular views of the surrounding Mourne Mountains, and only seven miles from the lively resort town of Newcastle and three miles from Hilltown. The house sits in front of the dam wall, on top of the Mournes, and is 

i want to get the value of [due_payment] array in woocommerce cart page and [1a0421df7401f1b79616141d5a4e223a] root name change each time for each product how i get this any help? I am using the rental and booking woocommerce plugin and product type is rental product in woocommerce I am beginners for the array and plugin customozation I also search for that but i didn,t get any idea how i get the value of [due_payment] array . please guide me how i do that

Upvotes: 0

Views: 685

Answers (2)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

Here's another way of doing it, using the array key. Will not need 2 foreach loop.

$cart_items = WC()->cart->get_cart(); 
foreach( $cart_items as $cart_item_key => $cart_item ) {  
      echo $cart_items[$cart_item_key]['rental_data']['rental_days_and_costs']['due_payment'];‌​‌
}

and yet another way of doing it. This time without the foreach loop.

$cart_items = WC()->cart->get_cart();
if ( is_array( $cart_items ) && !empty( $cart_items ) ) {
    $cart_item_keys = array_keys($cart_items);
    echo $cart_items[$cart_item_keys[0]]['rental_data']['rental_days_and_costs']['due_payment'];‌​‌
}

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to apply one more foreach() like below:-

global $woocommerce; 
$items = $woocommerce->cart->get_cart();    
foreach($items as $item => $values) {  
    foreach($values as $arr){ //apply one-more foreach()
      echo $arr['rental_data']['rental_days_and_costs']['due_payment'];‌​‌
    }
}

Note:- You can curtale thses two lines:-

global $woocommerce; 
$items = $woocommerce->cart->get_cart();

Into one:-

$items = WC()->cart->get_cart(); 

Upvotes: 1

Related Questions