I am encountering this problem. In my PHP code I am setting two sessions: Cart and Visited.
A cart is clear, visited is a function to view the latest visited products.
My cart has this array:
Partnr
Productname
Quantity
Productgroep
Subgroup
Visited has this array:
Partnr
Productname
Description
Productgroup
Subgroup
The session of the cart is stored correctly, but the visited isn't.
This is the code of the cart:
$_SESSION['cart'][] = array('quantity' => $_POST['quantity'],
'partnr' => $_POST['partnr'],
'hoofdgroep' => $_POST['hoofdgroep'],
'subgroep' => $_POST['subgroep'],
'productname' => $_POST['productname']
);
And the code of visited:
$addition = array('productcode' => $csv[1][1],
'productname' => $csv[1][2],
'description' => $csv[1][4],
'hoofdgroep' => $_GET['hoofdgroep'],
'subgroep' => $_GET['subgroep']);
$_SESSION['visited'][] = $addition;
Does anyone know why my visited won't be saved and by cart is saved?
UPDATE:
Session visited after 3 products shown $_SESSION['visited']:
Array
(
[0] => Array
(
[productcode] => AD-HPR-150-CR50
[productname] => SimpleXMLElement Object
(
[0] => Adapterring voor HPR-150
)
[description] => SimpleXMLElement Object
(
[0] => Adaptar
)
[hoofdgroep] => lighting
[subgroep] => accessoires
)
)
My cart session with three products in it $_SESSION['cart']:
Array
(
[0] => Array
(
[quantity] => 1
[partnr] => AD-HPR-150-CR50
[hoofdgroep] => lighting
[subgroep] => accessoires
[productname] => Adapterring voor HPR-150
)
[1] => Array
(
[quantity] => 1
[partnr] => AD-HPR-150-CR50
[hoofdgroep] => lighting
[subgroep] => accessoires
[productname] => Adapterring voor HPR-150
)
[2] => Array
(
[quantity] => 1
[partnr] => HP-CDT1006B-24
[hoofdgroep] => lighting
[subgroep] => diffuselite-dome
[productname] => HighPower 45inch Compact Diffused Tube Light (with inline dimmer 10 turn locking knob) Blue, 24VDC
)
)
UPDATE 2:
And the revised code of visited:
$addition = array('productcode' => $csv[1][1],
'productname' => (string)$csv[1][2],
'description' => (string)$csv[1][4],
'hoofdgroep' => $_GET['hoofdgroep'],
'subgroep' => $_GET['subgroep']);
$_SESSION['visited'][] = $addition;
Related
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.?
I'm currently working with XMLRPC API for connecting a website to openerp . The idea is we want to get the latest stock quantity from openerp. Currently i'm using this connector library . This is the sample data from my openerp api
[0] => Array
(
[create_date] => 2016-01-26 03:02:29
[qty] => 6
[propagated_from_id] =>
[package_id] =>
[cost] => 1500000
[inventory_value] => 9000000
[lot_id] =>
[reservation_id] =>
[id] => 2
[negative_dest_location_id] =>
[create_uid] => Array
(
[0] => 1
[1] => Administrator
)
[display_name] => 17326: 6.0Unit(s)
[__last_update] => 2016-01-26 03:02:29
[location_id] => Array
(
[0] => 19
[1] => Warehouse 1/Stock
)
[company_id] => Array
(
[0] => 1
[1] => PT. ONE WAY
)
[history_ids] => Array
(
[0] => 2
)
[owner_id] =>
[write_date] => 2016-01-26 03:02:29
[write_uid] => Array
(
[0] => 1
[1] => Administrator
)
[name] => 17326: 6.0Unit(s)
[product_id] => Array
(
[0] => 2756
[1] => [17326] AEG Vacuum Cleaner Dust Extractor Wet & Dry AP 20
)
[packaging_type_id] =>
[negative_move_id] =>
[in_date] => 2016-01-26 03:02:29
)
How can i filter that data by product_id
[product_id] => Array
(
[0] => 2756
[1] => [17326] AEG Vacuum Cleaner Dust Extractor Wet & Dry AP 20
)
Here is my code
$rpc = new OpenERP();
$x = $rpc->login("supermin", "my_site", "my_pass", "http://111.222.33.44:8069/xmlrpc/");
$data = $rpc->searchread(
array(
array("model", "=", "product.product"),
array("module", "=", "sale"),
array("product_id", "=", "2756"),
),
"stock.quant"
);
Any example will be helpful. Thanks
Try to use this:
//... your source
$data = $rpc->searchread(
array(
array('model', '=', 'product.product'),
array('module', '=', 'sale'),
array('product_id', '=', '2756'),
),
'stock.quant',
array(), // default
0, // default
10, // default
'product_id DESC' // default value was 'id DESC'
);
I did not use connector lib, but if we check searchread() method of OpenERP class we can see that default $order = "id DESC".
Hope this help you.
I have a question about this:
I have two array, one is static, and one can be updated by the user...
I would like to check for every id from the static array if exist the id to the other array, and if exsist, do something, if doesn't exist (when finish to check) pass to other ID etc...
now, the arrays are these:
user array (the user unlock 2 achievement):
Array (
[0] => Array (
[data] => Array (
[importance] => 0
[achievement] => Array (
[id] => 644081262362202
[title] => Achievement 2
[type] => game.achievement
[url] => http://www.***.com/achievements/achievement2.html
)
)
[id] => 104693166566570
)
[1] => Array (
[data] => Array (
[importance] => 0
[achievement] => Array (
[id] => 968802826528055
[title] => Achievement 1
[type] => game.achievement
[url] => http://www.***.com/achievements/achievement1.html
)
)
[id] => 104023386633548
)
)
the static Array (have 6 achievement saved):
Array (
[0] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement2
[title] => Achievement 2
[id] => 644081262362202
)
[1] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement3
[title] => Achievement 3
[id] => 912599152147444
)
[2] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement5
[title] => Achievement 5
[id] => 913757345379232
)
[3] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement6
[title] => Achievement 6
[id] => 921989084564878
)
[4] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement1
[title] => Achievement 1
[id] => 968802826528055
)
[5] => Array (
[data] => Array (
[points] => 50
)
[description] => you unlock the achievement4
[title] => Achievement 4
[id] => 1149671038394021
)
)
now, I use this script to echo the final output like the picture (results is the static array):
if (empty($results)) {
//echo 'noAchievement for the app';
} else {
foreach ($results as $result) {
$totalAchievementsApp .= ' [["' . "0" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";
}
}
now, How I can do to check inside the this script? I know I have to add another if inside the else to check if the ID is = to other ID, but I don't know how, I'm a little bit confused... I would like to check if the id of the static array exist in the other array, and if exsist, do this:
**$totalAchievementsApp .= ' [["' . "1" .'"],["'.$result[id] .'"],["'. $result[title] .'"],["'. $result[data][points]."]] ";**
Thank you very much :)
If I understand correctly, you want to indicate for each entry in the static array whether its ID exists in the user array.
You can use array_column to generate an array of all IDs in the user array. Then use in_array to check if each static ID exists in that array. Set a value to 1 if its found and 0 if its not found.
For the sake of example, I've generated a new final output array. But you could just add the "found" value to each entry of the the static array.
<?php
$static=array(
array('point'=>50,'title'=>'TITLE 1','id'=>54632),
array('point'=>50,'title'=>'TITLE 2','id'=>54344),
array('point'=>50,'title'=>'TITLE 3','id'=>34225),
array('point'=>50,'title'=>'TITLE 4','id'=>2323245),
array('point'=>50,'title'=>'TITLE 5','id'=>23872445),
);
$user=array(
array('id'=>2323245,'title'=>'TITLE 1','point'=>50),
array('id'=>54344,'title'=>'TITLE 2','point'=>50),
array('id'=>34225,'title'=>'TITLE 3','point'=>50)
);
$final=array();
foreach ($static as $entry) {
$final[]=array(
'found'=>in_array($entry['id'],array_column($user,'id'))?1:0,
'id'=>$entry['id'],
'title'=>$entry['title'],
'point'=>$entry['point']
);
}
echo"<pre>".print_r($final,true)."</pre>";
With your data, the output is:
Array
(
[0] => Array
(
[found] => 0
[id] => 54632
[title] => TITLE 1
[point] => 50
)
[1] => Array
(
[found] => 1
[id] => 54344
[title] => TITLE 2
[point] => 50
)
[2] => Array
(
[found] => 1
[id] => 34225
[title] => TITLE 3
[point] => 50
)
[3] => Array
(
[found] => 1
[id] => 2323245
[title] => TITLE 4
[point] => 50
)
[4] => Array
(
[found] => 0
[id] => 23872445
[title] => TITLE 5
[point] => 50
)
)
EDIT
Given the more complex structure of your actual arrays, I nested several array_column functions to access the deeper "data > achievement > id" keys in your user array:
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');
See the example below:
// initialize the "static" and "user" arrays
$static=array (
0 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement2',
'title' => 'Achievement 2',
'id' => 644081262362202
),
1 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement3',
'title' => 'Achievement 3',
'id' => 912599152147444
),
2 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement5',
'title' => 'Achievement 5',
'id' => 913757345379232
),
3 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement6',
'title' => 'Achievement 6',
'id' => 921989084564878
),
4 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement1',
'title' => 'Achievement 1',
'id' => 968802826528055
),
5 => array(
'data' => array(
'points' => 50
),
'description' => 'you unlock the achievement4',
'title' => 'Achievement 4',
'id' => 1149671038394021
)
);
$user=array(
0=>array(
'data' => array(
'importance' => 0,
'achievement' => array (
'id' => 644081262362202,
'title' => 'Achievement 2',
'type' => 'game.achievement',
'url' => 'http://www.***.com/achievements/achievement2.html'
)
),
'id' => 104693166566570
),
1 => array (
'data' => array (
'importance' => 0,
'achievement' => array (
'id' => 968802826528055,
'title' => 'Achievement 1',
'type' => 'game.achievement',
'url' => 'http://www.***.com/achievements/achievement1.html'
)
),
'id' => 104023386633548
)
);
// build array of user achievement IDs
$user_achvmts=array_column(array_column(array_column($user,'data'),'achievement'),'id');
// generate final array, with "found" values
$final=array();
foreach ($static as $entry) {
$final[]=array(
'found'=>in_array($entry['id'],$user_achvmts)?1:0,
'id'=>$entry['id'],
'title'=>$entry['title'],
'description'=>$entry['description'],
'points'=>$entry['data']['points']
);
}
echo"<pre>".print_r($final,true)."</pre>";
The result is:
Array
(
[0] => Array
(
[found] => 1
[id] => 644081262362202
[title] => Achievement 2
[description] => you unlock the achievement2
[points] => 50
)
[1] => Array
(
[found] => 0
[id] => 912599152147444
[title] => Achievement 3
[description] => you unlock the achievement3
[points] => 50
)
[2] => Array
(
[found] => 0
[id] => 913757345379232
[title] => Achievement 5
[description] => you unlock the achievement5
[points] => 50
)
[3] => Array
(
[found] => 0
[id] => 921989084564878
[title] => Achievement 6
[description] => you unlock the achievement6
[points] => 50
)
[4] => Array
(
[found] => 1
[id] => 968802826528055
[title] => Achievement 1
[description] => you unlock the achievement1
[points] => 50
)
[5] => Array
(
[found] => 0
[id] => 1149671038394021
[title] => Achievement 4
[description] => you unlock the achievement4
[points] => 50
)
)
Note that array_column is only available in PHP >= 5.5.0. For older versions, see the Recommended userland implementation for PHP lower than 5.5.
As an alternative to array_column, you could use array_map to build an array of the user IDs:
$user_achvmts = array_map( function($v) {return $v['data']['achievement']['id'];}, $user);
Or even just iterate through the user array:
$user_achvmts=[];
foreach ($user as $v) { $user_achvmts[]=$v['data']['achievement']['id']; }
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'];
}
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
(
...
)
)
)
)