Get product value on cart - php

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

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

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

Related

Display variation array in woocommerce cart and checkout page

Am trying add some custom data to my cart. For that I have used
$woocommerce->cart->add_to_cart($domainProductId, $period, 0, array('Domain Name' => $domain, 'Domain Term' => $period));
When I try to display the cart details another function or another page, the entered details is shown in variation array.
global $woocommerce;
$items = $woocommerce->cart->get_cart();
echo "<pre>"; print_r($items);
The items array is
<pre>Array
(
[2c098ef710cdd6e95cde45d2c5d6872b] => Array
(
[key] => 2c098ef710cdd6e95cde45d2c5d6872b
[product_id] => 129
[variation_id] => 0
[variation] => Array
(
[Domain Name] => domainnn
[Domain Term] => 1
)
[quantity] => 1
[data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0
[line_tax_data] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
[line_subtotal] => 95
[line_subtotal_tax] => 0
[line_total] => 95
[line_tax] => 0
[data] => WC_Product_Simple Object
(
[object_type:protected] => product
[post_type:protected] => product
[cache_group:protected] => products
[data:protected] => Array
(
[name] => net
[slug] => net
How can i display the variation array in my cart and checkout page.?

php delete specific value from array

I have an array $products that looks like this
Array
(
[services] => Array
(
[0] => Array
(
[id] => 1
[icon] => bus.png
[name] => Web Development
[cost] => 500
)
[1] => Array
(
[id] => 4
[icon] => icon.png
[name] => Icon design
[cost] => 300
)
)
)
I am trying to delete the part of array that matches [id] => 1 and for this I am using the following code
$key = array_search('1', $products);
unset($products['services'][$key]);
However it is not working and I am not getting any error either.
What am i doing wrong?
This should work for you:
$key = array_search('1', $products["services"]);
//^^^^^^^^^^^^ See here i search in this array
unset($products['services'][$key]);
print_r($products);
Output:
Array ( [services] => Array ( [1] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
And if you want to reindex the array, so that it starts again with 0 you can do this:
$products["services"] = array_values($products["services"]);
Then you get the output:
Array ( [services] => Array ( [0] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
//^^^ See here starts again with 0
This will loop through $products['services'] and delete the array whose 'id' key has value 1. array_values just re-indexes the array from 0 again.
foreach($products['services'] as $key => $service)
{
if($product['id'] == 1)
{
unset($products['services'][$key]);
array_values($products['services']);
break;
}
}

Perform foreach on values returned inside array

I'm not sure if I have worded this question correctly, but basically I have the following array result from WordPress I am trying to deal with:
Array
(
[0] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 3
[user_login] => Jean Gomes
[user_pass] => $P$BvW6LK9/blGKLuSj.9spNPW3.v104..
[user_nicename] => jean-gomes
[user_email] => jean.gomes#dpa.dev
[user_url] =>
[user_registered] => 2014-08-28 10:56:39
[user_activation_key] =>
[user_status] => 0
[display_name] => Jean Gomes
)
[ID] => 3
[caps] => Array
(
[author] => 1
)
[cap_key] => dpa2335327_capabilities
[roles] => Array
(
[0] => author
)
[allcaps] => Array
(
[upload_files] => 1
[edit_posts] => 1
[edit_published_posts] => 1
[publish_posts] => 1
[read] => 1
[level_2] => 1
[level_1] => 1
[level_0] => 1
[delete_posts] => 1
[delete_published_posts] => 1
[wpseo_bulk_edit] => 1
[author] => 1
)
[filter] =>
)
[1] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 4
[user_login] => Tammy Day
[user_pass] => $P$BejydpFxKDp60WruV71u0/JnJm9o.E.
[user_nicename] => tammy-day
[user_email] => tammy.day#dpa.dev
[user_url] =>
[user_registered] => 2014-08-28 10:57:23
[user_activation_key] =>
[user_status] => 0
[display_name] => Tammy Day
)
[ID] => 4
[caps] => Array
(
[author] => 1
)
etc
I would like to perform a foreach using these results, for the ID.
E.g.
foreach ( $authors as $author_id ) {
But I don't know how to access the ID. I tried:
foreach ( $authors->ID as $author_id ) {
But obviously this can't work, as there is a number before that, is there an equivalent of:
foreach ( $authors['X']->ID as $author_id ) {
that I could use?
Sorry I know this is probably a silly question, but I'm quite stuck. I know I probably have to do a foreach within a foreach but I can't work out the syntax at all.
foreach (array_expression as $value)
On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).
Read this documentation page for more details about foreach loop:
In your situation $value will pointer to the instance of WP_User Object. To access to the user's id just read object's property ID
foreach ( $authors as $author ) {
$author_id = $author->ID;
}
You cant try this instead of foreach :
$i=0;
while ($i<count($authors)){
$author_id = $authors[$i]['data']['id'];
//Do whatever you want here.
$i=$i+1;
}
As the dump states, the array elements are instances of WP_User. You can use the following loop to fetch the IDs:
foreach ($authors as $User) {
$author_id = $User->ID;
// Act
}
For a reference of the WP_User class, see http://codex.wordpress.org/Class_Reference/WP_User

Stripping unwanted data from an Array in php

This code builds an array:
$size = sizeof($include_quotes);
for ($i=0; $i<$size; $i++) {
$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
if (is_array($quotes)) $quotes_array[] = $quotes;
}
}
If i
print_r($quotes_array);
i get the following:
Array ( [0] => Array ( [id] => advshipper [methods] => Array ( [0] => Array ( [id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 ) [1] => Array ( [id] => 2-0-0 [title] => 1-2 working days [cost] => 3.2916666666667 [icon] => [shipping_ts] => [quote_i] => 1 ) [2] => Array ( [id] => 4-0-0 [title] => 2-3 working days [cost] => 2.4916666666667 [icon] => [shipping_ts] => [quote_i] => 2 ) [3] => Array ( [id] => 8-0-0 [title] => Click & Collect [cost] => 0 [icon] => [shipping_ts] => [quote_i] => 3 ) ) [module] => Shipping [tax] => 20 ) )
In some circumstances, I only want the data in field 0 to be passed onto the next part of the code. However, using
$sliced_quotes_array = array_slice($quotes_array,0,1);
Still returns all the results.
What is the correct method to get just:
Array ( [0] => Array ( [id] => advshipper [methods] => Array ( [0] => Array ( [id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 )
Any help greatly appreciated because i have tried numerous different ways and no luck yet.
Using the following still returns the same results
$testarray = array(0 => $quotes_array[0]);
print_r($testarray);
Why not just use the array constructor and explicitly include what you need:
array(0 => $quotes_array[0]);
Here is your array:
When you are saying " I only want the data in field 0 to be passed onto the next part of the code", you meant that you only want this data to be passed next, right? :
Array (
[0] => Array (
[id] => advshipper
[module] => Shipping
[tax] => 20
)
)
Is this what you want?
$newArray = array();
foreach ($quotes_array[0] as $items)
{
if (!is_array($items))
{
$newArray[] = $items;
}
}
$newArray will contain that data.
UPDATE
Okay, gotcha.
You can just use this:
$newArray = $quotes_array[0]['methods'][0];
Having done some reading on arrays and after a bit of trial, i found a solution to my problem.
I used:
unset($quotes_array[0]['methods'][1]);
By changing the index number after methods i was able to drop any shipping options i didn't require, whilst still maintaining the functionality.

Get value in mutli array

How would i get the value of a key in an array?
The array is done by google shopping api which is:
// Valid source values are "public", "cx:cse", and "gan:pid"
// See http://code.google.com/apis/shopping/search/v1/getting_started.html#products-feed
$source = "public";
// For more information about full text search with the shopping API, please
// see http://code.google.com/apis/shopping/search/v1/getting_started.html#text-search
$query = "\"mp3 player\" | ipod";
//The order in which the API returns products is defined by a ranking criterion.
// See http://code.google.com/apis/shopping/search/v1/getting_started.html#ranking
$ranking = "relevancy";
$results = $service->products->listProducts($source, array(
"country" => "UK",
"q" => $query,
"rankBy" => $ranking,
));
print "<h1>Shopping Results</h1><pre>" . print_r($results, true) . "</pre>";
I have the following array which outputs:
Shopping Results
Array
(
[kind] => shopping#products
[etag] => "*********"
[id] => tag:google.com,2010:shopping/products
[selfLink] => https://www.googleapis.com/shopping/search/v1/public/products?country=UK&q=iphone&rankBy=relevancy
[nextLink] => https://www.googleapis.com/shopping/search/v1/public/products?country=UK&q=iphone&rankBy=relevancy&startIndex=26
[totalItems] => 771622
[startIndex] => 1
[itemsPerPage] => 25
[currentItemCount] => 25
[requestId] => 0CMjH976CqbECFYNWtAodLRwAAA
[items] => Array
(
[0] => Array
(
[kind] => shopping#product
[id] => tag:google.com,2010:shopping/products/5735617/11254757413841304510
[selfLink] => https://www.googleapis.com/shopping/search/v1/public/products/5735617/gid/11254757413841304510
[product] => Array
(
[googleId] => 11254757413841304510
[author] => Array
(
[name] => Amazon.co.uk
[accountId] => 5735617
)
[creationTime] => 2012-05-04T05:03:50.000Z
[modificationTime] => 2012-07-20T02:02:16.000Z
[country] => GB
[language] => en
[title] => Apple iPod touch 8GB - Black - 4th Generation (Latest Model - Launched Sept 2010)
[description] => Apple iPod touch 8GB - Black - 4th Generation (Latest Model - Launched Sept 2010)
[link] => http://www.amazon.co.uk/dp/B0040GIZTI/ref=asc_df_B0040GIZTI8843997?smid=A1YZ4RXO7GUOYN&tag=googlecouk06-21&linkCode=asn&creative=22218&creativeASIN=B0040GIZTI
[brand] => Apple
[condition] => new
[gtin] => 00885909394739
[gtins] => Array
(
[0] => 00885909394739
)
[mpns] => Array
(
[0] => MC540BT/A
)
[inventories] => Array
(
[0] => Array
(
[channel] => online
[availability] => inStock
[price] => 135.95
[shipping] => 1.99
[currency] => GBP
)
)
[images] => Array
(
[0] => Array
(
[link] => http://ecx.images-amazon.com/images/I/41p2rNmazRL.jpg
[status] => available
)
)
)
)
[1] => Array
(
[kind] => shopping#product
[id] => tag:google.com,2010:shopping/products/5735617/4597224105326146239
[selfLink] => https://www.googleapis.com/shopping/search/v1/public/products/5735617/gid/4597224105326146239
[product] => Array
(
[googleId] => 4597224105326146239
[author] => Array
(
[name] => Amazon.co.uk
[accountId] => 5735617
)
[creationTime] => 2012-05-04T05:03:50.000Z
[modificationTime] => 2012-07-20T02:02:16.000Z
[country] => GB
[language] => en
[title] => SanDisk Sansa Clip+ 8GB MP3 Player with Radio and Expandable MicroSD/SDHC Slot - Black
[description] => 8 GB memory Digital FM-tuner with 40 preset radio stations Extendable microSD/microSDHC card slot
[link] => http://www.amazon.co.uk/dp/B002NX0ME6/ref=asc_df_B002NX0ME68843997?smid=A3P5ROKL5A1OLE&tag=googlecouk06-21&linkCode=asn&creative=22206&creativeASIN=B002NX0ME6
[brand] => SanDisk
[condition] => new
[gtin] => 00619659059989
[gtins] => Array
(
[0] => 00619659059989
)
[mpns] => Array
(
[0] => SDMX18-008G-E46K
)
[inventories] => Array
(
[0] => Array
(
[channel] => online
[availability] => inStock
[price] => 46.95
[shipping] => 0
[currency] => GBP
)
)
[images] => Array
(
[0] => Array
(
[link] => http://ecx.images-amazon.com/images/I/419U6bYDF1L.jpg
[status] => available
)
)
)
)
I don't need all this data i just need 3-4 of the keys but how would i access them? How would i echo the value of say [title] from each array?
This should work:
foreach( $results as $result)
foreach( $result['product'] as $product)
echo $product['title'];
You could either loop through the array like pointed out above or possibly use array_walk_recursive like this:
$title_array = array();
array_walk_recursive($input_array, 'find_titles');
function find_titles($value, $key) {
global $title_array;
if ($key == 'title') {
$title_array[] = $value;
}
}
This might be a better solution if you you are not certain what the structure of the input array will be (i.e. how many levels deep the key you are looking for is nested).
To output the title of each product in $results:
foreach ($results as $result) {
echo $result['product']['title'];
}
Consider using array_walk_recursive
Working example
<?php
$a = array("hai", array("ha"=>1, "hai"=>2, array("a"=>1, "b"=>2)));
function test($item, $key)
{
echo "$key holds $item\n";
}
array_walk_recursive($a, 'test');
0 holds hai
ha holds 1
hai holds 2
a holds 1
b holds 2
If you are interested only in title
Consider using foreach
foreach($results['item'] as $result) {
echo $result['product']['title'];
}

Categories