在购物车上获得产品价值

时间:2017-10-03 04:58:48

标签: php wordpress woocommerce

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 

我希望在woocommerce购物车页面中获取[due_payment]数组的值,并且每次为每个产品更改[1a0421df7401f1b79616141d5a4e223a]根名称我如何获得此帮助?我正在使用租赁和预订woocommerce插件和产品类型是woocommerce租赁产品我是阵列和插件定制的初学者我也搜索,但我没有,我知道如何得到[due_payment]的价值阵列。请指导我如何做到这一点

2 个答案:

答案 0 :(得分:1)

您需要再申请一个foreach(),如下所示: -

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'];‌​‌
    }
}

注意: - 你可以缩减这两行: -

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

成为一个: -

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

答案 1 :(得分:0)

这是使用数组键执行此操作的另一种方法。不需要2个foreach循环。

$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'];‌​‌
}

还有另一种方法。这次没有foreach循环。

$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'];‌​‌
}