hey i'm implementing a custom discount system since magento discount system does not feet my requirements so i'm trying to apply a discount on an Mage_Sales_Model_Quote_Item I've read some and I've found the following function setOriginalCustomPrice the thing is that it applies on the item, and if the user changes the quantity he will get the discount on the item with the new quantity, so i'm trying to use a different method addOption on the item and only on the cart page show the calculations based on the quantity in the option value
$item->addOption(array('code'=>'promo','value' => serialize(['amount'=>10,'qty'=>1])));
and in the cart page
$promo = $item->getOptionByCode('promo');
echo '<div class="orig-price">'.$item->getOriginalPrice().'</div>';
echo '<div class="new-price">'.$item->getOriginalPrice() - ($promo['amount'] * $promo['qty']).'</div>';
the problem is that it does'nt actually apply the new price on the product,
so i want to customize Mage_Sales_Model_Quote->collectTotals() to show my discounts
and send it to the admin back-end when order is completed
how can i achieve that?
thanks in advance
I think there is a fundamental flaw in your approach. I'm not sure what you don't like in standard discounts, and what you can't achieve with catalog or shopping cart rules, but what you're trying to do definitely breaks these features (along with my heart).
However, if you're sure about what you're trying to do, then don't customize Mage_Sales_Model_Quote->collectTotals().
This function just... well, it collects all totals: subtotal, shipping, discount, etc. And it looks like you're changing only price output, but Magento itself doesn't know anything about it.
So if you want to let Magento know that you're changing the item price, you have to either add your own total, or change one of the existing totals. After that Magento will do everything else. Since after your changes Magento outputs already calculated price instead of original one, it may be strange for customer to see the original price in the cart and the additional discount total. So it looks like you will have to change subtotal total.
To do that you have to rewrite Mage_Sales_Model_Quote_Address_Total_Subtotal class in your extension and insert your calculation in _initItem() method. Around line 111 in the original file you will see the code:
$item->setPrice($finalPrice)
->setBaseOriginalPrice($finalPrice);
And this is where Magento sets price for the item, so you can insert your calculations and change $finalPrice before that. If you have virtual products, you will have to change collect() method too.
Related
I trying to allow users to get the first product sample they add to their cart for free, but any samples they add after (including the one they already added) should be normal price. Right now I using setOriginalCustomPrice in a module, but the customer can change the quantity, and the price stays at zero instead of going up. Anyone know how to go about this?
You can achieve this by a simple calculation where you are using setOriginalCustomPrice.Currently I think you are setting price 0.Instead of doing this,use following logic to set custom price.
$customPrice=$qtyincart*$productPrice-$productPrice.
So if user add 1 qty then $customPrice will be 0 else will equal to sum of qty-1.
I hope this solve your purpose.
I have my shopping website in opencart and i want to put certain conditions on particular products in my store.
For example: I have a product in my store and i want to put following order condition on it.
Conditions: You need to order at least $50.00 in products to be able to to purchase this
Could you please tell me which is the correct way to do it?
There's no default method for that, AFAIK. You have to write a simple VQMod, which will check order subtotal upon add to cart operation.
I'm looking to implement some e-commerce functionality that gives discounts when certain quantities are reached. The catch is, its not quantities of one sku, any number of other products in a category can trigger the quantity break when in total they reach the threshold.
So if I have a model class for a Cart_Product lets say, I would typically put the logic for getting the prices in that class as a method. But since other instances of that class in the current cart need to be considered, I'm not sure of the best way to proceed.
Do I call the "owner" Cart instance inside of the Cart_Product get_price method and then add the logic to check for the quantity break? Or is there a better design pattern to use at this step?
First of all, model is not a class or instance. Model is a layer. What you are talking about in your question actually are domain objects (assuming they are not also responsible for saving themselves, which would violate SRP.
As for applying the discount, it depends on whether each product in your cart has a separate discount of the discount is same for all the products:
if each product can have a separate discount, then the logic for that should reside in the Product domain object.
if all products get the same discount, then the discount should affect only the sum total, therefore - compute in the Cart instance.
The logic you have described is a cart-wide feature; since the cart is the logical owner of the products inside, you would implement it there:
class Cart
{
private $products; // Cart_Product[]
// ...
function calculateDiscount()
{
$totalQuantity = array_reduce($this->products, function($sum, $product) {
return $sum + $product->getQuantity();
}, 0);
if ($totalQuantity > 10) {
$this->cartDiscount = 25; // apply 25% discount on the cart
} else {
$this->cartDiscount = 0;
}
}
}
This introduces a separate entity for a global cart discount. If you don't want that, you would have to apply the discount to each individual item.
i just went through something very similar. really the only thing the cart should know about is the product id and the quantity. everything else should be for display purposes only. in other words the product object is always responsible for the price. the only reason that a price is stored in the cart is to help show it in the view. otherwise we assume that the price always has to be checked with any insert or update, to prevent fraud.
here is another scenario - you have a special on shipping, like buy $100 worth of qualifying goods and you get free shipping. there might be a separate shipping special on specific products. the only way to calculate is with all of the cart items.
so my solution - which i am not sure is optimal - is to pass the cart items to a shipping object - do the shipping calculations - optionally add messaging for specific products to display in the cart - and then return the cart items.
otherwise you are having to put shipping methods in the cart class which does not make any sense but maybe there is another way to do this.
here is another scenario - inventory control. someone orders 30 blue widgets but you only have 10 blue widgets. ok you can check for inventory when you insert item in cart. but what if they update the cart and then increase to 30? that means that we have to check inventory - for every item in the cart - every time the cart is updated. and if we are doing that then might as well get the price in case it has gone up or down.
so i take the cart items - and pass them to a product object - which checks inventory - and if necessary reduces the quantity of the items down to current inventory - optionally adds messaging explaining that stock is limited - then passes back to cart object.
finally - suggest that you have an object that owns the shopping session. and then thats where the totals would be. that way the cart is never in charge of totals - its just a container. one way is you just start an order and then store the different totals there.
I have some simple products setup that have various custom options, some of which are set to required. What I would like to do is get the products set up to where the customer can choose from predefined quantities for a certain price.
For example say the product is a widget, i want the customer to be able to choose from say 100 for $20, 200 for $35, 500 for $80 ..... and so on.
Tiered Pricing doesn't work since the prices are increasing.
I tried using a custom options which worked out price-wise BUT it did not update the total weight of the product which I need calculated in order to calculate shipping quotes.
I also tried using group products and setting the products within the group as the specific quantities and setting the calculated weights BUT it doesn't allow me to keep the custom options i have set as required.
So right now I'm just at a lost. Any Ideas how i can get this setup with the predefined quantities that also updates weight for shipping?
I'm using magento 1.6.0
Well after some more searching I found something that works....
http://www.magentocommerce.com/boards/viewthread/73036/
I have two attributes for a product,first one is the unit price and second is the case price.
The unit price is the default price of the magento,for the case price i create a new attribute named case pirce.
My question is i display the prices in the product description,but it is not allowing me to add the case price to the shopping cart
Am i doing some thing wrong here,if so let me know the solution.
One way to achieve this is using the new "Qty Increments" feature in conjunction with Tier Pricing. If you look in the Inventory tab on the Product Edit screen, you can enable Qty Increments and set a value, which will enforce that the customer add products to the cart in batches equal to a multiple of the Qty Increment value.
Products are added in quantities of 1, so a "case" isn't really a logical unit to the system. Some options:
You could use javascript to add the product to the cart in the right quantity (for a case), but the user will be able to adjust this quantity.
You could create a second, related product as "Case of X", but the inventory will not match up.
You could create a product option for single/case quantities with a price, but this seems somewhat unintuitive and also has the quantity problem as above.*
You could create a grouped product with a fixed quantity. This is a bit contrived, but makes the most sense from a bookkeeping perspective.
Let me know if you have any questions. Hope that helps!
Thanks,
Joseph Mastey
magento calls it "tier pricing" you should try and configure your "cases" by quantity and assign a discount for it