Show the stock for configurable Products MAGENTO - magento

Im trying to show on the frontend the stock when we have less than 5 products.
For simple products is easy but when we are talking about configurable products it is not working.
This is the code I tried but is not working, the var $product is coming empty so I am always getting a 0 in $total_qty
Here is the code that I am using:
$product=Mage::getModel("catalog/product");
$prod=$product->load($prod_id);
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($prod);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
$total_qty=0;
foreach($col as $sprod)
{
$sprod=$product->load($sprod->getId());
$qty = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($sprod)->getQty());
$total_qty+=$qty;
}
?>
---
<p class="availability in-stock">
<?php //echo $this->__('Availability:') ?> <span>
<?php
if (($__manStock >= 1) && ($__manStock < 5))
{
echo $this->__("¡ JUST $total_qty LEFT !");
}
?>
</span></p>
Any help will be appreciated, thanks.

Configurable Products don't have stock. The simple products do.
You will have to query the associated products.
You can get the associated products using getAllowProducts()

Related

Magento 2: Switch the price for 'Sold' label after product is out of stock

I'm trying to to switch the price for a 'Sold' label on the product page of products that are out of stock.
If a product is sold out, the price should be hidden and in its stead should be a 'Sold' label.
I figured out that the price is placed in catalog_product_view.xml and it calls upon the vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml file, but I could not figure out where and how to bring in a condition to check whether product is sold out or not.
Can someone help here?
Thanks in advance.
Yuan
As I understand you have two parts to this issue
1) Hide price on Product-detail page if product is Out-of-stock
Prices are defined in vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml
called at class: Magento\Catalog\Pricing\Render and method: _toHtml()
You can override the method using DI to below
protected function _toHtml()
{
/** #var PricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock($this->getPriceRender());
if ($priceRender instanceof PricingRender) {
$product = $this->getProduct();
if ($product instanceof SaleableInterface && $product->isAvailable()) {
$arguments = $this->getData();
$arguments['render_block'] = $this;
return $priceRender->render($this->getPriceTypeCode(), $product, $arguments);
}
}
return parent::_toHtml();
}
$product->isAvailable() is the new condition that we have added
2) Show Sold label
outofstock label is shown by default, but if you still want to show create your block & template in vendor/magento/modul-catalog/view/frontend/layout/catalog_product_view.xml
and $product->isAvailable() function to check product's availability
Hope this helps
Hiding price for out of stock products in Magento 1.
RWD Theme
app/design/frontend/rwd/template/catalog/product/view.phtml
Change
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
To:
<?php if($_product->isSaleable()): ?>
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
<?php endif; ?>
Default Theme:
\app\design\frontend\base\default\template\catalog\product\view\type\default.phtml
Change
<?php echo $this->getPriceHtml($_product) ?>
To:
<?php if($_product->isSaleable()): ?>
<?php echo $this->getPriceHtml($_product) ?>
<?php endif; ?>
OR
Hiding price for out of stock products in Magento 2.
On admin page, Click on Stores, then under the Setting section, choose Configuration.
In this page, you find the Inventory section under Catalog. Expand the Stock Options section and you can start to set the custom status of the product.
Before coming to the Out of stock product section, you need to enter this field.
Set Items’ Status to be in Stock When Order in Canceled: You choose YES when you want to return items to your stock if an order is canceled.
Decrease Stock When Order is Placed: if you want to adjust the quantity on hand when the order is placed, you choose Yes
Then we can come to the part that allows you to display or disable the product out of stock in Magento 2.
You want to display the product out of stock, you set the Display Out of Stock Products section is Yes. In contrast, set No if you want to disable it.

Magento 1.9 - Get all products that contain upsell

how I can get all products that have / contain upsell products.
Thank you
<?php
$_products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('id');
foreach ($_products as $product){
$_product = Mage::getModel('catalog/product')->load($product);
if (count($_product->getUpSellProductIds()) > 0) :
echo $_product->getName()." has upsell products.";
endif;
?>

Magento Retail Store and Wholesale with Different Prices

Hey I was wondering about magento Retail/Wholesale capabilities.
The main store will be for normal customers (retail) and they see normal prices but for wholesalers they need to login and see wholesale prices?
I don't want normal visitors to see wholesale prices.
Is that possible?
Assign product price as customer groups after that use this code on list or view page, you will see product different price on both side same and equal to admin price.
<?php $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
if($groupId ==1 || $groupId ==""){?>
<?php if($product->getSpecialPrice()){?>
<?php echo "$".$rprice = number_format($product->getPrice(),2); ?> </div>
<?php }else{?>
<?php $product->setCustomerGroupId($groupId);
echo "$".$price = number_format($product->getPriceModel()->getFinalPrice(1, $product),2); ?>
<?php } ?>
</div>
<?php if($product->getSpecialPrice()){ echo "Special Price $".number_format($product->getSpecialPrice(),2);?>
<?php } ?>
<?php }else{echo "$".$price = number_format($product->getPriceModel()->getFinalPrice(1, $product),2); }
?>
for create customer group check this link:
http://www.magentocommerce.com/knowledge-base/entry/customer-groups

Cart page content, display product individual according to quantity

if i add product with quantity two then in cart contect there is only one product with 2 quantity display. but i want to display separate rows of same product according to quantity.
like cart contect should be as below.
product name Qty price
ABC 1 $10
ABC 1 $10
Finally i got solution. Here is an answer
open this file \app\code\core\Mage\Sales\Model\Quote.php and search for "_addCatalogProduct" function
and replace
$item = $this->getItemByProduct($product);
if (!$item) {
$item = Mage::getModel('sales/quote_item');
$item->setQuote($this);
if (Mage::app()->getStore()->isAdmin()) {
$item->setStoreId($this->getStore()->getId());
}
else {
$item->setStoreId(Mage::app()->getStore()->getId());
}
$newItem = true;
}
to with commenting code.
// $item = $this->getItemByProduct($product);
//if (!$item) {
$item = Mage::getModel('sales/quote_item');
$item->setQuote($this);
if (Mage::app()->getStore()->isAdmin()) {
$item->setStoreId($this->getStore()->getId());
}
else {
$item->setStoreId(Mage::app()->getStore()->getId());
}
$newItem = true;
// }
and in \app\code\core\Mage\Checkout\controllers\CartController.php
replace
$cart->addProduct($product, $params);
to
if($params['qty'] == 0 || $params['qty'] == '')
{
$params['qty'] = 1;
}
$quantity = $params['qty'];
for($loop=1; $loop<=$quantity; $loop++)
{
$params['qty'] = 1;
$cart->addProduct($product, $params);
}
If you really must do this then you could try creating an observer for the event sales_order_place_before and then in your observer you could modify the quote by looping through it and finding multiple qty items, change the qty back to 1 and then add the remainder of items as quote items to the quote object. Not sure if Magento will group them together again during/after the order is placed. You could maybe do the same thing using the event sales_quote_product_add_after which might be better as the user will see the individual lines on the cart page, but again, don't know if Magento will group the qty's back together during the checkout process without actually having a go at making it work. If it does, you could maybe try setting the product to be a super product and override the sku so it has a unique identifer tagged on (e.g. SKU100-1, SKU100-2 etc) before you add it to the quote.
What about this:
Replace this
<?php foreach($this->getItems() as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach ?>
with that on line 130 default theme /app/design/frontend/yourtheme/default/template/checkout/cart.phtml
<?php foreach($this->getItems() as $_item): ?>
<?php $qty = $_item->getQty() ?>
<?php for($i=0; $i < $qty; $i++) : ?>
<?php $_item->setQty(1); ?>
<?php echo $this->getItemHtml($_item); ?>
<?php endfor; ?>
<?php endforeach ?>

pass newly added attributes from product detail page to cart page (magento)

I am using Magento Version 1.7.0
I have added shirt size(newly added attributes) drop down box in product detail page.using below code
app\design\frontend\default{mytempalte}\template\catalog\product\view.phtml
<?php $product = $_product->getAttributeText('size_chart'); ?> shirt size
<select name="size_chart">
<option value="Select">Select</option>
<?php for($i = 0;$i < count($product);$i++) { ?>
<option value="<?php echo $product[$i]; ?>"><?php echo $product[$i]; ?></option>
<?php } ?>
</select>
customer can able to select the shirt size .After select the shirt size i need to show shirt size in cart and checkout page.
how to pass the shirt size value from product detail to other page?.
Thanks
Actually, in cart page you may get cart items in quote object.
$cart = Mage::getModel('checkout/cart')->getQuote();
$items = $cart->getAllVisibleItems();
foreach ($items as $item){
$options = $item->getProductOptions();
$superAttributes = $options['info_buyRequest']['super_attribute'];
if (!!$superAttributes){
$attributeObjSize = Mage::getModel('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY,'size_chart');
$attributeObjSizeId = $attributeObjSize->getAttributeId();
foreach ($superAttributes as $code=>$superAttribute){
if ($attributeObjSizeId == $code){
$sizeCode = $superAttribute;
$sizeLabel = $attributeObjSize->getSource()->getOptionText($sizeCode);
}
}
}
}
The $sizeLabel should be want you would like to have. Hope it works.

Resources