Can anyone tell me in magento, when we click Add to Cart button in product detail page, where and how is Product Price and Qty input multiplied and updated in shopping cart page Subtotal column. I want to customize the action for my custom pricing module.
Magento – Price Update in Add to Cart Functionality
addtocartpricequantity
Related Solutions
Edited:
- add New Action (rentAction) to CartContoller.php and Copy code of addAction to this new Action
- add New field (from_rent) to sales_quote_item which define a cart is doing from rent button
See Add a column to quote item table
- then using checkout_cart_product_add_after set from_rent=1 using
current request Action
See how use this event http://doghouse.agency/article/magento-adding-message-product-during-purchase
public function addrentFlag(Varien_Event_Observer $observer) {
try {
$Event = $observer->getEvent();
$product=$Event->getProduct();
$quote_item=$Event->getQuoteItem();
$item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
if(Mage::app()->getRequest()->getActionName()=='rent'):
$item->setFromRent(1);
endif;
} catch (Exception $e) {
// log any issues, but allow system to continue.
mage::logException($e);
if (mage::getIsDeveloperMode()) {
die($e->getMessage());
}
}
return $this;
}
a.you can use magento event/observer
.
Basically there are two events,by which you can change the cart price of that products:
checkout_cart_product_add_after
checkout_cart_update_items_after
checkout_cart_product_update_after
This 3 event is need because of:
Event1:checkout_cart_product_add_after
This event is fire when first time a product is cart for current session
of current input values from frontend
Event2:checkout_cart_product_update_after
This event is fire when a current exiting cart item
edited from edit link of cart item.
Event3:checkout_cart_update_items_after
This event is fire whenever cart items update from cart page.
As per as magento system, a cart item price
change using setter
functions setCustomPrice(), setOriginalCustomPrice
of cart item Object.
Just like:
$EachCartitem->setCustomPrice($price);
$EachCartitem->setOriginalCustomPrice($price);
// Enable super mode on the product.
$EachCartitem->getProduct()->setIsSuperMode(true);
And using events fire an observer
which is calculate price basic of your your rend field
.
![enter image description here][2]
Config.xml code like:
<global>
<models>
<magento65569>
<class>Stackexchange_Magento65569_Model</class>
</magento65569>
</models>
<events>
<checkout_cart_product_add_after> <!-- identifier of the event we want to catch -->
<observers>
<checkout_cart_product_add_after_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>magento65569/observer</class> <!-- observers class alias -->
<method>CalculatePrice</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</checkout_cart_product_add_after_handler>
</observers>
</checkout_cart_product_add_after>
<checkout_cart_update_items_after> <!-- identifier of the event we want to catch -->
<observers>
<checkout_cart_update_items_after_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>magento65569/observer</class> <!-- observers class alias -->
<method>UpdateCalculatePrice</method> <!-- observer's method to be called -->
</checkout_cart_update_items_after_handler>
</observers>
</checkout_cart_update_items_after>
<checkout_cart_product_update_after> <!-- identifier of the event we want to catch -->
<observers>
<checkout_cart_product_update_after_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>magento65569/observer</class> <!-- observers class alias -->
<method>ExitingcartItemUpdate</method> <!-- observer's method to be called -->
</checkout_cart_product_update_after_handler>
</observers>
</checkout_cart_product_update_after>
</events>
</global>
Observer code like this:
<?php
class Stackexchange_Magento65569_Model_Observer
{
/* This function will work when product will cart */
public function CalculatePrice(Varien_Event_Observer $observer)
{
//Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
$RadioVal=0;
$TextVal=0;
$resultParams=array();
$Event = $observer->getEvent();
$product=$Event->getProduct();
$quote_item=$Event->getQuoteItem();
$item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
$price=$product->getData('rent_price');
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
return;
}
/* This function will work when cart item will qty update */
public function UpdateCalculatePrice(Varien_Event_Observer $observer){
// Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
$cart=$observer->getEvent()->getCart();
$data=$observer->getEvent()->getInfo();
foreach ($data as $itemId => $itemInfo) {
$quote_item = $cart->getQuote()->getItemById($itemId);
if (!$quote_item) {
continue;
}
$item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
$price=$product->getData('rent_price');
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
}
}
public function ExitingcartItemUpdate($observer){
/*Mage::dispatchEvent('checkout_cart_product_update_after', array(
'quote_item' => $result,
'product' => $product
));*/
$Event = $observer->getEvent();
$product=$Event->getProduct();
$price=$product->getData('rent_price');
$quote_item=$Event->getQuoteItem();
$item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
return
}
}
In your block file
use Magento\Catalog\Block\Product\ListProduct;
<?php
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Block\Product\ListProduct $listProductBlock,
array $data = []
) {
parent::__construct($context, $data);
$this->_productCollectionFactory = $productCollectionFactory;
$this->listProductBlock = $listProductBlock;
}
public function getProductCollection()
{
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->load();
return $collection;
}
public function getAddToCartPostParams($product)
{
return $this->listProductBlock->getAddToCartPostParams($product);
}
?>
get productlist in view file
<?php
const PARAM_NAME_BASE64_URL = 'r64';
const PARAM_NAME_URL_ENCODED = 'uenc';
use Magento\Framework\App\Action\Action;
$_product=your product
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<?php echo $_product->getName()?>
<form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
<input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
<input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
<?php echo $block->getBlockHtml('formkey')?>
<?php $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface'); ?>
<button type="submit"
title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
class="action tocart primary">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
</form>
Best Answer
The subtotal logic on the cart page is implemented in the below template file.
Override this file into your local and modify it as per your requirement.