cannot render the weight - php

I'm working on a website. My client want me to show the weight of product. But the condition is if the weight is less than a Kg, it should show the grams. If it is more than a kg it should show the kg along with fraction point i,e if the weight is 1 kg 350 grams then it should show 1.35 Kg. My code for it goes like this.
<?php $_weight = $_product->getweight()?>
<?php if ($_weight >= 1) : ?>
<span><?php echo($_weight)?>Kg<span>
<?php else: ?>
<? $_weight1=($_weight * 1000) ?>
<span><?php echo(round($_weight1))?>grams<span>
<?php endif;?>
<br/>
But I don't know how to show the kg with only two fraction numbers. Please help me. I'm a budding developer.

You almost got there - when you use the round() function you can specify the decimal precision as the 2nd parameter.
<span><?php echo($_weight)?>Kg<span>
becomes
<span><?php echo(round($_weight,2))?>Kg<span>
http://php.net/round

<? $_weight1=($_weight * 1000) ?> line replace with <?php $_weight1=($_weight * 1000) ?> and try...
<?php $_weight = $_product->getweight(); ?>
<?php if ($_weight >= 1) { ?>
<span><?php echo round($_weight,2); ?>Kg<span>
<?php }else{ ?>
<?php $_weight1= round($_weight * 1000); ?>
<span><?php echo $_weight1; ?>grams<span>
<?php } ?>
<br/>

Related

how to do multiplication in php and mysql

I need to multiply the price by the quantity to get the total cost but i am not sure how to do this.
This is what i have done so far:
<?php do { ?>
<p align="Center">
<?php echo $row3['unitprice'] ?>
<?php echo $row3['quantity']; ?>
</p>
<?php } while ($row3 = mysqli_fetch_assoc($resultCustomer3))?>
at the moment i am only printing out unit price and the quantity.
how would i multiply the two to get the total?
HELP please!!
Try this
<?php while ($row3 = mysqli_fetch_assoc($resultCustomer3)) {?>
<p align="Center">
<?php echo $row3['unitprice']; ?>
<?php echo $row3['quantity']; ?>
<?php echo $row3['quantity']*$row3['unitprice']; ?>
</p>
<?php } ?>
To multiply two fields in php, you need this code:
$total = $mnth_190 * $rate_190;
After having calculated the result, you can normally output the variable total!

Return a Key from the Next and Last Element in Array / Foreach Loop

I am hoping to modify the manner in which discounts are displayed for an eCommerce platform (OpenCart - PHP MVC based).
The default behaviour is that the discounts will be displayed as:
5 or more: $20.00
10 or more: $18.00
20 or more: $16.00
I would prefer:
5 - 9: $20.00
10 - 19: $18.00
20+: $16.00
Stripping out the "or more" text is simple enough through the template file (code provided below).
For all but the last element, this would require taking the quantity key ($discount['quantity']) from the next element and applying a basic maths function (- 1), then returning this new value besides the original.
For the last element, I would need to simply return the last quantity value and add "+" text.
Original code (controller):
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$this->data['discounts'] = array();
foreach ($discounts as $discount) {
$this->data['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
);
}
Original code (template):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<span><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></span>
<?php } ?>
</div>
<?php } ?>
Modified code to strip "or more" text from template (Note: Separate echo's used to allow table formatting - To maintain simplicity, these tags are not included):
<?php if ($discounts) { ?>
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<?php echo $discount['quantity']; ?><?php echo $discount['price']; ?>
<?php } ?>
</div>
<?php } ?>
How can I further modify this code to return the quantities in the preferred format?
Note: The arrays are quite small but I would still consider performance as a priority.
Edit:
Thank you tttony for providing the solution below. This is the code I have used in the template file for the custom table formatting (without the sprintf/formatted string function).
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?> - <?php echo (int)$discounts[$i+1]['quantity'] - 1; ?></td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
<?php if (count($discounts)) { ?>
<tr>
<td><?php echo $discounts[$i]['quantity']; ?>+</td>
<td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
You can try this, very simple, in the product.php controller file
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$discounts_formated = array();
for ($i=0; $i < count($discounts) -1; $i++) {
$discounts_formated[] = sprintf("%s - %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1);
}
// Last discount: 30+
$discounts_formated[] = sprintf("%s+", $discounts[$i]['quantity']);
var_dump($discounts_formated);
Output:
array (size=3)
0 => string '10 - 19' (length=7)
1 => string '20 - 29' (length=7)
2 => string '30+' (length=3)
EDIT:
Edit the file product.tpl, I tested with OC 1.5.6.4 and it's working
<?php if ($discounts) { ?>
<br />
<div class="discount">
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<?php echo sprintf("%s - %s: %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1, $discounts[$i]['price']); ?><br />
<?php } ?>
<?php if (count($discounts)) { // last discount ?>
<?php echo sprintf("%s+: %s", $discounts[$i]['quantity'], $discounts[$i]['price']); ?><br />
<?php } ?>
</div>
<?php } ?>
Will print something like this:
10 - 19: $88.00
20 - 29: $77.00
30+: $66.00

How do I display a WAS and NOW price on my product page?

I've been having this problem for a while now. I use Magento with Bundle Products on this website:
http://canjicabrasil.com/bikinis/wave-brazilian-white-bikini.html
But when I apply a discount, it only shows the final price not a WAS and NOW like other sites.
What code should I use to call Prices in a way that:
If price is full it will display only one price
If Price has discount (on any of the budle items) it will display a WAS and a NOW price in the frontend of the site.
Anyone has come up with this solution?
This is the code on bundle.phtml
<?php endif; ?>
<?php if ($_product->isAvailable()): ?>
<p class="availability in-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('In stock') ?></span></p>
<?php else: ?>
<p class="availability out-of-stock"><?php echo $this->helper('catalog')->__('Availability:') ?> <span><?php echo $this->helper('catalog')->__('Out of stock') ?></span></p>
<?php endif; ?>
<div class="price-box-bundle">
<?php echo $this->getPriceHtml($_product) ?>
</div>
<?php echo $this->getChildHtml('bundle_prices') ?>
without looking at the code to see how you applying discounts and generating the new discounted values, i can't give you an exact answer.
but you can keep track of the original price, then when the discount is applied, display the original value then the new discounted value.
for example.
$originalPrice = 200;
function getDiscount($value,$originalValue){
return ($originalValue - $value);
}
echo 'Was: ';
echo '<s>'.$originalPrice.'</s>;
echo 'Now: '.$getDiscount(50,$originalPrice);
Obviously you code will be different but this is a general overview.

How to multiply $variable that includes € with a value

since I'm new to PHP I have a quite simple question. After Googling and searching here on stackoverflow I still can't get it to work.
<?php
$payinfront = $product->get_price_html();
$totalprice = $payinfront * 2;
?>
<p class="price">Total price: <?php echo $totalprice ?></p>
<p class="price">Amount to pay in front: <?php echo $payinfront ?></p>
<p class="price">Amount to pay after: <?php echo $totalprice - $payinfront ?></p>
The $payinfront value does get it's value from another part of my template. Let's say it's €10,-. This is the amount people have to pay in front. When we have done the service they have to pay the other half wich is the last rule.
Thanks for helping me out!
You want to store prices as floats, not as strings, so that PHP can recognize them as numeric values, and do calculations on them. Only cast them to strings at the very last moment, when you are echoing them in your template.
<?php
$payInFront = $product->getPrice(); // should return a numeric value, instead of a string
$totalPrice = $payInFront * 2;
$payAfter = $totalPrice - $payInFront;
?>
Then, when you echo the prices, you might want to format them in a certain way, using number_format():
<p>Price: € <?php echo number_format($totalPrice, 2, ',', '.'); ?></p>
Remove the currency symbol before multiplication:
$totalprice = substr($payinfront, 1) * 2;
Like this?
<?php
$payinfront = $product->get_price_html();
$totalprice = $payinfront * 2;
?>
<p class="price">Total price: €<?php echo $totalprice ?></p>
<p class="price">Amount to pay in front: €<?php echo $payinfront ?></p>
<p class="price">Amount to pay after: €<?php echo $totalprice - $payinfront ?></p>
Note that € makes an euro sign
You first have to remove the euro sign so that the variable can be considered a number. Then you make the multiplication and concatenate the euro sign below at the html part.
<?php
$payinfront = $product->get_price_html();
$payinfront = str_replace("€","",$payinfront);
$totalprice = $payinfront * 2;
?>
<p class="price">Total price: <?php echo $totalprice ?>€</p>
<p class="price">Amount to pay in front: <?php echo $payinfront ?>€</p>
<p class="price">Amount to pay after: <?php echo $totalprice - $payinfront ?>€</p>

Php/Magento show image based on stock qty

Nobody that can help me? (update 17-02)
Basicly what I need it the following.
Get the qty of stock of a product
Show qty number X
When negative number show 0
When it's > 0 show image X
When it's < 0 show image Y
Old info
In magento I am showing the stock qty with the actual numbers.
When the number is lower then 0 he always shows 0.
What I now want to do is add an image to both conditions.
For example when the qty is > 0 show a green image and when the qty = 0 then show a red image. Anybody knows how to do this?
<div class="qty-amount2">
<?php //echo (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>
<?php $_op_voorraad = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if($_op_voorraad < 0){
$_op_voorraad = 0;
}
echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
Ok got this fixed by hiring a developer but will share it
?>
<?php /* #var $this Mage_Catalog_Block_Product_View_Abstract */?>
<?php $_product = $this->getProduct() ?>
<?php if($_product->isSaleable()): ?>
<?php $_op_voorraad = (int) Mage::getModel('cataloginventory/stock_item')- >loadByProduct($_product)->getQty();?>
<?php if($_op_voorraad > 0): ?>
<div class="qty-amount2"><img style="float: left;" src="image_path" alt="">
<?php echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
</div>
<?php else: ?>
<?php $_op_voorraad = 0;?>
<div class=" qty-amount2 geen"><img src="image_path" alt="">
<?php echo $_op_voorraad; ?><span><?php echo $this->__(' op voorraad') ?></span>
</div>
<?php endif;?>
<?php else: ?>
<div class="geen"><img src="image_path" alt="">
<p><?php echo $this->__('Availability: Out of stock.') ?></p></div>
<?php endif; ?><br>
<?php echo $this->getPriceHtml($_product) ?>
<?php
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if ($_product->isAvailable()):
if(intval($stock)>0){
//green image code
}else{
//red image code
}
endif;
?>
You don't need to know the stock quantity.
If the item is in Out of Stock the isSaleable() method return 'false'. You can use this method.
For Example:
<?php if($_product->isSaleable()): ?>
<img src="[linkimage1]" width="135" height="135" alt="" />
<?php else: ?>
<img src="[linkimage2]" width="135" height="135" alt="" />
<?php endif; ?>
Remember to enable the Out of Stock visibility on frontend:
from admin:System->Catalog\Inventory->Stock Options->Display Out of Stock Products ('Yes')
Reindex all.

Categories