how to get the attribute group in magento - php

I need to get the attribute group of a certain attribute set , how can i do this ?
i think i got the attribute group id but i can't seem to get the attributes of that group.
$attributes = $_product->getAttributes();
foreach($attributes as $attribute)
{
$group_id = $attribute->getData('attribute_set_info/' . $_product->getAttributeSetId() . '/group_id');
print_r($group_id);
}
I would really appreciate if somebody could help me out , thanx ;)

Just use the ID to instantiate the model you want.
$product = Mage::getModel('catalog/product')->getCollection()->getFirstItem();
foreach($product->getAttributes() as $att)
{
$group_id = $att->getData('attribute_group_id');
//Mage_Eav_Model_Entity_Attribute_Group
$group = Mage::getModel('eav/entity_attribute_group')->load($group_id);
var_dump($group);
}

You can try this, will return all Attribute Group of Magento
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
->load();
foreach ($attributeSetCollection as $id=>$attributeGroup) {
echo 'group-name: '; echo $attributeGroup->getAttributeGroupName();
echo '<br>';
echo 'group-id: '; echo $attributeGroup->getAttributeGroupId();
echo '<br>';
echo 'set-id: '; echo $attributeGroup->getAttributeSetId();
echo '<br>';
}

Related

why do I get only 3 return values by using a json link:

there should be many more return values?
print_r($data);
shows that there is much more to be displayed.
Thanks for any hints,
Stefan
<?php
$data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-DOGE&type=sell'),TRUE);
//print_r($data);
$ncount = COUNT( $data );
for($i=0;$i<$ncount;$i++){
echo $data['result'][$i]['Quantity'] .'<br />';
}
?>
$ncount should be the count() of $data['result'];
Try this:
<?php
$data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-DOGE&type=sell'),TRUE);
$ncount = count($data['result']);
for($i=0;$i<$ncount;$i++){
echo $data['result'][$i]['Quantity'] .'<br />';
}
?>
While not a definite answer, you're running count on $data, but then displaying $data['result']. So if the root array only contains 3 values, you'll only go through the loop 3 times. Instead, try a foreach:
foreach ($data['result'] as $result){
echo $result['Quantity'] .'<br />';
}
You are making things WAY too difficult .. Set result to data['result'] and iterate through that
<?php
$data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-DOGE&type=sell'),TRUE);
$result = $data['result'];
foreach($result as $item){
echo $item['Quantity'] . '<br />';
}
?>

how to pass input value via jquery to php in magento adminhtml

In Catalog > Attribute > manage atrribute > Manage label/ options. I have created a module that will pull/display
options of selected category. i have done all the code that will do the work.
Now the problem is here:
i want to pass a value of selected option (category id) from html list.
This is how I pull and display the list of my categories.
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Attribute/Edit/Tab/Options.php
public function getCategoryCollection(){
$parentCategoryName = 'Default Category';
$collection = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', $parentCategoryName)
->getFirstItem()// The parent category
->getChildrenCategories();
$html = "<select>";
$html = $html. "<option>Select</option>";
foreach ($collection as $items => $item) {
$html = $html . "<option value=" . $item->getEntityId() . ">" . $item->getName() . "</option>";
}
$html = $html . "</select>";
return $html;
}
this is the function I want to pass when user do "onchange()" on the html select list
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Attribute/Edit/Tab/Options.php
public function getCategoryOptions(){
// Id of category we want to get attributes for
$categoryId = 123;
$sizeOption = array();
// Products in that category
$products = Mage::getModel('catalog/category')->load($categoryId); //put your category id here
$productslist = $products->getProductCollection()->addAttributeToSelect('*');
foreach($productslist as $product)
{
$sizeOption[] = $product['attribute_variant'];
}
return $sizeOption;
}
app/design/adminhtml/default/default/template/eav/attribute/options.phtml
I use "<td><?php echo $this->getCategoryCollection(); ?></td>" to display the select options list.
Your help would be appreciated.

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;

Shop with two items

I have two models. Shop and Item. I am trying to retrieve all of the shops, with 2 items each - however, I cannot get it working. The value of $shop['items'] is not changing.
foreach($shops as &$shop) {
foreach($shop->items as $item) {
$item->formatPreview();
}
$shop = $shop->toArray();
$shop['items'] = array_slice($shop['items'], 2);
}
How can I achieve this, and are there a smarter way using Eloquent?
EDITED
$shops = Shop::with('items')->get();
foreach ($shops as $shop) {
echo 'shop name: '. $shop->name . '<br>';
if($shop->items()->count()) {
foreach ($shop->items()->take(2)->get() as $item) {
echo 'item: ' . $item->name . '<br>';
}
}
echo '<hr>';
}
I was testing this inside a route closure so please format it as you want orfourse. Hope it is working now.

Accessing custom attribute on success.phtml

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.

Categories