Magento validating cart price rules always returning true for all the products - magento

I have created a cart price rule for a configurable product. The rule condition has the SKU of the parent product. The functionality is working correctly. Now I want to show the description of the rule on the product page. The issue is that the description is showing for all the products whereas it should show only for this particular product.
$objrules = $objectManager->create('Magento\SalesRule\Model\RuleFactory')->create();
$rules = $objrules->getCollection();
foreach ($rules as $tmprule) {
$rule = $objectManager->create('Magento\SalesRule\Model\Rule')->load($tmprule->getId());
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($currentProduct->getId());
$item = $objectManager->create('Magento\Catalog\Model\Product');
$item->setProduct($product);
if ($rule->getActions()->validate($item) && $rule->getIsActive()) {
$product_disctxt=$rule->getDescription();
}
}
}
The rule validation code $rule->getActions()->validate($item) is always returning true for all the products.

Related

Magento 2 & Cybersource payment invalid field for duplicated product categories

I'm using Cybersource Payment gateway for my Magento 2 install, the gateway require some Merchant Data fields to be populated with the oeder, one of the fields is the Product category, currently i'm using the below method
$name = $categories = [];
foreach ($order->getAllVisibleItems() as $_item) {
array_push($name, $_item->getName());
$product = $this->productModel->load($_item->getProductId());
$cats = $product->getCategoryIds();
foreach($cats as $cat){
$category = $this->categoryModel->load($cat);
array_push($categories, $category->getName());
}
}
Then i'm calling the category using
'product_category' => implode(',', $categories),
but the issue is, when i'm creating an order with more than one product from the same category lets say 3 products from the same category, it show in the response the categories 3 times, and in this way i receive an error in cybersource that the product category fields has an invalid value.
how to show only the product category only one time?

In cart only one seller product can be added in one time

I need help in magneto, In cart only one seller product can be added in one time.
other seller product should be restricted.
This is code to control based on sku
$product = $this->_initProduct();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
if($item->sku() == $product->sku()) {
Mage::getSingleton('checkout/session')->getQuote()->addItem($item);
return $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
}
Like this I need to control one seller product at a time

Simple product inside configurable product

Is it possible to get information about a simple product inside the configurable product in magento? I already access the information about the configurable product with the code below, but now, I need to get the stock qty of a simple product inside this configurable product.
$product = Mage::getModel('catalog/product')->load($product);
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();// get the product stock information
$product = $this->getRequest()->getParam('product');//gets the id of the configurable product
First load Configurable product:
$product = Mage::getModel('catalog/product')->load($Configproductid);
Second: load all simple product by using configurable product:
$allProducts = $product->getTypeInstance(true)
->getUsedProducts(null, $product);
3.Check simple product stock one by one:
foreach ($allProducts as $product) {
if ($product->isSaleable()) {
$Stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
var_dump($Stock )
}
}

Last order details are displaying duplicate values for configurable products

I have added a simple code to track each purchase on the site. All products have different types of rules based on categories to subtract a predefined percent of amount of product price.
But when there are configurable products in the cart then it is displaying me both products, original product price and the price of the selected option for the product details in the order detail.
If I have a lipstick in my cart and the color I selected is "RED" then lipstick price will change. But it is displaying me the original lipstick price as well as the red lipstick price in the order details.
I have added the code on order success page.
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('created_at','DESC')
->setPageSize(1)
->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
foreach ($items as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
I have also tried
$tempmain = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$temp = $tempmain->getAllItems();
$total = $tempmain->getGrandTotal();
foreach ($temp as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
With the same results.
How to get only "RED" lipstick price in Order details?
Thanks in advance.
This is the details for all the availabel lipstick and their values:
when I select "Tango" as the color for lipstick on front end:
I am getting the Price of both products on success page:
Here is the Order Details Screen shot:
In frontoffice, when a customer add a configurable produt in his cart and order it, you'll see (in Database) 2 lines (2 quote_items and 2 order_item).
It's normal because magento needs to store the configurable product and the linked simple produt corresponding to the user selection (and linked to the master configurable product).
When displaying order details, Magento handles that difference by checking if a product has a parent id. You can do the same to ignore some order_item :
foreach($items as $item){
if ($item->getParentProductId() {
continue; // ignoring simple product associated to master order item (configurable)
}
// your code
}
I'm NOT 100% sure what your tying to do, but if you are on the order success page then this is the way to retrive the current order info (get the the last order for the db may not always be the current order on the success page if two user make a purchase millisecond apart)
Try adding this to Success.phtml
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
foreach($items as $item){
if ($item->getParentItem()) {
continue;
}
echo Mage::helper('core')->currency($item->getPrice(),true,false);
}

How to get grouped product ID from purchased or cart product

I'm using grouped products to track promotions. Simple products will at times belong to multiple grouped products, so checking parentProductIds is of no use. I'm wondering how to track the grouped product ID when a product is purchased through the grouped (promotion) SKU. I can see it's being stored in info_buyRequest and super_product_config within the orders, but how do I get that information back out? And is there a way to get it out in the cart/quote?
I was able to get it with the following code in cart.phtml, in the foreach($this->getItems() as $_item):
$values = unserialize($_item->getOptionByCode('info_buyRequest')->getValue());
$parentId = $values['super_product_config']['product_id'];
Depending on where you want to get this information, you could get it after a checkout process when the sales is saved. Then you could use the events sales_order_save_after and create a method in a class to get the items of a grouped product.
What is important here is the object of a class Mage_Sales_Model_Order_Item which has information about the product and the parents of a product
Here is an example:
public function processSalesOrder($observer)
{
$order = $observer->getOrder()
$quoteItems = $order->getItemsCollection(null, true);
/*#var $item Mage_Sales_Model_Order_Item */
foreach ($quoteItems as $item) {
$parent = $item->getParentItem();
if(!is_null($parent)){
// do your stuff - you have a product parent which has children product
// $item is the children
echo 'The parent product is ' . $parent->getSku();
echo 'One of the children product is' .$item->getSku();
}
}
On cart page grouped product is treated as a simple product. In Magento 2 you can get the parent id of these simple products from session. This worked for me:
<?php
$catalogSession = $_SESSION['catalog'];
$parentId = $catalogSession['last_viewed_product_id'];
?>

Resources