I want to display the discounted price the client can have depending on the payment method he chooses directly on the product page.
Basically, what I need is a way to get the shopping cart rules that apply for payment methods. So then I could use it to build a table of payment methods X discounts like this:
Cash: $90 (10% discount)
CC: $100 (Full price)
Transfer: $100 (Full price)
So far I couldn't find a way to access the shopping cart rules. Any insights?
UPDATE:
For those wondering, I've found a way to get the shopping cart rules. Pretty simple actually:
$model = Mage::getModel('salesrule/rule')
->getCollection();
With this information I can loop through the rules and find the ones that apply to payment methods.
$conditions = unserialize($item['conditions_serialized']);
But I realized that this way is too much work and can get complex if rules are complex. I decided to get the rules that I want by ID and get the discount value, since it won't change all the time.
If you know how to make Magento calculate everything for me, like me asking "If a user chose this payment method, besides everything else (user group, catalog discounts, etc) what would be the final price?".
$model = Mage::getModel('salesrule/rule')
->getCollection();
foreach($model as $item){
// check, do whatever and get discount
}
Related
I want to write a script in Shopify that prevents discounts from rendering on certain products. I know this is wrong, but something like this:
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
if product = 123456789
CartDiscount.remove("Discount does not apply")
end
end
Output.cart = Input.cart
I looked at the documentation and saw the .reject({ message: String }) method but it applies to the whole cart. Is there a way to localize this to one instance?
Discounts are easy. First... get the discount code from the cart. You know it applies to the whole cart. Now you have an amount, or percentage or whatever you gave the customer in the code. Now kicks in the beauty of the scripts.
For each product, decide if it is discounted or not. If it is, apply the discount amount that you know from the discount code provided to the item. If it is a product not to be discounted, leave the price alone.
You are paying big bucks for the scripting engine. If you cannot program it yourself, pay someone that can! You are way better off studying a working script to learn, that trying to outfox yourself.
So you can in fact reject the discount code for the cart, but still discount products, some, not all, or others.
Old question but I found a resolution: basically, you can't "localize" a discount to a specific product. You have to block the entire cart from getting a discount. The approach I ended up taking is below:
# ID of product you want to block
productId = 10199241991
# Runs through a loop of items in your cart
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
puts product.id
next if product.gift_card?
next unless product.id == productId
case Input.cart.discount_code
when CartDiscount::Percentage
Input.cart.discount_code.reject({message: "Cannot be used with this product"})
end
end
Output.cart = Input.cart
With Magento version 1.7, how can I auto generate a personal 10% discount coupon for each newsletter receiver that can only be used once by that specific account/user?
Here is an idea. Actually 2 of them.
The quick one.
Create a coupon with your desired rules, set the number of uses to 1 per customer and unlimited for general usage and hard code the coupon code in the newsletter email.
Estimated time: 30 minutes including tests. Risk: minimum.
The slow but clean one:
Create an observer on the newsletter_subscriber_save_before or newsletter_subscriber_save_after that checks if the customer subscribes and if so, it creates a coupon with your desired settings. See this for creating coupons by code.
Then rewrite the method Mage_Newsletter_Model_Subscriber::sendConfirmationSuccessEmail so you can pass that code as a parameter to the e-mail template.
Something like this:
$email->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
$this->getEmail(),
$this->getName(),
array('subscriber'=>$this, 'coupon_code'=>THE COUPON GENERATED IN THE EVENT)
);
Then modify the newsletter subscription e-mail template to include this:
Your coupon code is: {{var coupon_code}}
Estimate 4h-8h. Risk: "not that minimum".
I would take the first approach.
Use personal discount extension http://www.magalter.com/personal-discount.html to generate 10% discount coupon. You will be able to choose customers who can use this coupon.
I'm integrating a system with Magento.
When I create an order, if my non-Magento system indicates the order does have a coupon I would like to mark the coupon code as "used" for that customer, so the next time he tries to use it, Magento will know that customer already used it.
How can I accomplish this?
I manage to do it like this:
$coupon = mage::getModel('salesrule/coupon')->load($code, 'code');
$coupon->setTimesUsed($coupon->getTimesUsed()+1);
$coupon->save();
$rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
$rule->setTimesUsed($rule->getTimesUsed()+1);
$rule->save();
$couponUsage = Mage::getResourceModel('salesrule/coupon_usage');
$couponUsage->updateCustomerCouponTimesUsed($customerId,$coupon->getCouponId());
Thanks to this post:
Magento - Single Coupon marked as used when payment pending
Set the Shopping Cart Rule's Uses per Customer to 1.
----EDIT----
If you want to apply the coupon in your code then you can do something like this:
$coupon_code = "YOUR_CODE";
Mage::getSingleton('checkout/cart')
->getQuote()
->setCouponCode($coupon_code)
->collectTotals()
->save();
I'm using magento 1.7, and one of the payment options is Paypal (UK) express checkout.
The problem is that I don't want paypal to send out emails with any tax breakdown on, is there a more straightforward way of solving this (at the Magento or Paypal end) rather than hacking the core module to pass sub+tax as sub and 0 as tax?
I can see that a lot of the fields are mapped in Model/Api/Nvp.php, but can't immediately see where I'd modify these values.
As far as I investigated, there is no easy configurable way to prevent taxes to be submitted to Paypal.
But indeed there is a core hack if you don't mind that only the total amount is submitted (no line items, no taxes).
Go to System/Config/Paypal and set Transfer Cart Line Items to No.
In your code go to function _validate() in class Mage_Paypal_Model_Cart.
At the end of this function add the following lines:
$this->_areItemsValid = false;
$this->_areTotalsValid = false;
Of course it is nicer to to rewrite this class in your app/code/local folder.
I'm struggling to figure out how to display the percentage or the amount of discount that is applied to a product in Magento via the Catalog Price Rules.
For example: I want the price to be displayed in the front-end as follows: [old-price] [special-price] [discount info] where [old-price] has a css strike through.
The [old-price] and [special-price] is available by default through the tax helper. I've tried using the CatalogRule model, but I have no way to load it with a product id as the load function expects an entity id and from what I can tell, there aren't any other useful methods to load by product ID. I've var dumped (as well as using get_class_methods) just about everything that I found in the price.phtml file (apart from $this of course), but nothing helps.
I could just use a simple calculation to work out the discount percentage or amount, but I have no way of knowing whether the catalog rule is based on a percentage, or fixed amount.
I hope this all makes sense?Thanks for the help.Rémy
I do agree Magento makes this kind of thing a bit too hard. I wanted to display the description of the coupon code next to the entered coupon code. I suspect my code will be able to help you on your way. I put this code at the top in the template checkout/cart/coupon.phtml:
<?php
$c = Mage::getResourceModel('salesrule/rule_collection');
$c->addBindParam('coupon_code', $this->getCouponCode());
$c->getSelect()->where("coupon_code is null or coupon_code='' or coupon_code=:coupon_code");
foreach ($c->getItems() as $item) {
$coupon_description = $item->getDescription();
}
?>
So you can see $coupon_description now holds the description of the Shopping Cart Price Rule as long as the user specified a coupon code. You can add more properties from the coupon this way.