I have created a store (opencart) in which i have a product and user can choose the quantity and pack of product. So I have created custom option to choice quantity.
the price of product is shown like this :
I have to add price to to an original price for each pack :
now the result Current :
packs :
$1.9
100(0)
200(+$0.9)
500(+$1.3)
1000(+$2.9)
1200(+$3.9)
What I want:
$1.9
100($1.9)
200($2.8)
500($3.3)
.
.
.
You can check link here :
http://www.shopfairpackaging.co.uk/index.php?route=product/product&path=60_69&product_id=52
Opencart options are designed to be cumulative. Meaning that as the shopper selects difference options, the final price gets increased or decreased accordingly.
Opencart does not support the schema that you are seeking. However if you are desperate and need to switch to your own schema, then you could edit the theme files.
To achieve the results you are seeking do the following:
Locate the product.tpl file in /catalog/view/theme/default/template/product/ folder
Locate this line (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>) around line number 195, depending on your version
replace it with this (<?php $additive=str_replace('$','',$option_value['price'])+str_replace('$','',$price);echo '$'.number_format($additive, 2);?>)
Scriptonomy Your answer is half right it was giving me result of 00 so I did this
(<?php
//get first currency symbol
$symbol = substr($price, 1);
//get float value form string
$x = floatval(ltrim(ltrim(ltrim($option_value['price'], '£'), '$'), '€'));
$y = floatval(ltrim(ltrim(ltrim($price, '£'), '$'), '€'));
if($option_value['price_prefix'] == "+"){
echo $symbol;
echo $x + $y;
}elseif ($option_value['price_prefix'] == "-") {
echo $symbol;
echo $x - $y;
}else{
echo $symbol;
echo $option_value['price_prefix'].$option_value['price'];;
}
?>)
Tell me if i did something worng
Related
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..
}
?>
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
I have the following PHP code in Magento which so far takes the first sentence of the description and makes it the meta description.
<?php if (Mage::registry('current_product')) : ?>
<?php if (strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1)))=="") : ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1))).'" />' ?>
<?php endif; ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php endif; ?>
I'm trying to do two things, modify it so that it only runs if the product is missing a manually set meta description, and make it pull the first 2 sentences instead of the first 1.
Impossible?
I'm trying to do two things, modify it so that it only runs if the product is missing a manually set meta description, and make it pull the first 2 sentences instead of the first 1.
Impossible?
Yes, it's impossible. Because, if the product description is empty you simply can't pull two sentences from it. Not even one ;)
Notes:
you don't need PHP tags on every line
your if-syntax works, but is not common
fetch the description only once, not 4 times from the registry
compare with empty, to test if a description is set
if not set, print "No Description."
if set, get two sentences
to fetch two sentences, i added the function teaser() it's from somewhere on SO
--
<?php
if (Mage::registry('current_product'))
{
$metadescription = $this->description();
if ($metadescription === '') {
$description = Mage::registry('current_product')->getDescription();
$twoSentences = teaser($description, 2);
echo '<meta name="description" content="'.htmlspecialchars($twoSentences).'" />';
}
}
function teaser($body, $sentencesToDisplay = 2)
{
$nakedBody = preg_replace('/\s+/',' ',strip_tags($body));
$sentences = preg_split('/(\.|\?|\!)(\s)/',$nakedBody);
if (count($sentences) <= $sentencesToDisplay)
return $nakedBody;
$stopAt = 0;
foreach ($sentences as $i => $sentence) {
$stopAt += strlen($sentence);
if ($i >= $sentencesToDisplay - 1)
break;
}
$stopAt += ($sentencesToDisplay * 2);
return trim(substr($nakedBody, 0, $stopAt));
}
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.
my script should do this:
Check if there is a $startprice and $endprice for a product. Those variables are checked against the $price that is the product's price.
The user is able to add both ($startprice and $endprice) or just one of them or nothing of them.
In the case that he adds only the
$startprice, the $price must be
bigger than this.
In the case that he adds only the
$endprice, the $price must be smaller
than this.
In the case that he adds both, the
$price must be between of them.
In the case that he does not add
anything, the script will show all
the results.
If the data satisfies the statements, it will show the product's list. In all cases the product's list code is the same.
*What I did until now is this but I can't continue it correctly.
The product's price is named $price and I can get it correctly for each product.
<?php
$startprice = $_GET['startprice'];
$endprice = $_GET['endprice'];
// this is the between code and it's the only I know how to do...
if (($startprice <= $price) && ($endprice >= $price)) { ?>
<h2> <strong> <?php the_title(); ?> </strong></h2>
<? } ?>
Your question is not clear. But just try this from what I understood..
<?php
$startprice = isset($_GET['startprice']) ? $_GET['startprice'] : 0;
$endprice = isset($_GET['endprice']) ? $_GET['endprice'] : PHP_INT_MAX;
// this is the between code and it's the only I know how to do...
if (($startprice <= $price) && ($endprice >= $price)) { ?>
<h2> <strong> <?php the_title(); ?> </strong></h2>
<? } ?>
You need to include more context.
For example, its clear there is some number of products, whose prices are checked against the prices specified by the user. What is the format of these products, are they in a multidimensional array?
For example, would the price be defined like this:
$PRODUCTS[$prod_id]['price'] = 9.99
Or maybe in a "price" array, indexed by product id?
$PRICE[$prod_id] = 9.99
Or maybe in an object?
$product->price
We need much more context to help.
Just guessing here, because you haven't shown us much code:
if(!isset($startprice)) $startprice = 0;
if(!isset($endprice)) $endprice = PHP_INT_MAX;
foreach($theproducts as $product) {
if($product['price'] >= $startprice && $product['price'] <= $endprice) {
echo $product['name'].'<br/>';
}
}