I'm trying to integrate a tracking pixel into the magento success page. For testing I build the following code and implemented it into line > 45 in success.phtml file within the template folder. Actually the variables are all empty. What's wrong?
<?php
<?php
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$skus = array();
$qtys = array();
$amounts = array();
foreach ($order->getAllItems() as $item){
$skus[$item->getProductId()] = $item->getSku();
$names[$item->getProductId()] = $item->getName();
$qtys[$item->getProductId()] = $item->getQtyOrdered() * 1;
$amounts[$item->getProductId()] = $item->getRowTotal() * 100;//or $item->getPrice()*$item->getQtyOrdered();//but this will ignore any applied coupons
}
$skuspipe = implode("|", $skus);
$namespipe = implode("|", $names);
$qtyspipe = implode("|", $qtys);
$amountspipe = implode("|", $amounts);
<!--
OrderID: <?php echo $orderID; ?>
skus: <?php print_r($skus); ?>
names: <?php print_r($names); ?>
qtys: <?php print_r($qtys); ?>
amounts: <?php print_r($amounts); ?>
skupipe: <?php echo $skupipe; ?>
namespipe: <?php echo $namespipe; ?>
qtyspipe: <?php echo $qtyspipe; ?>
amountspipe: <?php echo $amountspipe; ?>
-->
Thank you!
In Collections, Magento often only loads kind of a stub of data for each item.
You could load the whole objects by using
$item->load( $item->getId() );
on each iteration.
Also, try to debug output the collection first to see if there are any items found.
Related
I am stuck now, I tried to parse JSON API Google Book using PHP.
this is my PHP
<?php
$api = 'AIzaSyB2__dPng5IGT9Nlca0UT7FxN459mrITmo';
$book_api = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=fift+shades+of+grey&key='.$api);
$book = json_decode($book_api);
$a = $book->items;
$results = $a->volumeInfo;
$images = $results->imageLinks;
foreach ($results as $result) {
$title = $result->title;
$author = $result->authors;
$desc = $result->description;
$cat = $result->categories;
$pages = $result->pageCount;
}
foreach ($images as $image) {
$thumb = $image->thumbnail;
}
?>
<html>
<head>
<title>Exampe API Google Book</title>
</head>
<body>
<h1><?php echo $title; ?></h1><br/>
<?php echo $desc; ?>
</body>
</html>
I get error
Notice: Trying to get property of non-object in
and
Warning: Invalid argument supplied for foreach() in
I want to use to display the search results book.
Can you help me to fix it?
Thanks
You were not selecting the right property of array. I have given a complete solution for your reference below. You really don't need two loops for it.
<?php
$api = 'AIzaSyB2__dPng5IGT9Nlca0UT7FxN459mrITmo';
$book_api = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=fift+shades+of+grey&key='.$api);
$book = json_decode($book_api);
$meta_data = $book->items;
?>
<html>
<head>
<title>Exampe API Google Book</title>
</head>
<body>
<?php
for($i = 0; $i < count($meta_data); $i++)
{
$title = $meta_data[$i]->volumeInfo->title;
$author = $meta_data[$i]->volumeInfo->authors;
$desc = $meta_data[$i]->volumeInfo->description;
$cat = #$meta_data[$i]->volumeInfo->categories;
$pages = #$meta_data[$i]->volumeInfo->pageCount;
$thumb = $meta_data[$i]->volumeInfo->imageLinks->thumbnail;
echo "<h1>$title</h1><br/>";
echo "<p><img src=\"$thumb\" style=\"float:left; padding: 10px;\">$desc</p><br clear=\"all\" /><hr />";
}
?>
</body>
</html>
If you look at your resulting json -
...
items": [
{
"kind": "books#volume",
"id": "V1Re
...
This $a = $book->items; returns an array.
So doing this is wrong -
$results = $a->volumeInfo;
You need to iterate through $a using a foreach and get each individual volumeInfo and modify the rest of your code accordingly
I'm using Wordpress and Custom Content Types Manager, how do I go about editing the code below to only grab the first image from the array below?
<?php
$array_of_images = get_custom_field('slide_images:to_array');
foreach ($array_of_images as $img_id) {
?>
<div><?php print CCTM::filter($img_id, 'to_image_tag'); ?> </div>
<?php } ?>
I tried adding in array_slice($array_of_images, 0, 1); but no luck so far. Thanks!
If all else fails you could do the same as what you have except add an $i value. It's kind of dumb but it would work if you can't get a normal method to work. This would be a last ditch effort sort of thing...
<?php
$array_of_images = get_custom_field('slide_images:to_array');
$i = 0;
foreach ($array_of_images as $img_id) { ?>
<div><?php print CCTM::filter($img_id, 'to_image_tag'); ?> </div>
<?php if($i == 0) break; } ?>
$key = array_keys($array_of_images);
$value = $array_of_images[$key[0]];
For some reason my code is an array one minute and not the next. I'm just learning php and I can't figure this out. I've used this page to figure most of this out: How to add two Google charts on the one page?
My code is also used on a page that shows the data as a table. On that page it works fine....but for some reason I'm getting Invalid argument supplied for foreach() on the pie charts page...and it's saying it's not an array. I'm not sure how to go about fixing this.
The url I'm using is ?action=scores&data=pie so it should list all of the universities in "get_universities()" (there's 3 there).
Can anyone help please?
The switch:
// Showing scores
case 'scores':
// Grab the Uni ID and use the appropriate query
if (isset($_GET['uni_id']))
{
$uni_id = $_GET['uni_id'];
$uni = get_university($uni_id);
}
else
{
$uni = get_universities();
}
// We have to display this data according to the request
if (isset($_GET['data']))
{
$data = $_GET['data'];
}
else
{
$data = "table";
}
// Display the data accordingly
include (root . '/view/' . $data . '_view.php');
break;
The pie graphs:
// Create the data table.
<?php foreach ($uni as $uni) : ?>
var data<?php echo $uni['university_id']; ?> = new google.visualization.DataTable();
data<?php echo $uni['university_id']; ?>.addColumn('string', 'Area');
data<?php echo $uni['university_id']; ?>.addColumn('number', 'Score');
data<?php echo $uni['university_id']; ?>.addRows([
['Teaching', <?php echo $uni['teaching_score']; ?>],
['Intl Outlook', <?php echo $uni['int_outlook_score']; ?>],
['Industry Income', <?php echo $uni['ind_income_score']; ?>],
['Research', <?php echo $uni['research_score']; ?>],
['Citations', <?php echo $uni['citations_score']; ?>]
]);
<?php endforeach ?>
// Set chart options
<?php foreach ($uni as $uni) : ?>
var options<?php echo $uni['university_id']; ?> = {'title':'<?php echo $uni['university_name']; ?> Scores',
'width':400,
'height':300};
<?php endforeach ?>
// Instantiate and draw our chart, passing in some options.
<?php foreach ($uni as $uni) : ?>
var chart<?php echo $uni['university_id']; ?> = new google.visualization.PieChart(document.getElementById('chart_div<?php echo $uni['university_id']; ?>'));
chart.draw(data<?php echo $uni['university_id']; ?>, options<?php echo $uni['university_id']; ?>);
<?php endforeach ?>
The table view (which works):
<?php foreach ($uni as $uni) : ?>
<td>
<a href="?uni=<?php echo $uni['university_id']; ?>">
<?php echo $uni['university_name']; ?>
</a>
</td>
<etc>
The problem is with your foreach statement:
foreach ($uni as $uni) :
Here you override the $uni variable. Use different names for the collection and item, eg:
foreach ($uni as $theUni) :
// also change instances of $uni below
EDIT
The above is wrong. The parsing of the first parameter of foreach happens only once so in that foreach it won't be a problem. However a foreach is not a new scope, so if you plan to reuse the array variable, you need to choose a different name as the iterator so it won't get overridden.
$a = array(); // $a is array
foreach ($a as $a) {
// $a is element of original $a
}
// $a is the last element of the original $a array
foreach ($a as $a) // Fail, since $a is not an array anymore
I have two foreach loops. The first grabs a load of questions from Wordpress, the second is supposed to grab the multiple answers. This is straight forward had it not involved some randomisation of the questions, which makes it confusing.
This is the two foreach loops without them being randomised.
<?php
$repeater = get_field('step_by_step_test');
foreach( $repeater as $repeater_row ){ ?>
<p><?php echo $repeater_row['question']; ?></p>
<?php $rows = $repeater_row['answer_options'];
foreach ($rows as $row){ ?>
<?php echo $row['answer']; ?><br />
<?php } ?>
<?php } ?>
This loops through each question and also grabs the multiple answers.
How can I incorporate it randomising the questions? This is my attempt, this works for getting a random set of questions but I'm getting an error for the answers part (invalid argument supplied for foreach).
<?php
$amount = get_field('select_number_of_questions');
$repeater = get_field('step_by_step_test');
$random_rows = array_rand( $repeater, $amount );
echo implode(', ', $random_rows);
foreach( $random_rows as $repeater_row ){ ?>
<p><?php echo $repeater[$repeater_row]['question']; ?></p>
<?php $rows = get_sub_field('answer_options');
foreach ($rows as $row){ ?>
<?php echo $row['answer']; ?><br />
<?php } ?>
<?php } ?>
I use this plugin for wordpress - http://www.advancedcustomfields.com/
First I'm going to rewrite your first code block to not look like chewed cud.
<?php
$repeater = get_field("step_by_step_test");
foreach($repeater as $repeater_row) {
echo "<p>".$repeater_row['question']."</p>";
$rows = $repeater_row['answer_options'];
foreach($rows as $row) {
echo $row['answer']."<br />";
}
}
?>
And now for the magic: Add shuffle($rows) immediately before the foreach($rows as $row) { line, and the answers will appear in random order.
EDIT in response to comments: Start your code like this:
$repeater = get_field("step_by_step_test");
shuffle($repeater);
$repeater_limit = array_slice($repeater,0,5);
foreach($repeater_limit as $repeater_row) {
....
I'm using simplexml_load_file to get rss feed from from a wordpress blog. Here is my code
$rssfile = simplexml_load_file( "http://blog.sufuraamathi.com/?feed=rss2" );
$items = $rssfile->channel->item ;
foreach( $items as $item ) {
$article = array();
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['category'] = $item->category;
}
foreach( $items as $item ) { ?>
<?php if($article['category']=="Uncategorized") { ?>
<div><?php echo $article['title'];?></div>
<?php
} } ;
?>
The Problem: It outputs the same post repeatedly x times, where x is total number of posts. Right now there are only two posts in the Uncategorized category and three more posts in other categories. But the code echos the following:
<div>Hello world!</div>
<div>Hello world!</div>
<div>Hello world!</div>
<div>Hello world!</div>
<div>Hello world!</div>
Your problem is on the fifth row of your posted code. You must take out the array definition of the first foreach loop:
$rssfile = simplexml_load_file( "http://blog.sufuraamathi.com/?feed=rss2" );
$items = $rssfile->channel->item ;
$article = array(); // <- put it here
foreach( $items as $item ) {
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['category'] = $item->category;
}
...
as your current solution resets the $article array for each row. But why don't you loop everything in one foreach loop? If you're not using $article for some other purpose I don't see the use of assigning $item data to an array. Code could be simplified:
$rssfile = simplexml_load_file( "http://blog.sufuraamathi.com/?feed=rss2" );
$items = $rssfile->channel->item ;
foreach( $items as $item ) { ?>
<?php if($item->category=="Uncategorized") { ?>
<div><?php echo $item->title;?></div>
<?php
} } ?>