Magento quote items modify sku - magento

I need to change SKU on quoteitems and I do it like this:
foreach($items as $item){
$item->setSku($newSku);
$item->save();
}
But magento always resets SKUs into their original values. Is there some easy way to change it, like superMode on prices? Or if not, then where can I find methods to rewrite, so that they ignore my custom SKUs?
Tried looking for it, but even if I remove cart init and save from index action, they still reset somewhere.
I need it to work in Magento 1.7-1.8

Ok, so I didnt feel like waiting and found it.
You can find it in Mage_Sales_Model_Quote_Item and there in function setProduct. This function is run every time the cart is shown and it resets the data from Product to Item.
Not sure if now my data will get through whole checkout process, will update the answer if not.
EDIT
This has solved only cart, will need to find also function for order. Will update when I get it.
EDIT
In the end, I don't advise you to do this. But if you need it then, the way to do it is to create observe on event sales_order_save_after and modify sku of order items. This seems to work everywhere, only in email after checkout it doesn't work, there you have to modify email. Also I use this only on configurable products.

I do the same thing on my project using sales_quote_collect_totals_after
Just change quote item SKU as you want using this event.

Related

Shopify Script for Fixed Discount of a set price based on tags

Using Shopify Scripts :::
I am trying to achieve the below but getting issue. Could you please let me know how to do this.
$25 off a purchase of $125 or more from the "sale" section. These products have a tag "sale" and are also in a collection "sale" based on that tag.
Looking forward for your quick response.
Great Thanks
If you examine the documentation for Shopify Scripts you'll notice that there is no support for auditing the line item for tags. So you should try the other approach which is proven, and works well.
Look for an item with a compare at price. If you find one, then that typically means the item is on sale. So you can rig your logic to look for items with a compare at price, and if they have one, then determine if the cart has exceeded the $125 in value, and if so, apply your discount.
I can't give you the exact code as I don't know the structure of your code. But this is what you need to do.
Wherever a product can be added to the cart assign a JavaScript function to check if the product is of "Sale" collection. If yes, add an attribute cart.attributes.something.something and keep changing it as and when required.
When the checkout mechanism is present in the page, and if your conditions satisfy the Sale discount, take then to /checkout?DISCOUNT=<CODE> and click of checkout. It'll automatically apply the discount.

Magento custom price on Quote Item is ignored?

I want to be able to change price on Quote Items to be different than on Products, based on several Tutorials I have used this code:
$quoteItem->setCustomPrice($price);
$quoteItem->setOriginalCustomPrice($price);
$quoteItem->getProduct()->setIsSuperMode(true);
$quote->save();
After that when I get to cart it shows normal prices, not custom price. I have checked Quote Items in cart and they have correct customPrice, but it seems to be ignored. Do I have to activate something else to make this custom prices to take effect?
PS:
Magento v1.7
OK, so I found the problem, somehow it didnt get saved in my script. Not sure why, but I have rewritten my code into 2 functions, 1 for adding product, the other for changing price.
So this code is all that is necessary:
$quoteItem->setCustomPrice($price);
$quoteItem->setOriginalCustomPrice($price);
$quoteItem->getProduct()->setIsSuperMode(true);
$quoteItem->save();
No need to save quote, just quoteItem.
Should be an easy fix: If I'm not mistaken you are trying to save the price of a quote item, but you save the quote instead. You need to save the quote item itself as well.
$quoteItem->setCustomPrice($price);
$quoteItem->setOriginalCustomPrice($price);
$quoteItem->getProduct()->setIsSuperMode(true);
$quoteItem->save();
$quote->save();

Pulling Custom Option ID from Magento

We have a SOAP connection to magento that is working great - we're able to pull product info, skus, descriptions, etc. using the magento API. We've been able to successfully add products to the cart programmatically with custom options, and that works great. The problem is this:
When you create a custom option for a product, it is assigned a unique ID that has to be called in order to pass that option value to the cart. For example:
www.mysite.com/magento/checkout/cart/add?product=7&qty=1&options[OPTION ID]=robots
Assuming I have my custom option ID correct, this will add 1 product with the option "robots." Which is nice.
However, we have hundreds of products, and while they all use similar custom options, each option is given a unique ID. That means I need to be able to call the magento API and get custom options details (specifically the option ID) so that we can add them to the cart properly. I have been back and forth with Varien Support (Magento), but they are less than helpful, as usual. Now, I know I can find these options by using firebug in Firefox or Chrome, which I've done to test the "add to cart" script. However, that's not a proper solution. I need to be able to grab this data from magento based on product id.
Can this really be that hard? Shouldn't this be tied to the data for the product somehow? I've done a var_dump on the catalog_product.info and i see where it asks if there are options but doesn't provide any details on them. Thoughts?
Thanks in advance.
Do you need to get all options ids for specific products or just some particular? For the latter you can use this code:
$productEntity = Mage_Catalog_Model_Product::ENTITY;
$colorAttribute = Mage::getModel('eav/config')->getAttribute($productEntity, 'color');
$colorAttribute->getId();
For the first option code in this question might be useful How to get all super attribute options for a configurable item in Magento

Increase product amount in shopping cart

We are selling ready made packs ( total weight 18 kg ) which is include several food products. But we want to make a special thing to our customers which they should make their packs by selecting our product range.
So that, they should make first a base pack with selected products then they can multiply this package in the checkout section.
What I want to do, modify the checkout section of magento to multiply selected products with a text field and calculate total amount by button.
Like in image...
Does anybody help me to make such a thing?
Personally I would do this with frontend code, in prototype. The reason being that you will have to modify your template anyway and your customers will need javascript enabled to get to your cart anyway.
General approach is to have the onclick for your apply button take the id.value for your quantity text box and then update everything with the quantity class in the cart page. Then have this script call the same update url as the standard update quantities button.
Here is my suggestion: in your module you create an observer listening to controller_action_predispatch_checkout_cart_index. In your observer's method you can get the items in cart with $itemCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();. now you just have to loop through this condition and set the quantity:
foreach ($itemCollection as $item) {
$item->setQty(here_the_integer_you_want);
}
edit: sorry, didn't read about the input and button.
For the input and button, you just have to edit the template (app/design/frontend/base/default/template/checkout/cart.phtml). Or, if you want tom make an independant extension, you could add them by an observer. Check out inchoo's post. This form you've just created will call your controller/action, where you can set the items quantity (so no need for an observer anymore), with the same technique I've put earlier.
Let me know if that's not clear.
HTH

Magento: adding catalog price rules to options of a bundle

I've looking around and couldn't find how to do this.
I'd like to be able to set a catalog price rule for a specific option of a bundled product. Basically I want to set the price of any selection from option1 to be $0 in that bundle (but keep the price the same for that product if someone just adds the product to their cart from outside of the bundle). is there a way to do this that i'm missing?
The second approach I was thinking was to set the price to $0 of all those selections for that option (i saw a post on here on how to do something like that programatically) although without testing that that works, it seems like it wouldn't.
any ideas would be welcome.
This is something that Magento does not do by default. Additionally, beware of trying to change this through cleverness, as Magento has significant validation for prices once items are added to the cart (e.g. it will try to recalibrate them to the database on every page load). It sounds like trying to restructure the flow so that you aren't trying to use the shopping cart rules might be the best bet here. Would it be possible to use some other mechanism to give discounts, like a coupon code?

Resources