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.?
Related
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'];
}
I have the following $items PHP array...
Array (
[44567] => Order_Item Object (
[extra_data:protected] => Array (
[product_id] => 0
[variation_id] => 0
)
[data:protected] => Array (
[order_id] => 4432
[name] => Test Product
[product_id] => 99
)
[cache_group:protected] => order-items
[object_read:protected] => 1
)
)
I am trying to use in_array to find out if it contains the product_id 99, I have tried...
if (in_array('99', $item['product_id'], true)) {
echo 'DETECTED';
}
Does anybody have a similar working example I can look at?
Am using the codeigniter cart library, but now the client wants users to only be able to checkout items from one category at at a time because of some issues with their payment gateway.
Current the site has a single checkout logic for all categories.
When a users adds an items to the cart, i have an array like this
Array
(
[d8df18561040f3d9bd9868f5c5aaa7c2] => Array
(
[rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
[id] => MYU_SC1
[qty] => 1
[price] => 500
[name] => WAEC Scratch Card
[service_image] => assets/img/waec.jpg
[service_category] => scratch_cards
[subtotal] => 500
)
[99483fe03da62c9e98ce71232998f447] => Array
(
[rowid] => 99483fe03da62c9e98ce71232998f447
[options] => Array
(
[size] => 36
[colour] => N/A
)
[id] => 80433426a546064bf5f8d09a6e7fdabc
[qty] => 1
[price] => 5000
[name] => Green Vee Jeans
[service_image] => http://localhost/myunivacity/uploads/apparels/IMG_0425.JPG
[service_category] => apparels
[subtotal] => 5000
)
)
how i do check if whether or not the items in the cart have same value for "service_category" element?. Thanks for the help
You could go with something like this:
<?php
$categories = array();
foreach($this->cart->contents() as $cart_item) {
if(!isSet($categories[$cart_item["service_category"]]) {
$categories[$cart_item["service_category"]] = 1;
}
else {
$categories[$cart_item["service_category"]]++;
}
}
print_r($categories);
?>
This fills an array with a count per category
Here is an example of an array that is output:
Array ( [CART] => Array ( [ITEMS] => Array ( [0] => Array ( [product_id] => 269194 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => 15 Top Hits for Easy Piano [product_code] => HL102668 [product_price] => 14.9900 [original_price] => 14.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) [1] => Array ( [product_id] => 266421 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => Whistle [product_code] => HD245839 [product_price] => 3.9900 [original_price] => 3.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) ) [LAST_UPDATED] => 1349829499 [NUM_ITEMS] => 2 ) [JustAddedProduct] => [CHECKOUT] => Array ( ) )
There is an array for each unique product (in this example there are 2 unique products.) Sometimes there will be just one, sometimes there could be 20 or more unique products.
The value that is important to me is [product_code]. You can see that in the first array, there is [product_code] => HL102668. In the second there is [product_code] => HD245839.
How can I check to see if 'HD' exists in any of the [product_code] values? If it does, I need to return false.
Thank you for your help!
Access your sub array :
$products = $array['CART']['ITEMS'];
Loop through your sub array :
foreach ($products as $product)
Check if HD exists in your product_code, with either simple strstr, or with regex using preg_match (if you are comfortable with it).
if (strstr($product['product_code'], "HD")) {
// Do your actions
}
When user click on the add button, the product id are stored into the session array.
See Code below:
Array
(
[storeID] => 123
[10] => Array
(
[quantity] => 1
[product_id] => 2
[extras_id] => Array
(
)
)
[20] => Array
(
[quantity] => 12
[product_id] => 2
[extras_id] => Array
(
8
)
)
)
As you can see 10 and 20 is option_id from the product_id = 2
User can select number of options from a specific product.
User can select extras (or without) from option
Is this array good design or how can it be improved?
Example:
Product (2): Burger
- Option (10): Large (User not selected any extra)
- Option (20): Small (User selected coke(8) as extra)
User selected ID 10 and 20 for burger.
I don't see a problem with it, except that you could get it more "organized", this way:
Array
(
[123] => array(
[2] => array(
[10] => array(
[quantity] => '',
[extras] => ''
),
[20] => array(
[quantity] => '',
[extras] => ''
)
)
)
)
But that's just my opinion and my way to think.
What if product 3 also has option 10?
I'd go for using product's as the key, and adding quantity, options and extras an subs of that array.
This setup does assume you can't add the same product more then once, even if the extra's do not match. Another poster suggest adding an combination of extras/options/productid, which is good. If that's the case, please upvote him :).
Array
(
'cart' => array(
'storeid' => 123,
'products' => array(
2 => array(
'quantity' => 2,
'options' => array(10, 20),
'extras' => array(2)
),
3 => array(
'quantity' => 12,
'options' => array(150, 20),
'extras' => array(1, 7)
)
)
)
)
What I prefer is to store products in this format:
Array
(
[md5 hash of (product id + serialized array of selected options)] => Array
(
'qty' => 10
'title' => 'Product XYZ'
'price' => 49.99
'options' => Array
(
...
)
)
)
That gives each cart item it's own "id", easily allowing you to modify data for each cart item, such as qty, if you need to.
Edited:
This is a trimmed down example of what my cart's items array looks like:
7483f8f0007eb9ef3ddb8d2bff606bd6 and 859d1bb225ba5d16de4d4c23076cfae0 are both md5 hashes created by md5($itemId.serialize($submittedOptions)).
Array
(
[7483f8f0007eb9ef3ddb8d2bff606bd6] => Array
(
[id] => 3
[qty] => 2
[price] => 20.00
[title] => Product XYZ
[data] => Array
(
[photo] => /uploads/media/products/product_xyz.jpg
[link] => /product/3-product-xyz/
[sku] => PRODUCT-XYZ
[weight] => 10
[attributes] => Array
(
...
)
)
)
[859d1bb225ba5d16de4d4c23076cfae0] => Array
(
[id] => 3
[qty] => 2
[price] => 30.00
[title] => Product XYZ
[data] => Array
(
[photo] => /uploads/media/products/product_xyz.jpg
[link] => /product/3-product-xyz/
[sku] => PRODUCT-XYZ
[weight] => 15
[attributes] => Array
(
...
)
)
)
)