This is the code that shows a Manufacturer Part Number on my product listing
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
what I would like to do is not show the MPN: Field on the product page if the $text_mpn field is blank, e.g if no part number is listed.
You can put an if statement around it. You can close the PHP code block after the if, put your code inbetween and open a new PHP code block to close it:
<?php if ($text_mpn != ''){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php };>
For blocks like this, it can be a bit messy and unclear to see where the block ends if you use normal curly braces, so you might consider the Alternative syntax for control structures for these cases:
<?php if ($text_mpn != ''):?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php endif;>
try this:
<?php if($mpn != ""){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php } ?>
try this
<?php if(strlen($text_mpn) > 0) echo "<span class='p-rewards'>MPN:".$text_mpn."</span>".$mpn."</br>"; ?>
Case 1:
You can use empty() for check is variable is empty or not.
<?php if(empty($mpn)){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php } ?>
Case :2
You can simply use display:none if you wants to hide control.you can simply add attribute of style.
<div <?php if(empty($mpn))
{ echo 'style="display:none"';}
else {echo 'style="display:block"';}
?> >
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
</div>
<?php
$mpn = 'Ram Pukar';
$text_mpn = 'Hello ';
echo $text = !empty($mpn) ? '<span class="p-rewards">MPN: '.$text_mpn.'</span>'.$mpn.'<br />':null;
?>
Output:
MPN: Hello Ram Pukar
Related
When I echo my own variable to a div container it comes out as plaint text (unformatted). I don't understand why echoing a Magento variable comes out formatted but mine doesn't? Here's my code, in particular the <?php if(!isset($specialPrice)): { ?> section which I created. Here is the code:
<div class="product">
<a href="<?php echo $_item->getProductUrl() ?>"
title="<?php echo $this->escapeHtml($_item->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail') ?>"
alt="<?php echo $this->escapeHtml($_item->getName()) ?>"/></a>
<div class="product-details">
<p class="product-name">
<?php echo $this->escapeHtml($_item->getName()) ?>
</p>
<?php $specialPrice = $productToCheck->getData('special_price');
$orignalPrice = $productToCheck->getData('price');
?>
<?php if(!isset($specialPrice)): { ?>
<?php echo $product['price'] ?>
<?php } else: { ?>
<?php echo $specialPrice ?>
<?php } endif ?>
</div>
</div>
Echoing $product['price'] shows up with its CSS like this:
but if it enters the ELSE statement to display my variable it shows like this:
Does anyone know what could be going wrong?
$product['price'] returns you a unformatting price value.
Maybe you can call a block function, for example $block->getPrice() and in getPrice() you need to format the price by your custom preferences.
If you want to add styles to your price value then you need to use a magento tag class
When an item is chosen on my site, it opens a details page. This is the top of the details page above the html tags:
<?php require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Master_details = "
SELECT *
FROM Master_List
WHERE Master_List.Master_Id = $recordID
";
$Master_details = mysqli_query($conn, $query_Master_details) or die(mysqli_error());
$row_Master_details = mysqli_fetch_assoc($Master_details);
$totalRows_Master_details = mysqli_num_rows($Master_details);
?>
This is the code that makes the body of the page:
<div class="container2">
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
<?php
mysqli_free_result($Master_details);
?>
<!-- end .container2 --></div>
What I would like to do is create an if/else statement that will look at the Style_ID of the selected item and determine if the number is > 3. If it is, I want it to choose an item that has a Style_Id of 1, 2, or 3 and the same Length as the item chosen and return a random row in the layout above, skip a few lines and then display the information for the selected item in the layout above. Else if it is < or = 3, then I need it to just display as above.
I have tried using:
<?php
If (Style_ID > 3) {
echo 'Test';
}Else {
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
}
?>
<?php
mysqli_free_result($Master_details);
?>
But it doesn't work and has syntax errors. How can I create this if/else statement?
Note: I would appreciate being able to get one setup for all of it, but if not just fixing this part would be a big help right now.
Thanks to #Brad for his responses, I finally got this one figured out. I ended up changing some of my field names and finally figured out where to close the php tags to make this work. Here is what I ended up with:
<div class="container2">
<div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div>
<?php
if ($row_master_details['type_id'] > 3) {
echo "Test";
}else { ?>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php
mysqli_free_result($master_details);
?>
<?php } ?>
<!-- end .container2 --></div>
For starters, PHP is case-sensitive.
If (...) {
...
}Else {
You'll want to use lower-case if and else.
Next, if Style_ID is an attribute of the record, you'll need to access it like you did the others.
if ($row_Master_details['Style_ID'] > 3) {
I'm trying to edit the template and I need to add "Starting from" in front of the prices on the catalog page (and then maybe the product page, but one step at a time)
I've located where it echos the price in the tpl file
<p class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
What I tried was to add a row in the oc_product table called starting_from and manually enter "Starting from" in each one. (there are only 6 products for now).
With very little knowlegde i added
<?php echo $product['starting_from']; ?>
so it then looked like this
<p class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['starting_from']; ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
Shockingly, it didn't work.
Can someone try and explain the magnitude of my errors, please?
And maybe help me out.
Thank
If your want to simply add the hardcoded string Starting from, you can do:
<p class="price">Starting from
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
Actually, if I got you right, this is very basic, so I'd recommend some PHP tutorial to start with.
I am using onestepcheckout in Magento. I have added in some extra flat rates and I am using two of them. I want to be able to only show one of the flat rates depending on the subtotal of the cart.
I have got the subtotal into a variable but the code has a foreach through each shipping method available so I need a way to say if $total is over 500 only show the second shipping method, if the total is under 500 only show the first shipping method.
<?php $total = Mage::getSingleton('checkout/session')->getQuote()->getSubtotal(); ?>
<?php foreach ($_shippingRateGroups as $code => $_rates): ?>
<dd><?php echo $this->getCarrierName($code) ?></dd>
<?php foreach ($_rates as $_rate): ?>
<dt style="margin-bottom: 5px;">
<?php if ($_rate->getErrorMessage()): ?>
<ul class="messages"><li class="error-msg"><ul><li><?php echo $_rate->getErrorMessage() ?></li></ul></li></ul>
<?php else: ?>
<input name="shipping_method" type="radio" class="validate-one-required-by-name" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> />
<label for="s_method_<?php echo $_rate->getCode() ?>"><!--<b><?php echo $this->getCarrierName($code) ?>:</b>--> <?php echo $_rate->getMethodTitle() ?>
<strong>
<?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
<?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
<?php echo $_excl; ?>
<?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
(<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
<?php endif; ?>
</strong>
</label>
<?php endif ?>
</dt>
<?php endforeach; ?>
<?php endforeach; ?>
Just do ...
//first method here by default
if($total>500){
//second method here
}
Something tells me you already know how to do this though
I can't decipher, what the first and second method are in your code, otherwise I would've posted a more complete code
I'm making a website with Joomla and am using RokSprocket to display some news. Now, some Titles are too long, so I wanted to shorten it after a certain character number, but it's not working. Am a total php noob, my apologies.
Here's the whole code:
<li data-lists-item>
<h4 class="sprocket-lists-title ">
<?php if ($item->custom_can_have_link): ?><a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>"><?php endif; ?>
<?php if(strlen($item->getTitle)>10)
echo substr($item->getTitle,0,10) . ' ...';
else
echo $item->getTitle();?></a>
<div class="date"><?php echo date('d.M. Y', strtotime($item->getDate()));?></div>
</h4>
<span class="sprocket-lists-item" data-lists-content>
<span class="sprocket-padding">
<?php if ($item->getPrimaryImage()) :?>
<img src="<?php echo $item->getPrimaryImage()->getSource(); ?>" class="sprocket-lists-image" />
<?php endif; ?>
<?php echo $item->getText(); ?>
<?php if ($item->getPrimaryLink()) : ?>
<span><?php rc_e('READ_MORE'); ?></span>
<?php endif; ?>
</span>
</span>
</li>
I'm hoping it's something very obvious and I'm just being stupid. Thanks a bunch already!
$item->getTitle is probably a method not a property, so try:
$item->getTitle(); // <-- parenthesis
Change to:
<?php if(strlen($item->getTitle())>10)
echo substr($item->getTitle(),0,10) . ' ...';