Count of Custom Attribute Options - php

I would like to put a a message to say "available in other colors" on the category page if there is more than one color you can select on the product. Right now I have some code in content.product.php that detects if it's a variable product, but not if there are more than one option for an attribute. Color is a custom attribute.
Right now I have:
<?php if($product->product_type == "variable") { ?>
<h4>Available in other colors.</h4>
<?php } else { ?>
<?php } ?>
It's showing all variable product type with this message. I want to show it only if there are 2 or more colors attached to the product. The products has two attributes on it - size and color. Size always has three or four wheras color sometimes only has one

I guess you are looking for something like this?
<?php
if ($product->product_type == "variable") {
print '<h4>Variable products message.</h4>';
}
else if (count($product->color) > 2) {
print '<h4>This product is avilable in other colors!</h4>';
}
else {
#Optional print here..
}
?>
I might be wrong all you need to do is test by writing
<?php print_r($product); exit;?>
see where the colors are beign stored and just replace the ->color with the proper name. Good luck!

This is what ended up working
<?php
$fabric_values = get_the_terms( $product->id, 'pa_color-swatch');
if (count($fabric_values) >= 2) {
print '<h4>Available in other fabrics.</h4>';
}
else {
#Optional print here..
}
?>

Related

Mijoshop / Opencart if Category of Product

I am using Mijoshop which is a Opencart integration for Joomla. What I am currently looking to achieve is to display a specific div class in the header if a certain product or category is matched.
I've found this code which I've added to my category.tpl but without any luck or any of these being displayed:
<? php if($category_id==20){
echo "text1";}
else
{echo "text2"}
?>
Any ideas on how to get this to work? Some expert help would be greatly appreciated.
Try this:
<?php if($category['category_id'] == 20) { ?>
echo "text1";
} else {
echo "text2";
}
?>
The value should be called by $category['category_id'] and you had some syntax errors:
Space after ?
No semi-colon after "text2"

Display Specific Image Based On SQL Query Result

I have a page that displays products based on a previous user query. The search results contain sets of metadata, most importantly a "product rating" on a 1-10 scale.
Here's the code that pulls the product rating metadata
<?php echo get_post_meta( get_the_ID(), 'product_rating', true ); ?>
This pulls the 'rating' metadata from our dB and simply displays the rating as a numeral (1, 1.5, 2, 2.5 ect).
I'm looking to take the value of the 'product_rating' and set an image to display for each value. A rating of 1 will display one image, while a rating of 2 display a different image ect ect.
The current code I have in place substitutes an image for the value, but the image is displaying multiple times (up to 10 times for a single value). My code is below, using a rating value of "4" as an example.
<?php $rating = get_post_meta( get_the_ID(), 'product_rating', true ); ?>
<?php For($i=4; $i <= $rating; $i++){
echo "<img src='http://www.domain.com/lpimage/lock50.png' />";
}
?>
Is my code close to where I need to be, or am I completely off base for the function I want?
Assuming your rating variable is correctly defined I think you can handle the conditional display of various images as follows:
<?php if ($rating == 1) echo '<img src="urlofimage1.jpg"></img>';
elseif ($rating == 2) echo '<img src="urlofimage2.jpg"></img>';
elseif ($rating == 3) echo '<img src="urlofimage3.jpg"></img>';
elseif ($rating == 4) echo '<img src="urlofimage4.jpg"></img>';
?>
You use the double equal sign when comparing a value, you only use the single equal sign (=) when setting a variable's value.
Use >= and <= for ranges, ie. 1.3 or 2.7

Joomla 1.5 - Show image based on category ID

I'm trying to set up a blog page on an old Joomla 1.5 site. I need a different header graphic to show based on which of 3 categories is the current category.
This is what I have thus far, which doesn't really work. For the sake of this example, we'll just use an echo statement in place of the graphic.
if ($this->catid = "960") {
echo "this is category number 960";
}
else {
echo "This is another category";
}
?>
Does anyone remember how to do this in Joomla 1.5?
Figured it out...
<?php $catNum = JRequest::getVar('id');
if ($catNum == "962"){
echo "Category Number 962";
}
else{
echo "nope";
}
?>

WordPress Conditional if_page not working

my Conditional PHP is not working and i dont know why. Can you help me?
Here the Code:
<?php
if( is_page(array('5279','4945') ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif( is_page(array('5656','5668','5672','5677','5682','5690','5735','5738','5741','5744','5749','5752')))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
But i need it in this way... (letters are the Pages ids)
For pages a,b,c,d i need a own logo
For pages e,f,g,h i need a own logo
For pages j,k,l,m i need a own logo
and for Page n,o,p,q i need a own logo
Try this code, literally just tidied it slightly and removed the quotation marks round the page numbers.
<?php
if(is_page(array(5279,4945) ))
{
echo '<img src="http://www.example.de/logo_with_text.png">';
}
elseif(is_page(array(5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_with_text_and_icon.png">';
}
else
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}
?>
Just make sure you have the right pages. To do this go to pages in the dashboard and hover over the link to the page you want, you should see it shows the id number in the url, ie post=100. This is the id for that page and the one you will need to use. Using the id means that regardless of the page name, this code will always work for you.
An extreme test would also be using the below instead of the else
elseif(!is_page(array(5279,4945,5656,5668,5672,5677,5682,5690,5735,5738,5741,5744,5749,5752)))
{
echo '<img src="http://www.example.de/logo_without_text.png">';
}

Show available stock quantity on product page

Firstly I would like to apologise if this question has been answered somewhere else but I am unable to find what I am looking for as I am new to PHP and assume I need this to solve my problem.
I have built a website and am using Mals-e shopping cart. I have everything up and running but I would like to show how many products are still in stock under the product description and if there are no items in stock. For example:
Available stock: 2
or
Sold Out
I have read that I need a text file with product name, price and quantity, a PHP file to read and rewrite the quantity available and out put the results on the product page and a mypage.php page but I really don't know where to start. I've spent days trying to sort this out.
I have Mysql database with some items in table called (items) with available quantity but don't know how to go about sorting it out. Any help would be most appreciated.
Thank you.
Without seeing the actual code you're using to display the product, it's hard to say buy all you should need is something like:
<?php
// get the product and stock level
if($product->numberInStock > 0) {
echo 'Available: ' . $product->numberInStock;
} else {
echo 'Out of stock';
}
If you're editing a phtml type template (HTML with embedded PHP), you might display it like:
<? if($product->numberInStock > 0): ?>
<p>Available: <?= $product->numberInStock; ?></p>
<? else ?>
<p>Out of stock</p>
<? endif; ?>
Had same issue, found out following.
Inside your catalog/product type template you can use this:
<?php
$_product = $this->getProduct();
$_qty = $_product->getStockItem()->getQty();
?>
<p>
<?php if($_qty > 0): ?>
Available: <?php echo $_qty; ?>
<?php else: ?>
Out of stock
<?php endif; ?>
</p>
session_start($_POST['quantity']);
if(isset($_POST['quantity']))
{
$postedquantity=$_POST['quantity'];
$productQuantity="20";
if($postedquantity<$productQuantity){
echo "In stock";
echo "<br/>";
}
$productQuantity=$productQuantity-$postedquantity;
echo $productQuantity."Remaining";
}
You can even do like this,storing quantity value in session and everytime it's posted it will check whether it is in stock or not and it will show how much quantity is remaining.

Categories