Accessing custom attribute on success.phtml - php

I have a custom attribute which i created to track the profit of each item.
For analytical purposes, I need to add this to a variable in JS and append it to a url string.
Unfortunately, I can’t seem to get access to the attribute and every time I try to echo the value, it returns null.
Here is the code;
$orderObj = Mage::getModel(’sales/order’)->loadByIncrementId($this->getOrderId());
$orderItems = $orderObj->getAllItems();
$basket = ‘’;
$mail_body = ‘’;
foreach($orderItems as $item)
{
$basket .= $item->getSku() .’|’. number_format($item->getRowTotal(), 2, ‘.’, ‘,’) .’|’. round($item->getQtyOrdered()) . ‘,’;
}
foreach($orderItems as $item) {
$product_item = Mage::getModel(’catalog/product’)->load($this->getProductId());
$mail_body .= $product_item->getAttributeText(’profit’);
$mail_body .= “---\n\n”;
}
the main code which I am trying to get to work is in the foreach.
Any ideas why this does’nt return a value?

Try
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
foreach($items as $item){
$product = Mage::getModel('catalog/product')->load($item->getProductId());
echo $product->getAttributeText('profit');
}

$custom = Mage::getModel('catalog/product')->load($item->getProductId());
echo $custom->getAttributeText('profit');
OR
This is the working solution, it's so simple, yet I don't understand:
<?php
$custom = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $custom->getAttributeText('profit');
?>
hope this will sure help you.

Related

How to get gravatar in wordpress post using php?

I know the get_avatar() function but its not working. Maybe its due to the complexity of the loops? Please take a look at the code below and let me know! Thanks!
function displaymeta(){
global $post;
$m_meta_description = get_post_meta($post->ID, 'my_meta_box_check',
true);
global $wpdb;
$user_nicenames = $wpdb->get_results("SELECT id,user_nicename FROM
{$wpdb->prefix}users", ARRAY_N);
foreach($user_nicenames as $nice_name)
{
foreach($nice_name as $name)
{
foreach($m_meta_description as $val)
{
$flag=strcmp($name,$val);
if($flag==0)
{
echo"<li>";
echo $name. "<br>";
echo get_avatar($name->ID,50);
echo"</li>";
}
}
}
}
}
add_filter( 'the_content', 'displaymeta' );
I've tried $name,$nice_name, $user_nicenames in function get_avatar($val->ID,50); but nothing seems to work! What am I missing here?
You've already used the right function, which is get_avatar().
But the problem is that the $name as in get_avatar($name->ID,50) is not an object. Instead, it's a string, which could be the user ID or display name (i.e. the user_nicename column in the WordPress users table).
So try replacing the foreach in your displaymeta() function with the one below, where I assigned $name to $nice_name[1], and the user ID is assigned to $user_id:
foreach($user_nicenames as $nice_name)
{
$user_id = $nice_name[0];
$name = $nice_name[1];
foreach($m_meta_description as $val)
{
$flag=strcmp($name,$val);
if($flag==0)
{
echo"<li>";
echo $name. "<br>";
echo get_avatar($user_id,50);
echo"</li>";
}
}
}
Additional Note
If you remove the , ARRAY_N as in: (but you don't have to remove it. These are just extra info..)
$user_nicenames = $wpdb->get_results("SELECT id,user_nicename FROM
{$wpdb->prefix}users", ARRAY_N);
then the variable $nice_name would be an object. Hence you can then access $nice_name->user_nicename like this:
$user_id = $nice_name->id;
$name = $nice_name->user_nicename;
UPDATE
In reply to your comment on the missing content, it's because you didn't capture the variable that WordPress passes through the the_content filter. And you also need to append the LI's to that $content, and finally return the modified content (i.e. $content).
So try this code (which is already utilizing the new foreach code as I provided before or above):
function displaymeta( $content ){
global $post;
$m_meta_description = get_post_meta($post->ID, 'my_meta_box_check',
true);
global $wpdb;
$user_nicenames = $wpdb->get_results("SELECT id,user_nicename FROM
{$wpdb->prefix}users", ARRAY_N);
// Add the opening UL tag. Remove if not needed.
$content .= '<ul>';
foreach($user_nicenames as $nice_name)
{
$user_id = $nice_name[0];
$name = $nice_name[1];
foreach($m_meta_description as $val)
{
$flag=strcmp($name,$val);
if($flag==0)
{
$content .= "<li>";
$content .= $name. "<br>";
$content .= get_avatar($user_id,50);
$content .= "</li>";
}
}
}
// Add the closing UL tag. Remove if not needed.
$content .= '</ul>';
return $content;
}
add_filter( 'the_content', 'displaymeta' );
Hope that helps! =)

php - Retrieve value from $_SESSION and output using another variable

My question might seem totally silly but I am really stuck here.
I know $_SESSION is a global variable but I can't seem to know how to retrieve the value it stores.
For example:
<?php
if(isset($_SESSION["cart_products"]))
{
foreach ($_SESSION["cart_products"] as $cart_itm)
{
//set variables to use in content below
$product_name = $cart_itm["product_name"];
$product_code = $cart_itm["product_code"];
echo '<p>'.$product_name.'</p>';
echo '<p><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></p>';
}
}
echo $_SESSION["cart_products"];
?>
Here you can see, the $_SESSION["cart_products"] holds some value (information like product name, product code etc). Now, the problem is that I just want to echo out all the product names that are stored in $_SESSION["cart_products"].
Since it's a cart list, it contains more than ONE product details. But, when I echo out $product_name, it only shows the name of the last product in the list. And echoing out $_SESSION["cart_products"] gives array to string error.
Please, tell me how can I list out all the product names separating by a ,.
Any help will be greatly appreciated.
Thanks in advanced.
PS: I have already tried using implode() function.
for displaying all the product name separated by , use this code.
$allProductName = '';
$prefix = '';
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$allProductName .= $prefix . '"' . $product_name. '"';
$prefix = ', ';
}
echo $allProductName;
Here's a edited version of your code
<?php
//you need to start session first
session_start();
$item = ["product_name" => "BMW", "product_code" => "540M"];
$list = array( $item, $item );
// Assuming you session list
$_SESSION[ "cart_products" ] = $list;
if(isset( $_SESSION["cart_products"])) {
foreach ( $_SESSION["cart_products"] as $cart_itm ) {
//set variables to use in content below
$product_name = $cart_itm["product_name"];
$product_code = $cart_itm["product_code"];
echo '<p>'.$product_name.'</p>';
echo '<p><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></p>';
}
// to print names seperated by ','
$temp = "";
foreach ( $_SESSION["cart_products"] as $cart_itm ) {
$temp .= $cart_itm["product_name"] . ", ";
}
echo $temp;
}
// you cant print array using echo directly
print_r( $_SESSION["cart_products"] );
?>
Finally, I got the answer to my query with the help of #Christophe Ferreboeuf. However, some modification was still needed.
Here's the corrected code:
$allProductName = '';
$prefix = '';
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$allProductName .= $prefix . '"' . $cart_itm["product_name"]. '"';
$prefix = ', ';
}
echo $allProductName;

Get Configurable Options in MiniCart Magento

I need to show configurable option, I have this code
$productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
if($productOptions){
if(isset($productOptions['options'])){
foreach($productOptions['options'] as $_option){
$productser = $_option['option_id'].','.$_option['option_value'];
}
}
$superAttrString ='';
if(isset($productOptions['info_buyRequest']['super_attribute'])){
foreach($productOptions['info_buyRequest']['super_attribute'] as $key => $_superAttr){
$superAttrString .= '&super_attribute'.'['.$key.']='.$_superAttr;
}
}
if($superAttrString):
$superAttrString.'&qty=1';
endif;
}
$html .='<span class="label">Configurable Options: # '.$superAttrString.'</span>
For now the result of the $superAttrString variable is
&super_attribute[92]=3&super_attribute[135]=5
How can i make to show there Labels instead that ID's
Thank you so much
I think something like this should work, inside your foreach:
foreach($productOptions['info_buyRequest']['super_attribute'] as $key => $_superAttr){
$attr = Mage::getModel('catalog/resource_eav_attribute')->load($key);
$label = $attr->getSource()->getOptionText($_superAttr);
$superAttrString .= '&super_attribute'.'['.$attr->getAttributeCode().']=' . $label;
}

RSS feed limit items

How can i limit the items from my RSS-feed? (Example: 5 items). I hope someone can help me with this code.
<?php
$url = "LINK TO RSS";
$rss = simplexml_load_file($url);
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = $item->description;
echo '<p class="rss-feed">'.$title.'</p>';
echo '<p>'.$description.'</p>';
}
}
?>
If you want to limit the number of items displayed you can use the array_slice function to store a subset of the retrieved values in $items, e.g. change:
$items = $rss->channel->item;
to:
$items = array_slice($rss->channel->item, 0, 5);
If you want to change the number of items fetched from the URL then you need to check whether/how this URL supports this behaviour.

Scraping a table using Simple HTML Dom

I am trying to scrape this product table,
http://www.dropforyou.com/search2.php?mode=search&posted_data%5Bcategoryid%5D=2&posted_data%5Bsearch_in_subcategories%5D=on
I need the product id, the quantity and the price.
Since the site uses cookies and a post form I am grabbing the site with CURL. Which works fine. I am then loading that into simple html dom with $html = str_get_html($content);
I have been able to load all the table values into an array, however I can't label them. They just come in as 0,1,2 and I can't tell what's what.
I tried using a different method posted here on stackoverflow, however it gives me Fatal error: Call to a member function find() on a non-object in
My working code that isn't labeled
$content = curlscraper($urltoscrape);
$html = str_get_html($content);
$tds = $html->find('table',2)->find('td');
$num = NULL;
foreach($tds as $td)
{
$num[] = $td->plaintext;
}
echo '<pre>';
var_dump ($num);
echo '</pre>';
The code I found on Stackoverflow that just gives me Fatal error: Call to a member function find() on a non-object in
$content = curlscraper($urltoscrape);
$html = str_get_html($content);
foreach($html->find('tr',2) as $page)
{
$item['sku'] = $page->find('td',0)->plaintext;
$item['product'] = $page->find('td',1)->plaintext;
$item['Qty'] = $page->find('td',2)->plaintext;
$item['description'] = $page->find('td',3)->plaintext;
$item['price'] = $page->find('td',4)->plaintext;
$table[] = $item;
}
print_r($table);
Try initialize variable for your foreach function and then use your code. But you don't say which line created that error?
$line = $html->find('tr',2);
foreach($line as $page)
{
//var_dump($page) //You can check array
$item['sku'] = $page->find('td',0)->plaintext;
$item['product'] = $page->find('td',1)->plaintext;
$item['Qty'] = $page->find('td',2)->plaintext;
$item['description'] = $page->find('td',3)->plaintext;
$item['price'] = $page->find('td',4)->plaintext;
$table[] = $item;
}

Categories