Following code is used to set custom price for simple product. Custom price set in cart as needed but when i switch currency then custom price value remains same with current currency symbol.
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
Is there any way to set custom price that work with currency switching.
I found a solution to do this.
First Step:
Add item to quote with custom price using following code suggested by #Ashish Raj
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = $customPrice;
$customPrice = $Current_currency_price = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
Second Step:
Second step is to create an controller post dispatch observer by adding following code in config.xml file of module
<events>
<controller_action_postdispatch>
<observers>
<frontend_currency_change>
<class>modulename/observer</class>
<method>hookToControllerActionPostDispatch</method>
</frontend_currency_change>
</observers>
</controller_action_postdispatch>
</events>
And add following code to observer class
public function hookToControllerActionPostDispatch($observer) {
if ($observer->getEvent()->getControllerAction()->getFullActionName() == 'directory_currency_switch') {
$quote = Mage::getSingleton('checkout/session')->getQuote();
if ($quote && $quote->hasItems()) {
foreach ($quote->getAllVisibleItems() as $item):
//add condition for target item
$customPrice = 23;//use custom price logic
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$customPrice = Mage::helper('directory')->currencyConvert($customPrice, $baseCurrencyCode, $currentCurrencyCode);
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
$quote->collectTotals()->save();
endforeach;
}
}
}
This is working for me. Hope this will help to someone having same issue.
I will prefer if somebody have better solution.
Thanks.
Use below code hope it will help you ...
First Step:
//you need to find base currency code and then find current currency code...
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = $customPrice;
Second Step:
//convert price from base currency to current currency
$customPrice = $Current_currency_price = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);
3rd Step:
then you can use your code:
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
Related
can someone help me about this at OpenCart 3?
I need add below iFrames Pixel Code into Confirmation/Thank You Page for order tracking.
<!-- Offer Conversion: -->
<iframe src="https://marktamerica.go2cloud.org/aff_l?offer_id=13763&adv_sub=<ORDER_ID>&amount=<SALE_AMT>" width="1" height="1" /></iframe>
<!-- // End Offer Conversion -->
You should replace the following tags in the pixel code as follows:
<ORDER_ID> = Replace this value with your parameter that results as the order confirmation number.
<SALE_AMT> = Replace this value with your parameter that has the Subtotal amount of the transaction that excludes tax and shipping charges.
But got someone give me a code as below
In catalog/controller/checkout/success.php
Before Unset session
$order_id=$this->session->data['order_id'];
$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($order_id);
$data['total'] = $order_info['total'];
$data['order_id'] = $order_id;
And Then Use
{{order_id}} instead of <ORDER_ID>
{{total}} instead of <SALE_AMT>
The code I already try, is work, but the {{total}} I need
<SALE_AMT> = Replace this value with your parameter that has the Subtotal amount of the transaction that excludes tax and shipping charges.
Seems like you want to use sub_total. Here you have to use another method from catalog/model/checkout/success.php, the getOrderTotals instead of getOrder.
In catalog/controller/checkout/success.php
Use following code
$order_id = $this->session->data['order_id'];
$this->load->model('checkout/order');
$data['order_id'] = $order_id;
$totals = $this->model_checkout_order->getOrderTotals($order_id);
foreach ($totals as $total) {
if ($total['code'] == 'sub_total') {
$data['total'] = $total['value'];
break;
}
}
Now you will get subtotal in {{total}} in your success.twig file.
The requirements are if the items are already on discount, then discount extra x%, if the items are not on discount, then discount y%. I need to handle the case that both type of these above items appear in the same cart? Is there a existing solutions, or if I need to implement myself, where would I start. Thank you
There is an event in magento,
sales_quote_collect_totals_after
This is fired whenever your total is calculated, what you can do is set a flag in session on click on the button to apply discount, and in this above event's observer method, check if it is set then apply discount.
In your config.xml
<global>
<events>
<sales_quote_collect_totals_after>
<observers>
<class>Custom_Module_Model_Observer</class>
<method>collectTotals</method>
</observers>
</sales_quote_collect_totals_after>
</events>
</global>
Make a Observer.php in
Custom
/Module
/Model
/Observer.php
Make a function in Observer.php
public function collectTotals(Varien_Event_Observer $observer)
{
$quote=$observer->getEvent()->getQuote();
$quoteid=$quote->getId();
//check condition here if need to apply Discount
if($disocuntApply) $discountAmount =5;
if($quoteid) {
if($discountAmount>0) {
$total=$quote->getBaseSubtotal();
$quote->setSubtotal(0);
$quote->setBaseSubtotal(0);
$quote->setSubtotalWithDiscount(0);
$quote->setBaseSubtotalWithDiscount(0);
$quote->setGrandTotal(0);
$quote->setBaseGrandTotal(0);
$canAddItems = $quote->isVirtual()? ('billing') : ('shipping');
foreach ($quote->getAllAddresses() as $address) {
$address->setSubtotal(0);
$address->setBaseSubtotal(0);
$address->setGrandTotal(0);
$address->setBaseGrandTotal(0);
$address->collectTotals();
$quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
$quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());
$quote->setSubtotalWithDiscount(
(float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
);
$quote->setBaseSubtotalWithDiscount(
(float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
);
$quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
$quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
$quote ->save();
$quote->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
->setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
->setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
->setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
->save();
if($address->getAddressType()==$canAddItems) {
$address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount()-$discountAmount);
$address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
$address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount()-$discountAmount);
$address->setBaseGrandTotal((float) $address->getBaseGrandTotal()-$discountAmount);
if($address->getDiscountDescription()){
$address->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
$address->setDiscountDescription($address->getDiscountDescription().', Amount Waived');
$address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
}else {
$address->setDiscountAmount(-($discountAmount));
$address->setDiscountDescription('Amount Waived');
$address->setBaseDiscountAmount(-($discountAmount));
}
$address->save();
}//end: if
} //end: foreach
//echo $quote->getGrandTotal();
foreach($quote->getAllItems() as $item){
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
$rat=$item->getPriceInclTax()/$total;
$ratdisc=$discountAmount*$rat;
$item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
$item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
}
}
}
}
collectTotals function will be called, whenever the quote totals is updated, so there is no need to call it explicitly.
Check for how it works here.
Setting magento session variables, check here.
hope it helps!
I am a newbie to Magento Community Edition.
It might be a little thing but don’t know how to do that.
I want to set the cart price to cartprice * 5?
Example - if the cart items weight is greater then 10kg then set cartprice to (10 * 5).
You can do this by event observer.
<checkout_cart_product_update_after>
<observers>
<update_price_edit_save_product>
<class>ABC_MModule_Model_Observer</class>
<method>cartUpdatePrice</method>
</update_price_save_product>
</observers>
</checkout_cart_product_update_after>
in Observer class define this function, and do the implementation accordinly to set the price by item.
public function cartUpdatePrice(Varien_Event_Observer $obs){
// Get the quote item
$item = $obs->getQuoteItem();
// Ensure we have the parent item, if it has one
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
//hidden filed from detail page
$item->setCustomPrice($new_price);
$item->setOriginalCustomPrice($new_price);
$item->getProduct()->setIsSuperMode(true);
}
You can make your own module using event observers to accomplish this. First define a store config value for weight and how many times it should be increased i.e. 10kgs and 5 times in your requirement.
Using event checkout_cart_product_update_after you can call your observer function and include below code in that function
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$weight = 0;
foreach($items as $item) {
$weight += ($item->getWeight() * $item->getQty()) ;
}
if($weight > Mage::getStoreConfig('weight'))){
$weight = $weight * Mage::getStoreConfig('number of times you want to increase)
}
return $weight;
I have the following simple code with a loop of products:
$_product->setCustomerGroupId(6);
$price = $_product->getFinalPrice();
I have a customer group (id of 6) that has a 50% discount applied to it.
The $price variable is always the full price. How can I get the discounted price?
This is in an API script I am writing, so there is no customer/session object. I can create one if required but would prefer to try and steer clear of creating temp sessions.
Check This:
<?php
$now = Mage::getSingleton('core/date')->timestamp(time());
$websiteId = 1;
$customerGroup = 4;
$productId = 9369;
$rules = Mage::getResourceModel('catalogrule/rule');
$rules->getRulePrice($now, $websiteId, $customerGroup, $productId));
?>
does anyone know how one can get the catalog- and cart price rules from an order?
I know that I can get the discount percentage from an order item via the method getDiscountPercent(), but how can I get all the rules that were applied to the whole order?
For example, I have a rule "Customer Group X gets 20% off all items in the store".
Now I want to determine which rules were actually applied when the order has been submitted by the user. I need this for an order export interface where I have to supply all discounts that the user got.
Thanks in advance!
Have a look in the sales_flat_order_item table. there is a field called applied_rule_ids which will give you the id of the rule applied to that item. Also you can find out in this table how much discount was applied and the percentage.
Example
//The order I want to check
$order_id = 859;
//Get the list of items for your order
$items = Mage::getModel('sales/order_item')
->getCollection()
->addFilter('order_id',array('eq'=>$order_id));
//loop through each item
foreach($items as $item){
//if the item has not had a rule applied to it skip it
if($item->getAppliedRuleIds() == '')continue;
/*
* I cant remember in the database they might be comma separated or space if multiple rules were applied
* the getAppliedRuleIds() function is the one you want
*/
foreach(explode(",",$item->getAppliedRuleIds()) as $ruleID){
//Load the rule object
$rule = Mage::getModel('catalogrule/rule')->load($ruleID);
// Throw out some information like the rule name what product it was applied to
echo "<p>".$item->getSku()." had rule ".$rule->getName()."(".$item->getAppliedRuleIds().") applied </p>";
}
}
/* Example of getting catalog rules applied for specific product */
$productId = 6;
$user = Mage::getSingleton('customer/session')->getCustomer();
$product = Mage::getModel('catalog/product')->load($productId);
$storeId = $product->getStoreId();
$websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
$dateTs = Mage::app()->getLocale()->storeTimeStamp($storeId);
$customerGroupId = $user->getGroupId();
$resource = Mage::getResourceModel('catalogrule/rule');
$rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
Yeah, it's kind of a pain that Magento doesn't leave any history around in the order record when you use sale pricing with regard to how that base price was calculated.
Fortunately it's open source, so you can patch this in if you like.
I recently wrote an observer that fires when the order record is loaded, to address this very issue. It cross-references the line base_price with the product's current full-retail price. If there is a mismatch, it adds a couple fields to the order item to help expose this info to any external script that's listening (in our case, a custom order fulfillment script that relays orders to SAP using Magento's SOAP API).
Here's the basic idea - make a module in app/code/local/YourCo/SalePricing
and set up an observer class in app/code/local/YourCo/SalePricing/Model/Promo/Observer.php
<?php
class YourCo_SalePricing_Model_Promo_Observer
{
public function __construct()
{
}
// tag order items that have a sale-price
// with original retail price and total sale discount for this line
public function report_sale_pricing($observer)
{
$event = $observer->getEvent();
$order = $event->getOrder();
$items = $order->getAllItems();
foreach ($items as $item) {
// heads up, you may want to do this for other types as well...
if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$regular_price = $this->_lookupFullRetail($item,$order->getStoreId());
$sale_price = $item->getBasePrice();
if ($regular_price - $sale_price > 0.005) {
$qty = $item->getQtyOrdered();
$total_sale_discount = ($regular_price * $qty) - ($sale_price * $qty);
$item->setFullRetailPrice((string)$regular_price);
$item->setSaleDiscount((string)$total_sale_discount);
}
}
}
}
private function _lookupFullRetail(&$item,$store_id)
{
$mpid = $item->getProductId();
$p = Mage::getModel('catalog/product')->setStoreId($store_id)->load($mpid);
return $p->getPrice();
}
}
Your module's etc/config.xml needs to tell magento when to run your observer:
<?xml version="1.0"?>
<config>
<global>
<models>
<yourco_salepricing>
<class>YourCo_SalePricing_Model</class>
</yourco_salepricing>
</models>
<events>
<sales_order_load_after>
<observers>
<yourco_salepricing>
<type>singleton</type>
<class>YourCo_SalePricing_Model_Promo_Observer</class>
<method>report_sale_pricing</method>
</yourco_salepricing>
</observers>
</sales_order_load_after>
</events>
</global>
</config>
Make sure to activate your new module in app/etc/modules/... and clear the config cache.
Now, when you load the order, you can loop over each item and check for $item->getFullRetailPrice() -- if it's there, you know the item was on sale (well, either that or the price has gone up since the order was placed). You still don't know why, ie which sale price rule was in force, but for our application we didn't really care, and getting that info to be saved with the order would have been a much tougher mod.
What might help you though:
$order = Mage::getModel('sales/order')->load(20569);
echo Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription());
This prints out all the discount rules name which have been applied to the order.