Magento Add to cart button with predefined quantity - magento

How to Add quantity for this add to cart button code:
onclick="setLocation('
<?php
echo (string)Mage::helper('checkout/cart')->getAddUrl(Mage::getModel('catalog/product')->load($prod->getId()));
?>
')"

Mage_Checkout_Helper_Cart getAddUrl suggests that you can pass through the product as the first parameter, and an $additional array too. This additional array can include qty as a key, with a value.
So, you can call:
$additional = array("qty"=> 3);
echo (string)Mage::helper('checkout/cart')->getAddUrl(Mage::getModel('catalog/product')->load($prod->getId()), $additional);

Related

Magento - Add attribute if the product qty is zero

I want to insert this attrribute that I have created into the product page above the add to cart button but only if the qty is ZERO. Basically an estimated arrival date. Items that are available on back order only have a note above the add to cart button of when the product will actually be available from. I have put the below in and it works but I don't want it to show for products already available.
<p><font color="red">AVAILABLE FROM: <?php echo $_product->getResource()->getAttribute('due_date')->getFrontend()->getValue($_product); ?></font></p>
Can someone let me know what I need to add please.
Most appreciated
Anthony
You should be able to get the stock quantity on the product page by using:
Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()
You can then use that in a conditional to show the piece of code that you want. However, I'm not sure you need all of that to show the due date. Because you're already loading the product when you are on the product page, you should just be able to use:
<p class="red">AVAILABLE FROM: <?php echo $_product->getDueDate(); ?></p>
If that doesn't work, make sure that you have your due date attribute set to be visible on the product page. It'll be the one called, "Visible on Product View Page on Front-end".
Full code block could look something like this:
<?php
$qty = Mage::getModel(cataloginventory/stock_item)->loadByProduct($_product)->getQty();
if ($qty <= 0): ?>
<p class="red">AVAILABLE FROM: <?php echo $_product->getDueDate(); ?></p>
<?php endif; ?>
If you allow backorders, it's important you check for <= 0, rather than == 0. This is because if someone backorders it, your stock will go negative.
Also, it's more clean to remove the font tag and use CSS to style the text.

Magento: Display price twice on product pages

This is the code that generates the price on product pages:
<?php echo $this->getPriceHtml($_product) ?>
in *app/design/frontend/default/CUSTOM_TEMPLATE/template/catalog/product/view.phtml*
I have a simple product with a few custom options. When the customer selects one of the option, the price increases. All works fine until here.
My question is how can i display the price twice on the same page (need this for deign purposes)?
I tried to copy and paste the above code in two locations on the same page, but the problem is that the second price will not update when the product options are selected. Only one price updates, the second won't. Any ideas on how can i make it work?
There is most likely a JavaScript on the page that either calculates the new price, or fetches it with AJAX. So the price outputted with:
<?php echo $this->getPriceHtml($_product) ?>
Will get overwritten by the new one. Solving this requires that you find the Javascript and locate where it does this. You should also check what HTML element surrounds the getPriceHtml-call. I guess the template might have something like this:
<p class="totalProductPrice"><?php echo $this->getPriceHtml($_product) ?></p>
If you surround the output of your second price output with an element with identifiable classname or ID, you should be able to modify the Javascript to output the newly calculated price to that element as well as the original one.
EDIT:
After checking my own codebase, I think that the file you are looking for is located at /js/varien/Product.js. Inside the function reloadPrice: function(), check line 481 for this:
if($('product-price-'+this.config.productId)){
$('product-price-'+this.config.productId).innerHTML = price;
}
And modify the script to also update your second price element.
The solution for me was:
Copy the price_clone.phtml to price_clone2.phtml and change:
<?php echo $this->getPriceHtml($_product, false, '_clone2') ?>
Add the block in the catalog.xml
<block type="catalog/product_view" name="product.clone_prices2" as="prices2" template="catalog/product/view/price_clone2.phtml"/>
And in the product.js change the initPrices function
initPrices: function() {
this.containers[0] = 'product-price-' + this.productId;
this.containers[1] = 'bundle-price-' + this.productId;
this.containers[2] = 'price-including-tax-' + this.productId;
this.containers[3] = 'price-excluding-tax-' + this.productId;
this.containers[4] = 'old-price-' + this.productId;
this.containers[5] = 'product-price-' + this.productId+'_clone2';
},

magento add tax on custom attribute based price

we sell bunch of product and we need to display each price product and entire box price.For that reason i have created attribute with each price and calling it on product page.So main price of product will be box price and each price will be display from attribute.but now i when i add tax i can see the main price with excluding and including tax but how to apply that tax on attribute based price?
this would be each product price
<?php echo $_product->getResource()->getAttribute('each_price')->getFrontend()- >getValue($_product)?>
this would be whole box price
<?php echo $this->getPriceHtml($_product, true);?>
some thing like this can be done to achive this.please look the code below
<?php $txrate= $_product->getData('tax_percent');
if($txrate!=""):
echo '<div class="each-inc-tax">';
$ech_price= $_product->getResource()->getAttribute('each_price')->getFrontend()->getValue($_product);
$tx=($txrate*$ech_price)/100;
$each_tax=$ech_price+$tx;
echo ' <b>Each Price Including Tax:</b> <b>'. $each_tax.'</b>';
echo '</div>';
endif;
I was using the MSRP attribute in my magento and storing the value excluding Tax, but wishing to display on the frontend including Tax.
I used the following code...
<?php
$_finalPriceExclTax = $this->helper('tax')->getPrice($_product, $_product->getPrice(), false);
$_finalPriceInclTax = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice(), true);
$_taxpercent = $_finalPriceInclTax / $_finalPriceExclTax;
$_msrpPrice = $_product->getMsrp() * $_taxpercent;
?>

Magento: Get Product Backordered Quantity in New Order Email

I'm trying to add the number of backorder products to the email template that comes out in the new order email.
So far I've managed to get the product but I just can't seem to get it's quantity. I've tried a variety of methods but nothing seems to work. Here is my latest attempt:
The file is app/design/frontend/base/default/template/email/order/items/order/default/phtml
<?php
// 1. Get Qty of Product in Store.
// 2. Get Qty of product from order
// 3. IF qty from order > qty in store then get number over
// 4. Display number over
$_sku = $this->getItem()->getSku();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);
//print_r($_product);
echo 'qty in shop: '.$qtyInStock = $_product->getStockItem()->getQty(); //debugging
echo 'qty ordered: '.$qtyOrdered = $_item->getQtyOrdered()*1; //debugging
?>
<?php if($qtyOrdered > $qtyInStock): $back_order = $qtyOrdered - $qtyInStock; ?>
<p>* This product is not available in the requested quantity. <?php echo $back_order; ?> of the item(s) will be backordered.</p>
<?php endif; ?>
I'm not finished with the logic yet. I'm just stuck on getting the products quantity.
$_product->getStockItem()->getQty();
Any help most appreciated.
Billy
Found a solution:
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);
$stock_count = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
Seems quite roundabout but it does the job.
Feel free to suggest better...

Magento - show the stock indicator on product list

how can I show the stock indicator on Magento product list ?
Thanks a lot.
To show the quantity of items left in stock use this:
<?php echo (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>

Resources