Magento - Display Category information in Sales Orders view - php

EDIT: So a guy in this thread says the following:
"All order data in Magento is product-specific. It doesn't store anything about what category the products are in. So, as you have found out, there are no reports available in Magento for that sort of thing."
Is this true? Is what I am trying to do a lost cause?
I am trying to display product categories on Magento's backend Sales > Order > specific order view > Information > Items Ordered grid. This is not the main "Orders grid" that you see when navigating to Sales > Order. I want to alter the more detailed one that you see after clicking on a specific order in that first grid.
Following advice from this thread, I've made a new column and given it the proper title (Categories).
Then, to try to populate that column, I added code from Joost de Valk's solution on this thread to app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
I feel like I should be close (or not) but currently that code is just returning the word "Array". Any thoughts?
My current code is below. The column in question is the second td - the one that echoes $cats:
<?php $_item = $this->getItem() ?>
// Here is the snippet I pasted in to define variables.
<?php $product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
<?php $this->setPriceDataObject($_item) ?>
<tr<?php if (!$this->canDisplayGiftmessage()): ?> class="border"<?php endif; ?>>
<td>
<?php if ($this->canDisplayContainer()): ?>
<div id="<?php echo $this->getHtmlId() ?>" class="item-container">
<?php endif; ?>
<div class="item-text">
<?php echo $this->getColumnHtml($_item, 'name') ?>
</div>
<?php if ($this->canDisplayContainer()): ?>
</div>
<?php endif ?>
</td>
// Here is the column in question.
<td class="a-center"><?php echo $cats ?></td>
<td class="a-center"><?php echo $_item->getStatus() ?></td>
<td class="a-right"><?php echo $this->displayPriceAttribute('original_price') ?></td>
etc, etc, etc ...

$_cat is an object
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
<td class="a-center"><?php echo $cats ?></td>
You need to know which property you want to display
echo $_cat->getXyz();
eg.
echo $_cat->getName();
To see a list of some of it properties try
print_r($_cat->getData());

So it seems that categories are really complicated and certainly beyond my abilities. I figured out a work-around, however ugly. Since I am not using the manufacturer field and have only 4 relevant categories, I am creating "manufacturers" for each of those 4 categories, going through and assigning them to all products, and then calling for manufacturer in the relevant file. The code for manufacturer is as follows:
<?php
$manufacturer = Mage::getModel('catalog/product')->load($_item['product_id'])->getAttributeText('manufacturer');
echo $manufacturer;
?>
Thanks to this post for that snippet and thanks to everyone else for their help.

Related

How to send product id in order to display it on another page?

I'm working on page which consists from database of products.
On the front page you can see every product but with only limited information.
I would like to create secondary page by clicking on Learn more of a product with all information.
Really something widely used in eshops where there is no need to create new page for every product but rather one template which will get information from PHP.
The problem is that I don't know how is this principle working.
I don't even know how it is called so I can search for some tutorial.
Maybe fetching array through products' ID?
For showing products I use:
`
<?php
$db = new PDO("mysql:host=localhost;dbname=cars", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Query
$cars = $db->prepare("SELECT car_id, name, price, availability, description FROM cars
");
$cars->execute();
$cars = $cars->fetchAll(PDO::FETCH_ASSOC);
?>
`
And then:
<?php foreach($cars as $car): ?>
<td>
<div><img src="<?php echo $car['image'];?>"
</div>
</td>
<td>
<a href="#">
<div id="textTable"><strong><?php echo $car['name'];?></strong></a>
Learn More..
</div>
</td>
<td>
<?php echo $car['availability'];?>
</td>
<td>
<strong><?php echo $car['price'];?>€</strong>
</td>
</tr>
<?php endforeach;?>
You can send you send your product id like this
View
And fetch each record from product id in database with only one php page
This code is for mfetch record in you second page.
$id = $_GET['product_id'];
$cars = $db->prepare("SELECT car_id, name, price, availability, description FROM cars WHERE cae_id='$id'");
$cars->execute();
$cars = $cars->fetchAll(PDO::FETCH_ASSOC);
One possible solution :
Learn More..
(I removed the id="learnMore" because an id must appear only once in an HTML page)

Getting an info from the DB with Tables and Updating it

Hi guys im having a difficulty with this scenario:
I want to get the product information using modals, i got this following code:
//Model.php
public function getProduct($product_id){
$this->db->select('product_id,product_name,product_price,product_qty');
$this->db->from('tbl_products');
$this->db->where('product_id',$product_id);
$query = $this->db->get();
return $query->result();
}
//Controller.php
public function view_product(){
$product_id = $this->input->post('product_id');
$this->load->view('header');
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
//update_product.php (modal) my View
lets just go straight into the form
<form action="" method="post">
<?php foreach($posts as $post){ ?>
<input type = "hidden" name = "product_id" value = "<?php echo $post->product_id;?>"/>
<input type = "text" name = "product_name" value = "<?php echo $post->product_name;?>"/>
<input type = "text" name = "product_price" value = "<?php echo $post->product_price;?>"/>
<input type = "text" name = "product_qty" value = "<?php echo $post->product_qty;?>"/>
<button type="submit">Update</button>
<?php } ?>
</form>
I got a table already: i can see all products, in my product_page.php
Here is the tables face looks like:
ID Name Price Quantity Option
1 Shoes 150.00 1 Update
2 Liquor 67.50 5 Update
3 Paint 1000.00 5 Update
Once I click the Update button, the update_product.php(a modal) will pop up and get the result of 1 of the product, if I press the first Update only the information for Shoes will be inside the modal, at first i tried it, I get all the information of all the products which makes my modal redundant and looping due to foreach, then I tried getting the information from the table ID itself, and no product pops out, how can I see only 1 product using modal? thank you very much maam and sirs. Please I really need youre help :(
Although I'm not really clear what you're asking, Here is an answer to you question. Your controller code doesn't seem to be making any sense.If you want to display all your products in on page and then you want to edit/update a product when clicked on corresponding update link, here is what you can do.
Use single controller method for list and update
//Controller.php
public function view_product(){
$product_id = (isset($this->input->post('product_id')) ? $this->input->post('product_id'): False ;
if($product_id == False){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
} else{
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
}
This is simple modification to your code to make it work correctly, but since this code cannot handle form submission of the update form (unless you're pointing update form to a different controller), you will have to add some other code to this controller and your code will get messy in no time. My personal suggestion to you is this,
Use different controllers for list view and update
//Controller.php
public function view_product(){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
}
public function update_product($product_id){
if($this->input>post('submit')){ // 'submit'should be replaced with the name attribute of your submit button
//call your model and handle the update form submission here
}
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('product_page',$this->data);
}
now, update option of your product_page.php should point to 'update_product' controller method with corresponding product id e.g. {base_url}/controller_class_name/update_product/1
I think your product_page.php has some code like this to loop through all the products and display products in a table, now when you click update link it will point to update_product controller and it will handle the update process.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php if(!$products){ ?>
<tr>
<td colspan="5">No result</td>
</tr>
<?php } ?>
<?php if($products){ ?>
<?php foreach ($products as $product) { ?>
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->name; ?></td>
<td><?php echo $product->price; ?></td>
<td><?php echo $product->quantity; ?></td>
<td>
<a href="<?php echo base_url(); ?>controller_class_name/update_product/<?php echo $product->id; ?>" >Update</a>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
You won't be needing a foreach loop inside your update_product.php because you're updating only one product at once.
Hope this is the answer you're looking for, if not please comment and I will edit the answer accordingly.

Show Three records of MySql table in three columns of html table Using PHP

I Need to show three records of MySql table in three different column using php.My Query is
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
I want to show the result as here
How can i Write LOOP for it?like
while(loop condition)
{
//what will go here?
}
UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records
OP saying, it's not so simple. But it is.
So, you have 2 ways to do it.
First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
EDIT
I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.
One way of looping through them is foreach()
assuming you have your results in $results array:
foreach($results as $result) {
//create your table
$result['id']; //has the item id
$result['title']; //has item title
//and so on...
}
HERE is a great tutorial on looping through mysql result sets :D (W3Schools)
Another one HERE
To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data
You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)
HERE is a good and easy to understand tutorial on HTML tables.
while ($data=mysql_fetch_array($rs)) {
}

Different <li> class based off of category or tag in Wordpress

My goal is to have action icons in lists that correspond to how the list item is tagged or categorized.
For example, if I have a list item that is a webinar it should have a webinar icon next to it, the next item in the list might be a white paper, which should have a white paper icon next to it. The population of the icons should be controlled by how the list item is tagged or categorized.
I don't know where to start; any suggestions are appreciated. Thanks!
EDIT:
Thought it might be helpful if I show the list I'm wanting to modify - technically, the items that I want to modify are in the span class=meta" section, but I'm open to using whatever method worls:
<ul class="sub_nav">
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active last">
<a href="#resource_center" title="Resources II">
Featured Resources
</a>
</li>
</ul>
<div id="resource_center">
<?php
$resources = get_posts("post_type=resource&posts_per_page=2&cat=31&tag=featured");
?>
<?php foreach ($resources as $key => $resource): setup_postdata($resource); ?>
<?php if ($key === 0): ?>
<?php endif ?>
<p><span class="meta"><?php echo apply_filters('get_the_date',mysql2date('m.d.Y', $resource->post_date), 'm.d.Y') ?></span>
<?php echo $resource->post_title ?> – <?php echo strip_tags(get_the_excerpt()) ?></p>
<?php endforeach; ?>
<span class="more">Read More</span>
</div>
Just name all your icons after the tags they correspond to and put them in the same folder on your server (let's say http://www.yoursite.com/tagicons)
In your loop, just iterate the meta tag inside an image tag
<img src="http://www.yoursite.com/tagicons/{$tag}.png" />
Paste the code you're using to iterate your list items if you need more help.
Cheers
-D
EDIT:
I se you're using wordpress.
Refer to http://codex.wordpress.org/Function_Reference/wp_get_post_tags
to see how to get the tags you're looking for.
If you are generating the list inside the WordPress loop you can add the category as a class to the list element. For example:
...loop started
$categories = get_the_category();
$ids = '';
foreach ($categories as $category) {
$ids .= ' ' . $category->slug;
}
echo '<li class="' . $ids '">This item</li>';
...more loop
Then utilize CSS to style the list block.
While I think both of these solutions would have worked, I decided to go with a third solution I discovered as I researched options to meet my use case. This one was ideal because I was able to seamlessly fit it into my existing code structure and because I have a relatively low number of resources that I need to add the featured image to.
I added the code below, which basically uses the post's featured image as a left-aligned thumbnail.
<?php if ( has_post_thumbnail()): ?>
<?php
$thumb_id = get_post_thumbnail_id($resource->id);
$args = array(
'p' => $thumb_id,
'post_type' => 'attachment'
);
$thumb_image = get_posts($args);
$thumb_caption = $thumb_image->post_excerpt;
?>
<?php if (!empty($thumb_caption)): ?>
<div class="caption"><?php echo $thumb_caption ?></div>
<?php endif ?>
<?php the_post_thumbnail('sidebar-thumb'); ?>
<?php endif; ?>
Followed by this code snipped to grab the image and put it by the list item:
<?php echo get_the_post_thumbnail($id, 'thumbnail', array()); ?>
Here's a screen shot of my test site list section after I added the code - it's exactly what I was looking for:
Thanks for the suggestions and help, it got me moving in the right direction!

Magento: Getting Product Url's for Products within a Grouped Product

For a grouped product, I would like to display a link to the simple products it is composed of. For example, if I have a grouped product called Dining Set composed of plates, knives, forks, etc. I'd like each of the subproducts to have a link to that subproduct (click plates goes to the Simple Product for plates)
<?php foreach ($_associatedProducts as $_item): ?>
<tr>
<td><?php echo $this->htmlEscape($_item->getName()) ?></td>
<td class="a-right">
<?php echo $this->getPriceHtml($_item, true) ?>
</td>
<?php if ($_product->isSaleable()): ?>
<td class="a-center">
<?php if ($_item->isSaleable()) : ?>
View
<?php else: ?>
<p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock.') ?></span></p>
<?php endif; ?>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
This is a code snippet from the grouped.phtml file in
app/design/frontend/blank/default/template/catalog/product/view/type/grouped.phtml
In particular the line that has $_item->getProductUrl(),
this does not work, and I don't know the code needed to get the url for this associated product item. If anyone could help here it would be much appreciated.
Also, where on earth can I find the method's available (and how they're used) for Products or Categories or $_item and the like?
Easy to find all methods and functions. Always trace back to the Core /app/code/core/Mage/Catalog/Model/Product.php or any of the other files in that Folder.
Your code is perfect. Just use
$_item->getUrlPath() ;
instead of productURL.
Just a few notes on getting the available methods / data:
First, to get all methods actually coded into the classes, you can get all the available methods with:
$array = get_class_methods($_item); //yields an array of the methods in the class
var_dump($array); // to see the methods
To get all data-related methods, first find out the data members in the class. This works with most objects in Magento:
$data = $_item->getData(); // $key => $value array
Then you can get any piece of data you want two ways:
// assuming I want 'my_data'
$data = $_item->getMyData();
$data = $_item->getData('my_data');
<?php echo $this->htmlEscape($_item->getProductUrl()) ?>
or here is the whole A HREF:
<a href="<?php echo $this->htmlEscape($_item->getProductUrl()) ?>">
<?php echo $this->htmlEscape($_item->getName()) ?>
</a>

Categories