Magento and decimels in the cart - magento

I'm changed some functions in magento to strip decimals in price.
The solution appears to work on simple products with no options, but the products with options still display the .00 when the option is chosen. Ironically the drop down for the option displays the additional cost of the option without the decimels, but the main price with the option selected still shows the decimels. Could this be in a js file? configurable.js has reloadOldPrice() method i tried to dump it, but the price var is always 0
any ideas?

NEW EDIT
My previous code won't work indeed. I tested the following and it works:
// Wrap original reloadPrice function
spConfig.reloadPrice = spConfig.reloadPrice.wrap(function(original){
// Call original reloadPrice() function
original();
// Get the DOM-element that contains the price
var priceSpan = $('product-price-'+this.config.productId).down();
// Get the current value
var oldP = priceSpan.innerHTML;
// Change the value
var newP = oldP.sub('.00','');
// Update the element
priceSpan.update(newP);
});
In Magento the spConfig object defined like var spConfig = new Product.Config(...);, so be sure to add the code I give here after instantiation of spConfig.
There is still work to be done:
I'd recommend changing the var line newP = oldP.sub('.00',''); to something that also catches ,00, since in some locales that will be the price format.
Furthermore, if you choose to, for example, show prices including and excluding tax the code above won't work because $('product-price-'+this.config.productId).down() will contain two elements (I think).
If you rather append the code to the configurable.js file, you should append it like:
Product.Config.prototype.reloadPrice = Product.Config.prototype.reloadPrice.wrap(...);
(notice the .prototype which I forgot in my first answer).
OLD POST (doesn't work)
In case people wonder why it doesn't work, first of all it should've been Product.Config.prototype.formatPrice instead of Product.Config.formatPrice and, secondly, the formatPrice function apparently isn't responsible for how the price html is outputted.
configurable.js also has a function formatPrice, which is probably called when the price is updated.
So you could try:
Product.Config.formatPrice = Product.Config.formatPrice.wrap(function(originalFormatPrice, price, showSign) {
var str = originalFormatPrice(price, showSign);
return str.slice(0, -3); // remove last three characters (.00)
});

**Go your price.phtml file**
line no 201
<?php echo $_coreHelper->currency($_price, true, true) ?>
replace this code
<?php $_prix = $_coreHelper->currency($_price,true,true) ?>
<?php $_prix = str_replace(".00", "", $_prix); ?>
<?php echo $_prix ?>

Try to use free extension ET Currency Manager. In this extension this function is realized.

Related

How do i display the selected value in multiselect while editing and updating the options(codeigniter)

How do i display the selected value in multiselect while editing and updating the options.
my multiselect function is:
foreach ($subjects as $sub){
$subject_selected[]=$sub['subject_name'];
}
echo form_multiselect('subject[]',$subject_list,'',$js,$subject_selected); ?>
if I m wrong please correct me..
It should be like
echo form_multiselect('subject[]',$subject_list,$subject_selected,$js); ?>
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected. The parameter usage is identical to using form_dropdown() above, except of course that the name of the field will need to use POST array syntax, e.g. foo[].
I have searched for this thing and have failed so many times. But at last find a way around to resolve the issue.
This is the way that I have came across with.
<?php
$manpower_list = array();
$manpowers = $this->db->select('m.id, m.manpower_name')
->from('task_manpower tm')
->join('manpower m', 'tm.manpower_id = m.id')
->where('tm.deleted', 0)->where('m.deleted', 0)
->where('tm.task_id', $model_info->id)
->get()->result();
foreach ($manpowers as $power) {
$manpower_list[] = $power->id;
}
?>
<?php echo form_dropdown("manpower_id[]", $manpower_dropdown, $manpower_list, "class='select2 validate-hidden form-control' id='manpower_id' multiple='multiple'"); ?>
Hope it helps someone.

Magento getProductUrl() is not returning the right url (random?)

I am using Magento 1.5.0.1 and the getProductUrl() function used in the cross sell and up sell blocks on the product page is throwing up different URL formats.
Either the correct url like:
/laptop-bag.html
Or the wrong one (well it works, but of course its not the rewrite URL):
/catalog/product/view/id/825/s/laptop-bag/category/16/
Sometimes both cross sell and up sell blocks return the correct URL, sometimes both use the longer version, and in some cases, one uses the correct and the other uses the long version??
Any ideas why this is happening?
I have already run a magento database repair, reindexed, and refreshes / flushed all caches.
Try $product->getUrlPath() instead of $product->getProductUrl()
UPDATE: As per below comment by #jordan314, Magento recommends to EE customers:
The url_path attribute is no longer used as of 1.13 but is still available for backward-compatibility, and Magento will not assign a value to it for new products, so it's not recommended to continue using it. Perhaps you could try using $product->getProductUrl() instead.
The incorrect url is generated because it can't find the rewritten url.
Maybe it is caused because incorrect store_id.
eg:
$id = 290;
Mage::app()->setCurrentStore('default');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";
//change store id
Mage::app()->setCurrentStore('admin');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";
result:
store_id: 1
http://local.com/surestep-pro-diabetic-test-strips-50-strips-professional-care.html
store_id: 0
https://local.com/index.php/catalog/product/view/id/290/s/surestep-pro-diabetic-test-strips-50-strips-professional-care/
The correct url rewrite can be found in table named core_url_rewrite (including the information about the store_id)
If it found match value in core_url_rewrite, it will generate 'the correct url' else it will concat the product_id + url key + category_id
$routePath = 'catalog/product/view';
$routeParams['id'] = $product->getId();
$routeParams['s'] = $product->getUrlKey();
if ($categoryId) {
$routeParams['category'] = $categoryId;
}
Try add this when you're getting your collection
$collection->addUrlRewrite();
It has helped me.
$id = 10;
Mage::app()->setCurrentStore('admin');
$url = Mage::helper('catalog/product')->getProductUrl($id);

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