Block discount on certain product shopify script - ruby

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

Related

Show payment method discount rule on magento product page

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
}

Magento: don't send tax to paypal/don't show tax in paypal emails

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.

Magento - Credit Memo "Return to Stock" not updating Stock Availability

So when you do a credit memo in Magento, it sets the stock back to the correct level but does not change the "out of stock" back to "in stock" (if applicable). I came across this post by Wright Creatives (http://wrightcreativelabs.com/blog/55-credit-memo-in-stock.html) and it solves this problem. However, the method is too slow! It takes about 30 seconds per product.
I've ultimately had to remove this as a solution (because of the "speed") and now my boss would like the functionality reimplemented.
I know that the is_in_stock data controls this & I'm wondering if there is already a module out there, an article/tutorial, or someone who can help me get started on a "better/faster" solution.
I know it's old but because this isn't yet fixed not even in 1.7.0.1 I came up with a better solution.
Tested on 1.5.1 and above:
\app\code\core\Mage\CatalogInventory\Model\Observer.php
in
public function refundOrderInventory($observer)
after
Mage::getSingleton('cataloginventory/stock')->revertProductsSale($items);
//add this
foreach ($creditmemo->getAllItems() as $item) {
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
if(!$product->isConfigurable()){
$stockItem = $product->getStockItem();
//$stockItem->setQty($item->getQty());
$stockItem->setIsInStock(1);
$stockItem->save();
$product->setStockItem($stockItem);
$product->save();
}
}
Write a module that observes the event for credit memos and sets the in_stock flag on the products stock item object before product save. I cant tell you the event you want to observe but I am positive you can find it :)
If there is none, the uglier way would be to observe the products before save. More logic to pull it off but if you always want products to be in stock if they have qty regardless of anything else, then its not a bad idea.
Stores >> Config >> Inventory >> scroll to bottom
Go to System -> Configuration -> Inventory (under Catalog) -> Product Stock Options -> Automatically Return Credit Memo Item to Stock and make sure it's set to Yes.
Or simply go to your database and in core_config_data where the path is 'cataloginventory/item_options/auto_return' make sure that the value column is set to '1';

Magento - tracking who to give back to

We have a Magento multi-site that give a percentage back to a non-profit, and what we would like to do is to allow customers to select which non-profit or a group within that non-profit to receive the percentage.
Trying to keep it simple we thought allowing customers to enter a discount code named something like "GIVE BACK to {non-profit name}" (but no actual dollar amount subtracted from the purchase or maybe just a penny, I don't think you can have a 0 to a discount code), then internally we know to give the percentage back to that non-profit.
It just seems a little bit of an odd way of doing this, it would be better to have a drop down of the none-profits at the end, but we are not sure how to create that.
Does anyone have any suggestions, on an easy way of doing this?
Thanks in advance!
You could add an attribute to the quote/order model and then populate that with an appropriate value, populated at the cart level? It's the kind of think we've done for affiliate modules we've built in the past.
You'll need a custom controller to grab the value when the customer moves from the cart to the checkout, which means you'll also need to make the Cart -> Checkout step a form submission instead of a straight link.
Then, at the end of the month, you'll need to run a report on the collection with something along the lines of:
// I've added a * in the SELECT because I'm not sure of the attribute names off the top of my head :)
$collection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('*')
// Make sure the orders are in the correct date range
->addAttributeToFilter(...)
// Make sure the orders are in a valid state, e.g. processing, pending, complete, etc..
->addAttributeToFilter(...)
$donation_total = array();
foreach ($collection as $order) {
// You'll have to investigate the attribute values for these
$charity = $order->getData('charity_attribute_code');
$order_total = $order->getData('order_total_attribute_code');
if (!isset($donation_total[$charity])) {
$donation_total[$charity] = 0;
}
$donation_total[$charity] += $order_total;
}
print_r($donation_total);
You could make this more efficient with proper SUM()ing in the query.

Display Percentage or Amount of discount for catalog price rule in Magento

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.

Resources