Magento rounds up the custom product price - magento

I'm setting a custom price depending on each customer. Using a webservice I receive the specific price for each customer in the format: XX,DD
When I use the function setprice in order to set the new price for the product:
$product->setFinalPrice($price);
Magento rounds up the price and, for example, if the $price is 38,50, then it sets the product price to 38. I've tried changing the ',' for '.' with str_replace, but in this case, it seems it can't set up the price.
How can I tell Magento to use the decimals as well?
Thanks

First you should convert your price to decimal number:
$value="38,50";
$amount = Zend_Locale_Format::getNumber($value,array('locale'=>'nl_NL'));
// or you can use str_replace
In Magento products has two types of standard price:
$product->getPrice();
$product->getSpecialPrice();
finalPrice - it is not actually product value, it will be calculated by Magento based on price, special_price, tier price and so on. You should set price value and save the product:
$value = "38,50"; //this decimalformat is used in nl_Nl locale
$amount = Zend_Locale_Format::getNumber($value,array('locale'=>'nl_NL'));
$amount = Mage::app()->getStore()->roundPrice($amount); //round price based on Magento logic
$product->setPrice($amount); //price - is actual value of product
// some extra code here
$product->save();

I'll throw in that you can use:
echo number_format($_product->getPrice(), 2)
It will give you a couple of decimal places.

I am guessing you do not want the price to round up. If that is the case then your solution depends on the version of PHP you are using.
PHP 5.3 you can simply use the round function to round it down like so:
round($price, 2, PHP_ROUND_HALF_DOWN);
If you are using PHP 5.2 you do not have the luxary of the PHP_ROUND_HALF_DOWN so you have to put the following function in somewhere (a helper makes the most sense to me) and call it:
floor($line_item_price * 100) / 100;
What this does is first multiply the value with 100 and then floor the value. This gives you a rounded down value with the precision of 2. Then divide by 100 to get the correct value.
The number 100 comes from the power(10, desired precision)
I hope you are on PHP 5.3. I did not enjoy having to do the PHP 5.2 solution very much.

To round up a price in magento you need overwrite a directory/currency model method convert.
Mage_Directory_Model_Currency::convert()
here you will find like this
return $price*$rate
and need to set like this
return round($price*$rate);

Related

codeigniter shopping cart is not saving decimal qty value

codeigniter shopping cart is not saving decimal qty value, If the user inputs 2.5 as qty it accepts as 25. so how to make qty as double or float type.
in side cart system library i have changed
$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
to
$items['qty'] = trim(preg_replace('/([^0-9\.])/i', '', $items['qty']));
but nothing changed. please i need help!
I suggest you don't use it because it is DEPRECATED
https://www.codeigniter.com/userguide3/libraries/cart.html
you can see in this link

Magento: Multiple Currency Conversion on Custom Options Prices

I have several websites using different currencies on each store view. The base currency (default) is set to GBP.
If I add a fixed price to a custom option the currency is converted according to store currency which is fine.
The problem starts when I want to update other values afterwards as it keeps converting the price.
For example, Let’s say I have a product on Store View 1 with a custom option that adds 100 GBP for a blue color option.
The same product will appear on Store View 2 with a custom option that adds 120 EUR for a blue color option, according to the correct currency conversion.
If I change any value on Store View 2, for example, changing the name of the product or adding an image, a change that doesn’t involve the price or the custom options, and than I save the change, it will continue to convert the price of the custom option.
If it was 120 EUR it will refer to it as if it is a new value of 120 GBP and convert it to 143.5 EUR and so on.
If I’ll click save again it will convert it again to 171.5 EUR and so on.
This happens because Magento refers to the custom options added price as a new value that needs to be converted.
Any idea how can I solve this as every time I change a value of a product it changes the custom options price?
I know that this is quite old question, but this bug still occurs (even in Magento 1.9) so maybe my answer will help somebody.
You have to overwrite Mage_Catalog_Model_Resource_Product_Option_Value class.
First add this method:
protected function checkIfPriceHasNotBeenChanged($object, $storeId)
{
$newPrice = (float)sprintf('%F', $object->getPrice());
$priceTable = $this->getTable('catalog/product_option_type_price');
$select = $this->_getReadAdapter()->select()
->from($priceTable, 'price')
->where('option_type_id = ?', (int)$object->getId())
->where('store_id = ?', $storeId);
$oldPrice = $this->_getReadAdapter()->fetchOne($select);
return $newPrice == $oldPrice;
}
Then use it in _saveValuePrices method:
if ($object->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
&& !$object->getData('scope', 'price')) {
$baseCurrency = Mage::app()->getBaseCurrencyCode();
$storeIds = Mage::app()->getStore($object->getStoreId())
->getWebsite()
->getStoreIds();
if (is_array($storeIds)) {
foreach ($storeIds as $storeId) {
if ($priceType == 'fixed') {
if ($this->checkIfPriceHasNotBeenChanged($object, $storeId)) {
continue;
}
(...)

load product using custom attribute as well as if product qty is greater then 0 in magento?

Am trying to do special offers to my magento shop.Actually that is easy but actually my scenario is differs from others.My scenario is,I must get the products based on custom attribute(date of sale) and as well as product qty must be greater than zero(Qty>0).I got the products using custom attribute like,
<?php
$collection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')- >getProductAttributes())
->addMinimalPrice()
->addStoreFilter();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
$collection->addAttributeToFilter('date_sale', array('lt' => $todayDate));
return $collection;?>
On this i got the products based on date of sale is lesser than today date.On that itself i must get products which qty is greater than 0.
give me some hopes guys?
After the filter use another addAttributeToFiler('attribute code of inventory quantity',array('gt' => 0));
This acts as AND filter
You can check 'and' and 'or' filter here OR and AND STATEMENT addAttributeToFilter magento asnd use according to requirement.

To add discount to the product which is 15 days old from created date

I want to add discount to the product when that product exceeds more than 15 days from created date.
Generally i know how to add discount to the product, but i felt this is difficult to set the discount with that condition. It make me as so confused. I have found where to add that attribute in combo box.
\app\code\core\Mage\SalesRule\Model\Rule\Condition\address.php
I got created date of product by $product->getCreatedAt()
But i totally confused "how to do that action?". If anybody know, Please help me guys!
Here i am givin you some brief idea to make customization in catalog rule to check if your product is old from created with 15 days and more.
Try to build your own module to manage this kind of catalog rule in your applicaion
you can take understand from this tutorial to manage catalog rule
specially just go throw last 4 slides to make your own rule to achieve your goal.
I have simply used shopping cart price rules by the following way.
first i had created product attribute named as days. Then I have set the value to that days textbox as programmatically as follows
require_once("app/Mage.php");
Mage::app();
$productCollection = Mage::getModel('catalog/product')->getCollection();
$date2=Date("Y-m-d");
foreach($productCollection as $_product)
{
$product = Mage::getModel('catalog/product')->load($_product->getEntityId());
$date1=$product->getCreatedAt();
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$product->setDays($days);
$product->save();
}
After that i have added that product attribute promo rules. Then i set the condition as if differerence between dates which are from product creation date to now is more than 15 days, to add discount to that particular product.

Magento: Fixed shipping cost BELOW certain basket price

How can I set the shipping cost for Magento for all baskets that are below a certain point. For example, all baskets under £5 have fixed shipping of £1.99.
I think this is what you are looking for, unless I have the wrong thing. It seems to be called Table Rate Shipping.
the only way i've managed to get it working is to add an extra case statement in my shipping module.
$result = Mage::getModel('shipping/rate_result');
...
} else if ($request->getPackageValue() < 6) {
$price = '1.99';
...

Resources