Magento: Can I load a cart by quoteId? - magento

I need to load a cart by a quoteId, cause I want to add a product to a different cart than the current cart. Is this possible?
TIA!

$cartId = 99;
$cart = Mage::getModel('sales/quote')->load($cartId);
$productId = 55;
$product = Mage::getModel('catalog/product')->load($productId());
$cart->addProduct($product);

Related

Magento2.4: How to get Salable quantity in list.phtml file?

How can I get salable quantity on list.phtml or category page file, I want to show labels on products with 0 salable quantity.
Are there any other approaches without using object manager?
Please use this code in phtml file to get salable qty
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo($qty[0]['qty']);
Try the below code to get salable QTY.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
$qty = $StockState->execute($_product->getSku(), 2);
?>
Either Object Manager is not a good approach, but you need to inject in your custom module such as:
namespace Cloudways\Module\ModelName;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
Refer Link: https://magento.stackexchange.com/questions/301956/how-to-get-salable-qty-in-magento-2-3-3/302187#302187

unable to set discount amount programatically

I am trying to create order programatically. Which is happening perfectly fine. I just don't know how to set discount amount programmatically. which is going to be different for different orders.
You can add a custom price to a product when you add it into the quote using this:
$custom_price = 100; //Products new price for the specific order/customer.
$qty = 1;
$product_id = 210;
$product = Mage::getModel('catalog/product')->load($product_id);
$quoteItem = $quote->addProduct($product, $qty);
$quoteItem->setCustomPrice($custom_price);
$quoteItem->setOriginalCustomPrice($custom_price);
$quoteItem->getProduct()->setIsSuperMode(true);
$quote->save();

product has a custom options or not in magento?

how to check that product has a custom options or not in magento?
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$code = $product->getAttribute()->getAttributeCode();
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$hasOptions = $product->hasCustomOptions();
Simple as that. At the end $hasOptions should be true or false.

How to check if Configurable Product is out of stock?

We all know that a configurable product in magento is associated with simple product.
If the simple products associated to the configurable product becomes Inventory = 0, it means that the configurable product is out of stock
So the question is how do i detect if Configurable Product is out of stock? i want to detect so I can display in front-end the "Out of Stock" text.
something like this
if($configurable_product->isOutOfStock()) {
echo "Out of Stock";
}
How can i do this in Magento?
if (!$configurable->isSaleable() ||$configurable_product->getIsInStock()==0){
// out of stock
}
For checking child simple product:
$allProducts = $configurable->getTypeInstance(true)
->getUsedProducts(null, $configurable);
foreach ($allProducts as $product) {
if (!$product->isSaleable()|| $product->getIsInStock()==0) {
//out of stock for check child simple product
}
}
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);
This shows only the configurable products that are in stock.
Just a slight update/correction to Quovadisqc's answer. When defining $qty it should be
$qty = $stockItem->getData('qty'); // correct
Instead of what's currently there,
$qty = $stockItem->setData('qty'); // incorrect
I'd post this as a comment but I don't have enough rep.
In the foreach loop of products the following if statement works.
if ($product->getIsInStock() === '1' && $product->isSaleable() === true) {
echo 'this product is in stock';
}
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$qty = $stockItem->getData('qty');
$inStock = $stockItem->getData('is_in_stock');
if ($qty < 1 || $inStock == 0) {
// OutOfStock
}
I prefer to double check with qty since products won't always be out of stock on qty == 0 depending on config settings.

Magento - Add to Cart

I´m using this Code (in cartsontroller.php) to add a extra Product to Cart Page - but I dont see it in Cart itself?
There are no Errors or something like that...
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(25);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Resources