Please suppose:
Row product price = $1.5;
Quantity = 3;
Instead of total amount $4.5, it should be different amount based on finished product criteria.
Using configurable product options/attributes, my product page can display its finished product price correctly based on a special formula (written in JS).
Though I can calculate correctly using JS function (formula) on product page, I can't display its calculated amount on shopping cart page. How can I pass the calculated amount to shopping cart page so that its calculation persists in rest of steps in checkout?
You need an observer when adding products to cart, based on the same logic (options and quantity) as your JS function.
You have to build an observer that catches the add-to-cart event sales_quote_add_item and put your logic there. You can define the product price with
$observer->getEvent()->getQuoteItem()->setOriginalCustomPrice([your price])
This price will be saved to the quote object and persists in other steps in checkout.
If you want to calc the row total separate from price per item, you have to overwrite the calcRowTotal() method from the Mage_Sales_Model_Quote_Item class like explained here:
http://www.magentocommerce.com/boards/viewthread/220715/
Related
In my Shopify website, users are able to add any number of units for a product irrespective of the inventory. The stock is only checked when users click on checkout. How can I handle this issue?
Ideally, users should be able to add only available inventory to the cart. It's annoying when someone adds multiple quantities only to know they aren't in stock.
You need to check the variant.inventory_quantity => https://shopify.dev/docs/themes/liquid/reference/objects/variant#variant-inventory_quantity
If you have no option you can get it like so product.first_available_variant.inventory_quantity.
But if you have multiply options you will need to loop all the variants and create a JS object that will store each variant inventory_quantity and perform a JS check or update the max attribute of the number input for the quantity before adding the product to the cart.
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.
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 am running a silver bullion website. The price of silver changes every hour. The silver price is stored in a variable. I am searching for a shopping cart for wordpress that can do the following things:
For example I have a product name Silver Coin:
Silver Selling price at the present hour is $25.5
---> The cart Should pick this value from a PHP variable But dont output it as actual price of the product
The product price will be 1.2% of $25.5 ---------------->
This Should be the actual price of product and output as the product price
How to get this functionality and with which shopping cart? I am using WP-eCommerce Plugin, anyone can help please.
I did something similar not long ago and it was fairly straight forward.
Create a function in your themes function.php file which returns the latest price of silver. If you only want to pull it in every hour then this function could check a time stamp of when it was last retrieved and then either return the price from the database or retrieve the new price and update the database.
WP-eCommerce supports templates, find the correct template file which displays your products and inside the loop where the price is being displayed replace their function call with your own. I think the function is called, wpsc_the_product_price()
If for example some of your prices are 1.5% of the silver price and others are 1.2% then you could set the percentage in the product price. Then instead of replacing the function call in the template file use it to retrieve the product percentage and along with your silver price calculate the product price.
I'm writing a Magento extension that applies a new kind of discount to products based on an hourly schedule. I'd like the discount to apply to the final price after all other discounts (tier price, special price etc.) have been applied.
Which property of a Product object holds this final price? is it getFinalPrice()? getCalculatedFinalPrice()? Something else?
Note: I thought of "piggybacking" Catalog Price Rules for my purposes but I realized that won't work because these work on a daily schedule and I need to schedule hourly.
getFinalPrice() is the price the user sees in their cart. If this has been set explicitly on the product model, it will return that value. Otherwise it returns the result of the product's price model method getFinalPrice().
The price model will check to see if the product has a calculated_final_price property set. If not, it will apply tier pricing then special pricing then set the final price on the product. It will then dispatch an event giving you an opportunity to change the final price. Finally, it will apply any prices for custom options on the product.
The best way to do what your are trying to do would probably be to hook into the catalog_product_get_final_price event and set the final price of the product based on the hour at that time. So in your config, set up an event handler for the catalog_product_get_final_price event. Your observer will have access to the $this->getProduct() and the $this->getQty() where you can update the price.