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
Related
I develop a new ecommerce site using PHP. I manage cart related data to database and store that data in session. The issue is when I add a product if the product is not in cart it adds that but if a product already exists it doesn't update quantity. This is my code for reference. I use key as product id
$getUserTemp['products']=array();
$mode=mysqli_real_escape_string($conn,$_POST['mode']);
$productId=mysqli_real_escape_string($conn,$_POST['product_id']);
$quantity=mysqli_real_escape_string($conn,$_POST['quantity']);
$productData=getSingleProduct("SELECT id,name,
quantity as quantity_in_stock,
original_price,discounted_price
from `products`
where id=".$productId."");
$productData['quantity']=$quantity;
switch ($mode) {
case 'add-to-cart':
if(empty($getUserTemp['products'])){
$getUserTemp['products'][$productId]=$productData;
}else{
foreach ($getUserTemp['products'] as $key => $value) {
if($key == $productId){
$value['quantity']+=$quantity;
$value[$productId]=$value;
}else{
$getUserTemp['products'][$productId]=$productData;
}
}
}
mysqli_query($conn,"UPDATE `session_cart` set session_data='".json_encode($getUserTemp)."' where user_id='".$currentLoggedUserId."'");
print_r($getUserTemp);
break;
}
My array data:
Array
(
[user] => Array
(
[id] => 8
[profile_id] => 6
[user_name] => bala#mail.com
[role] => user
[password] => $2y$10$bL4LybXcOX6nkEgRAM6Jjerrz2czEZJLWv7W1MNg.vWscb39NEsx2
)
[products] => Array
(
[38] => Array
(
[id] => 38
[name] => HP
[quantity_in_stock] => 33
[original_price] => 50.00
[discounted_price] => 40.00
[quantity] => 1
)
[8] => Array
(
[id] => 8
[name] => ASUS VivoBook 15 (2021) Core i3 10th Gen - (8 GB/512 GB SSD/Windows 11 Home) X515JA-EJ362WS Thin and Light Laptop (15.6 inch, Transparent Silver, 1.80 kg, With MS Office)
[quantity_in_stock] => 20
[original_price] => 50.00
[discounted_price] => 40.00
[quantity] => 1
)
)
)
I have an array that contains 3 products with attributes of "color" and "size" and the products are identified by a number ( ['code'] ).
My problem is that when i pull the data from the database I get this array that is in 6 pieces because "color" and "size" get stored in separate arrays.
My question is, how do I generate the data into an array of these 3 products with all their data in the same array.
Array(
[0] => Array
(
[code] => 123
[name] => box
[stock] => 2.00
[price] => 10.00
[color] => brown
)
[1] => Array
(
[code] => 123
[name] => box
[stock] => 2.00
[price] => 10.00
[size] => L
)
[2] => Array
(
[code] => 1234
[name] => box
[stock] => 3.00
[price] => 11.00
[color] => brown
)
[3] => Array
(
[code] => 1234
[name] => box
[stock] => 3.00
[price] => 11.00
[size] => XL
)
[4] => Array
(
[code] => 12345
[name] => box
[stock] => 4.00
[price] => 12.00
[size] => XL
)
[5] => Array
(
[code] => 12345
[name] => box
[stock] => 4.00
[price] => 12.00
[color] => gray
)
)
Expected output:
[0] => Array
(
[code] => 123
[name] => box
[stock] => 4.00
[price] => 12.00
[color] => gray
[size] => XL
)
What i'm looking to do is just combine the doubled array into one. Dont want to mess with SQL anymore - it gets attribute name in one table, attribute values from another table, code,stock,price from another table, name from another table. I know something can be done with just this array even if it will be just a temporary solution.
You can do this:
assuming your array is $products
$merged = array();
foreach($products as $product) {
if (!isset($sorted[$product['code']])) {
$merged[$product['code']] = $product;
} else {
$merged[$product['code']] = array_merge($sorted[$product['code']], $product);
}
}
Use the product code as array key and then merge the array.
If the two rows are always direct successors, with the first one holding the color field, while the latter holes the size field, you can make it easily by iterating over them in a for loop:
$maxCount = count($array);
for ($i = 1; $i < $maxCount; $i += 2) {
$array[$i - 1]['size'] = $array[$i]['size'];
unset($array[i]);
}
This will iterate over each second element of the array and add the size field to the preceding field.
If you need to have succeeding array keys afterwards you can call $arry = array_values($array);.
In case the the associated rows might not be successors you need to map them based on their code field (in case thats the primary key). You can use array_reduce() for that:
$desiredOutput = array_reduce($array, function($output, $element) {
if (!array_key_exists($element['code'], $output)) {
$output[$element['code']] = $element;
} elseif (array_key_exists('size', $element)) {
$output[$element['code']]['size'] = $element['size'];
} elseif (array_key_exists('color', $element)) {
$output[$element['code']]['color'] = $element['color'];
}
return $output;
}, []);
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 wish to update a single product quantity from a session with a new value using php.
How can I do this.
Array data is as displayed
Array
(
[products] => Array
(
[0] => Array
(
[name] => Ford AA Flatbed
[code] => IWV001
[qty] => 1
[price] => 15.00
[weight] => 0.12
)
[1] => Array
(
[name] => Ford AA Stakebed
[code] => IWV003
[qty] => 1
[price] => 15.00
[weight] => 0.21
)
)
)
any help would be most appreciated.
I want to be able to search through the session and find the product by code and update the quantity within that product.
Something like this:
foreach($products as &product) {
if ($product[code] == $codeToUpdate) {
$product[qty] = $newQty;
}
}
Of course, this is a sequential search so it will be slow for large data sets.
I have an array like this
Array
(
[35635d5ebdd938d6360e65a9e2484073] => Array
(
[rowid] => 35635d5ebdd938d6360e65a9e2484073
[id] => MYU_SC3
[qty] => 1
[price] => 4800
[name] => JAMB UTME Scratch Card
[service_image] => assets/img/jamb.jpg
[service_category] => cards
[subtotal] => 4800
)
[d8df18561040f3d9bd9868f5c5aaa7c2] => Array
(
[rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
[id] => MYU_SC1
[qty] => 1
[price] => 1600
[name] => WAEC Scratch Card
[service_image] => assets/img/waec.jpg
[service_category] => cards
[subtotal] => 1600
)
[a4a751dd9a69824eb3abb6f49c7a7f61] => Array
(
[rowid] => a4a751dd9a69824eb3abb6f49c7a7f61
[id] => MYU_SC2
[qty] => 1
[price] => 1600
[name] => NECO Scratch Card
[service_image] => assets/img/neco.jpg
[service_category] => cards
[subtotal] => 1600
)
)
I want to retrieve the sub-arrays that match elements in another array
$card_skus = array("MYU_SC1","MYU_SC2","MYU_SC3");
Am looping through the main array
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus))
{
//Didn't know what to do at this point
}
}
How do i get this done, thanks for the help
I guess #nickb already answered in the comments, didnt see it while writing an answer.
Try this
$subArray = array();
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus))
{
//Didn't know what to do at this point
$subArray[] = $item;
}
}
Use $subArray now as per your requirement