What is the conceptual difference between a Product and a Quote Item - magento

Involved classes : Mage_Sales_Model_Quote_Item and Mage_Catalog_Model_Product.
I get both of them as a result of listening an event ( on cart add ). I am trying to update the quantity information for a product from an external source.
So far I based my code only on the product information and I am not sure if this is correct.
What is the purpose of Quote Items?
How about an bundled of configurable product? Do you have any recommendation on how to get the individual items from a bundle product?
Thanks

I want to thank both responders so far for their effort but their responses are pretty far from my question. I'll try to respond myself based on the things that I've learned.
A quote is a concept related to the order, only that is previous to that in terms of work flow in Magento. A real world concept is something like a preorder, like a postIt on which you place your asked dishes in a restaurant without being an order or a bill.
I was monitoring an event (checkout_cart_product_add_after) that is sending me the $product and the $orderItem. I understand now that is sending both in order to get information about the product and information about billing and the representation of that product in the future order.
In the case of the grouped products for example where the $product is Tshirt with various associated sizes, the $product will contain the SKU of the main grouped product and the $orderItem will contain the instance of the Tshirt that was selected ( medium size SKU ).
FYI: So in order to update the information of a product at cart update you have is better to get the product info from the $item if is a complex type ( bundle, configurable or grouped )

Magento drops quote items into the cart, specifically. Those quote items are retrieved by using $product->prepareForCart. These items also include different information, such as quantity and configurable product options (on a quote item).
From a backend perspective, data for products are stored in: catalog_product_entity_*, whereas quote items are stored in sales_flat_quote_item (at least in Enterprise. someone else might want to verify this on community).
EDIT: Attaching some code that we wrote to import product inventories a while back.
$product = Mage::getModel("catalog/product")->load($productId);
$product->seStockData(array(
"qty" => (int)$yourQuantity,
"is_in_stock" => ((int)$isTheProductInStock),
"manage_stock" => $manageStock,
"is_qty_decimal" => $isQtyDecimal,
"use_config_manage_stock" => $useConfigManageStock,
));
Mage::getModel('catalog/product_api')->update($sku,$product->getData());
For your purposes you may just need to call $product->save();, but I'm including the whole snippet as written because it works.
Hope that helps. Thanks,
Joe

My first answer would be that a product and a quote for a product are two separate entities and therefore should not be modelled in a unified entity.
An example of why would be from the company I work for, and why we model these things separately:
In our e-procurement system you might have a "contract" between a specific buyer and a seller. The "quote" item models this when it comes to invoice entries. If there isn't a contract use the normal product price to create a Quote Item else adjust the price using the "contract" between supplier and buyer.

Related

How do I find a Magento products primary category

I am using Magento 1.6.2.
I need to find an easy way of checking which category is the "default" category for any given product, where multiple categories have been assigned to that product.
The reason for this is as follows:
I have different discount amounts available on different categories in my store.
Where a product falls into two categories with different discounts applied, the highest discount should be applied to that product, but if that product has been added to the system (created) with a default category that has a lower discount, and then afterwards added into the higher discount category as well, the higher discount level is not being applied when that product is purchased.
If, however, I add the product to the higher discount category first, and then to the lower discount category afterwards (so the product appears on my website in all the right places), the correct (higher) discount level applies to that product.
I can only assume, therefore, that the default (or primary, or first, however you want to label it) category is the one that determines the outcome of the discount level to be applied to it. If this is wrong, then I need a lesson in understanding the mechanics behind this to see where I have gone wrong in the overall setup of this system...
If my assumption is correct, then I need a way to determine which category any given product (using the SKU) has been assigned to first, to ensure that the correct discount can be applied to that product.
I hope I have explained that clearly enough!
Does anyone have any suggestions. I have been unable to find information on this regardless of guessing specific search terms. The only things I can find are related to general category retrieval, and not this very specific task I need.
Cheers!
Im afraid your assumption is incorrect. Magento has no concept of a 'primary' category for a product. It only knows the product is in category id(s);
1, 12, 234
etc. The id's are stored numercially with no idea of which category might be more important than another.
What you could do instead is create a custom attribute and enter the id of the category you want to treat as 'primary' and retreive this attribute value when working out whether to give more discount or not.

Magento: Show a products immediate categories, not parent or grand-parents and display on different lines

I'm building a store that has proven a little difficult and the client has requested some things that seem like a lot of effort for not much benefit.
They want the product brand, type, name and year all on new lines on catalogue and page views.
I have reasoned that can be achieved through placing the products into brand and type categories (and unfortunately year as well which I'm working to find another solution as this will be a nightmare). These categories are in parent categories called Shop By Brand and Shop By Type.
I'm wanting to echo the immediate categories of the product individually. So echo BRAND and echo TYPE. This is so the order can be BRAND, NAME, TYPE.
To achieve this I need to get the immediate category and not grand-parent category of the product, and echo each individually.
This is unless somebody presents a more elegant solution.
Thanks in advance.
You can do this with attributes. Let's say you add a new attribute, 'Year' (attribute code 'year'). Add that into your attribute set and enter a value.
//display the year
echo $_product->getYear();
The same method can be used for other attributes. There'a already a 'manufacturer' attribute you can use for this, or if you're a stickler for semantics, you can create a new 'brand' attribute and use the same concept (manufacturer and brand are not the same thing to some store owners).
//display the brand
echo $_product->getBrand();

Programmatically apply a discount price in product list

I would like to programmatically display a discounted price on the product list/page so that each customer as a personal price shown based on a customer attribute (like gender for instance, and of course let it be assigned as new price for the user session) but I can't see a viable option via the magento catalog price rules involving customer attributes other than the assigned group.
I'm using the group to do different kind of discounts and categorization of the customers, so the path of "split the customers on the gender" (or similar) is not an option unfortunately, since I can't assign more than 1 group per customer.
At the moment I was thinking about setting up a custom observer to check for calls to the price generation and then let it consider my customer attributes when calculating the price.
Is that possible? if so, any pointers about?
Would you suggest something different?
Thanks a lot to everyone for your time.
As I was thinking before, I solved my issue creating a custom module with an observer looking for catalog_product_get_final_price calls, getting the customer details from the current customer session.
It works great, I just need to let it display the correct price in the catalog product list, but is minimal, as the price seems to be calculated correctly inside the product page and the cart

how to get id of product in shipping method

Is there anyway to get product Id of product in Shipping module, so that for different category I can charge Additional amount.
for eg: i have to two product in carts. One is in phone category and other is in computer category. for shipment I have to add $5 for phone and $25 for computer . so shipment charge should be $30 dollars in frontend. Is there anyway I can get product Id and corresponding product's category Id in Shipping method collect rates method??
Please help me, I am in a deep trouble....I hope Eg that I provided , explains what I need.
Thanks in advance.
You can get the products from quote
Mage::getSingleton('checkout/cart')->getQuote()->getAllItems();
or
Mage::getSingleton('checkout/type_onepage')->getQuote()->getAllItems();
and iterating over those items (be aware of grouped and bundled parents and so on) will give you the category(s) they might belong to and you can make your conditions against those
This is only a tenable option if products belong to only one category. Unless something has changed in the last couple of versions, there is no data for quote items that shows the category context from which they were added.
WebShopApps.com has several already-developed solutions for shipping scenarios. Depending on development contract & budget, it may be more cost-effective to use one of their well-tested extensions, e.g. Shipping Override Matrix.

Magento: Add a "fake" product to cart/quote

I understand how to programmatically create a product and also add to cart. I know this might sound dumb but is it is possible to generate a product on the fly and add that to the cart/quote but never actually save it in the database.
We want to create a made to order interface and I was thinking at the end it could add a bundle product with all the selections but that bundle product wouldn't actually exist in the backend.
I figured as long as you can make sure the quote and order has what it needs in terms of the product it would be ok, but obviously there is probably a lot that is tied to looking up stuff in the db on a specific sku or ID. I know that if you delete a product and then look at an order in the admin that causes issues, at least it did for this one scenario I was dealing with.
I was thinking of creating a giant bundle product that had like 6 different bundle items and each item could potentially have like 500 products and then based on what the user selects I programmatically add the bundle to cart. But then I wasn't sure if there would be a negative affect with having a gigantic bundle product like that as well.
UPDATE:
I don't think this will work, obviously there are a lot of information tied to the product in the database and we setup a test and right away we get an error for $item->getProduct(). We are moving forward with creating a giant bundle product and also the generic product with adding custom options on the fly, which Anda pointed out below. Any other suggestions will be greatly appreciated.
I'm not sure that clockworkgeek's approach is going to work. On every page load, Magento loads the items from the cart to make sure that they are still valid (in-stock, prices correct, etc), and amends the cart to reflect those values. My understanding of the system in the past has been that a product in the cart needs to have a corresponding database value to survive this process.
The "giant bundle product" approach is a pain, but in the past has been the best approach I have found. Attempting to change the values of the product (such as price or attributes) will be overridden by the cart checks, so you need a product w/ maximal flexibility, such as an overly-customized bundle product or configurable product.
Hope that helps!
Thanks,
Joe
Why not create a generic product in db and then set the product customization as custom options (additional_options) on the fly depending on the user selection. You can add custom options to the product (actually to the quote item) without having to save them in the database. I did this once for a website that sells glasses with prescription. The prescription was added as an option.
You can programmatically create Mage_Sales_Model_Quote_Items and add them to the cart. You've noticed it needs a product to match it's product ID but it needn't be a useful one. It could be a blank, disabled product, also created in code. All that's needed is a stub.
The necessary stuff for the cart is stored in the quote item - fields like name, value and quantity. Those fields are then copied directly to the order without using a product.
Mage::getModel('catalog/product')
creates a new product. you can add it to a cart, by doing something like this:
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product')
->setStoreId($storeid)
->setTypeId($type_id)
->setQty($quantyty)
->setWhatAttributYouWant($attribute);
$cart->addProduct($product);
product attributes you can find in the DB in tables that start like catalog_product_... or take an already created product, and see what attributes it has in the _data array (with debugger or just print_r($product->getData))

Resources