magento weight attribute without decimal points - magento

I need to show weight attribute without any decimal points in admin panel on front end that is working fine but I need to remove the 4 decimal points from admin panel where site admin enter the product details.
Any help in terms of code or database changes will be appreciated.
image url : http://sale24by7.com/weight.png
Thanks & Regards

You can use Event
adminhtml_catalog_product_edit_prepare_form
To catch the form output and modify it as you want !
The method will look like this :
public function renderWeight( Varien_Event_Observer $observer )
{
$form = $observer->getForm();
$element = $form->getElement('weight'); // Weight attribute from the Form Data
if($element){
$oldWeight = $element->getValue(); // Weight Value you want to modify
$values['weight'] = (int) $oldWeight; // Assign the new Weight Value
$form->addValues($values); // Add it to the form
}
}
I have created the module you downloaded from here Download

I just use this:
.$this->htmlEscape(number_format($weight));

To change the decimal precision of attributes that have a backend_type set to decimal,
You have to change the type of value in this table :
catalog_product_entity_decimal
If you have not change yet, you should see :
decimal(12,4)
You should replace with
decimal(12,0)

Related

Magento - Get Attribute Set Group Name

Sorry I got my initial question wrong.
I want to get the attribute set name in the compare products page (the page that compares products side by side)
I want to get the Group names, that fall under the attribute set. U know how the attribute sets are maintained ( Attribute Set > Groups > Attributes ) So that I can display the compare page product attributes by group name. How can I get the group names?
I'm not sure how to do this, can someone point me to the right code or right direction?
The template being used for this page is the default one at:
template\catalog\product\compare\list.phtml
To fetch the attribute set name in the compare products page, you can try the following code in your template\catalog\product\compare\list.phtml
After this line of code
<?php foreach($this->getItems() as $_item): ?>
Paste this code to print your attribute set name
// Get attribute set model.
$model = Mage::getModel('eav/entity_attribute_set');
// Get attribute set id.
$attributeSetId = $_item->getAttributeSetId();
$attributeSet = $model->load($attributeSetId);
// This is attribute set name.
$attributeSetName = $attributeSet->getAttributeSetName();
echo $attributeSetName;
The above code will print your attribute set name and based on your HTML requirement, you can alter the code.

How to get attribute codes of super product attributes of a configurable product

For example, a configurable product with attributes Size and Color, I need to get the attribute codes of the above attributes.
or to be more specific, I need to know whether an attribute is used to configure a configurable product. I need this to check at product list page
try using this code
$config_product = Mage::getModel('catalog/product')->load($config_product_id);
$productAttributeOptions = $config_product->getTypeInstance(true)->getConfigurableAttributesAsArray($config_product);
Create an array as shown below:
$attributeValues['additional_options'][$count]['label'] = $aAttr['name'];
$attributeValues['additional_options'][$count]['value'] = $aAttr['value'];
Then pass the array while adding items to the order:
if (!empty($product['product_options'])) {
$orderItem->setProductOptions($product['product_options']);
}
Here $product['product_options'] is the array that we have created in the first step.

getRate() and Magento tax percentage

I'm trying to get the tax rate (percentage, not currency) for a given postcode so I can display it in a third-party quote PDF printout (no relation to the "quote" Magento uses as the shopping cart pre-checkout). While I'm still relatively new to Magento it appears that getRateRequest() and getRate() are the two main functions which get the tax rate based on all the variables (product tax class, customer tax class, etc.).
Since this is for a third-party extension and all our products are taxable I figured I would just use getRate() with the correct Varien Object input and it would return the tax rate. After a week of trial and error I can't figure out why I'm always getting a rate of zero. I've confirmed I'm calling the getRate() function and that it's not returning zero from the first if() statement checking for Country and Customer/Product class ID. In addition I've confirmed all the variables are being passed on and accessible in the getRate() function itself.
I've created an object with the below input (based on the output of getRateRequest()) that I call with getRate() and am hoping someone can shed light on what is wrong with my data input or why the getRate() function is always returning a result of zero. (I'm actually setting with $variables below, they are just defined earlier up and one of my test case values are below)
// UPDATED CODE (variable values come from 3rd party quote extension)
$country = 'US'; // use short country code
$region = '12'; // must be numeric!
$postcode = '95050';
// our quote extension stores the customer id ('2') which we use to get the tax class
$customer = Mage::getModel('customer/customer')->load( '2' );
$custTax = $customer->getTaxClassId();
$TaxRequest = new Varien_Object();
$TaxRequest->setCountryId( $country );
$TaxRequest->setRegionId( $region );
$TaxRequest->setPostcode( $postcode );
$TaxRequest->setStore( Mage::app()->getStore() );
$TaxRequest->setCustomerClassId( $custTax );
$TaxRequest->setProductClassId(2); // 2=taxable id (all our products are taxable)
$taxCalculationModel = Mage::getSingleton('tax/calculation');
$rate = $taxCalculationModel->getRate($TaxRequest);
My backup plan is to just do a direct SQL lookup formula although that will probably get a bit messy. Since our web development team didn't exactly follow good coding standards an eventual site re-write is in my future anyway once the initial launch fixes are in (all 4 pages of them).
Thanks for any help and taking the time to read this.
EDIT - Stack Overflow is awesome :)
You can also try this
$store = Mage::app()->getStore('default');
$request = Mage::getSingleton('tax/calculation')->getRateRequest(null, null, null, $store);
$taxclassid = $product->getData('tax_class_id');
$percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxclassid));
If you change:
$TaxRequest->setRegionId(California);
to
$TaxRequest->setRegionId($stateId);
where $stateId is numeric region id. Your code should work then.

How to set default selected options in magento product detail page

I have requirement in which i am getting product ID from external Application with product super_attribute options like color,size. I am getting all those and i can do the add to cart option.
But here my actual requirement is to select the requested options by customer and redirect them to product detail page in magento so that here they can still enter optional text to print. So i need to set the requested options in detail page and redirect them to product detail page instead of adding it to cart. They will enter more details and then they will do addtocart manually.
How can i set the selected options while loading itself.
Please help me.
Thankfully this is already built in to most themes. You need to know the ID of both attributes and values from the Simple product that is included in the Configurable product. I've only seen it work with Configurable types so that might be a limitation.
For each attribute make a key=value pair
Combine the attributes as a query string, eg. "123=345&678=890"
Append the string as a hash fragment (not as a query).
This is an example from the official demo, note that the first option needs to be selected for the second to work so it has two key/value pairs.
http://demo.magentocommerce.com/zolof-the-rock-and-roll-destroyer-lol-cat-t-shirt-126.html#525=99&272=22
Rough Magento2 example add the below for pre-selecting the custom options by name=value in:
/www/mysite/app/design/frontend/mycompany/mytheme/Magento_Catalog/templates/product/view/options.phtml
The below code looks at the label of the option and the text value of the select. And depends on your theme structure. Example below for Luma.
It expects the following format in the url
product.html?SelectLabel=OptionValue&SelectLabel=OptionValue
This does not account for multi language etc.. you could easily adapt it to instead look for the select id and option id which would be more accurate replacing
$(label).parents().eq(1).find('select option:contains('+arr[k]+')').attr('selected', true);
with (untested)
$("#"+k+" option[id='"+arr[k]+"']").attr("selected", "selected");
<script>
require(['jquery'],function($){
$(document).ready(function(){
function getJsonFromUrl() {
var query = location.search.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
}
var arr = getJsonFromUrl();
for (var k in arr){
if (arr.hasOwnProperty(k)) {
//alert("Key is " + k + ", value is" + arr[k]);
var label = $('.product-options-wrapper').find("span:contains('"+k+"')");
$(label).parents().eq(1).find('select option:contains('+arr[k]+')').attr('selected', true);
}
}
});
});
</script>

How do I get attribute set name?

I am trying to get attribute set name in Magento product view template. I can get attribute value by $_product->getAttributeText('attribute'), but how do I get attribute set name?
I would like to display an attribute only if it is belong to a certain attribute set.
Whenever you have a product object, you can access its attribute set like this:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
This will give you the name of the attribute set, which you can then compare using strcmp:
if(0 == strcmp($attributeSetName, 'My Attribute Set')) {
print $product->getAttributeText('attribute');
}
For more sexyness you can shorten it to:
$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
Try the following code:
$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Default';
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
echo $attributeSetId;
Find more info about Attribute Set in the following article.
Thanks
Joe's answer requires a couple of alterations in order for it to work.
Firstly it should be $_product not $product, and secondly there is an erroneous ')' in the last line.
The following code should be correct:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
Comparing to a text value can have problems if users decide to later change that text - which is easy to do in Magento for attribute sets. One other option is to use the underlying id instead which is never going to change.
You can get this by looking up the value of the attribute_set_id column in the database using
select * from eav_attribute_set;
This number is also in the edit link in admin which is in bold below
http://.../index.php/admin/catalog_product_set/edit/id/10/key/6fe89fe2221cf2f80b82ac2ae457909ce04c92c51716b3e474ecad672a2ae2f3/
Your code would then simply use that property of the product. Base on the id of 10 in the link above this would just be
if (10 == $_product->getAttributeSetId()) {
//Do work
}

Resources