Magento (PHP): Add Product invisble to Cart - php

I am looking for a way adding a product to cart without display it.
Example:
New customer order -> welcome letter in the cart, but the customer doesnt see it.
The added product cost always 0 €, so it never changed the shopping cart total.
The solution should be changed as few as possible.
Any idea?
Thank you!
(Sorry for my very bad english...)

I would say to change the formulation a bit and add it to the order just before the order is saved. While the quote is being converted into an order (sales_model_service_quote_submit_before is one good event here, but there are others), add the product to the order.
This way, you don't have to hack around trying to hide items in the cart, and the effect is the same.

Two options:
Define a new product type that extends the Virtual product type in Magento, and then use a custom Item Renderer for Checkout and Cart that doesn't output any html. Here's a useful tutorial for that process.
Define a new boolean product attribute called "cart_visibility" or something like that, override DOCROOT\app\design\frontend\base\default\template\checkout\cart\item\default.phtml in your own theme and test for that value before outputting the item's attributes e.g. on line 28:
<?php if($_item->getCartVisibility(){ ?>
Don't forget to close the brace at the end of the file obviously.

if it's 0 then why add it to the cart just add it as a message to cart page by adding it to cart template or injecting your own block to this template

Related

how to force magento to use my custom price

Hi and thanks for reading,
I'm working on magento EE 1.13, here I added new price fields to product edit page (offer price and deal price) beside the original price and special price fields.
and here the problem come: I need to make magento use deal price or offer price when they are set, I find it complicated and not easy to find.
what I think is maybe I can override addFinalPrice method or override getFinalPrice method, but I'm not sure if it's right, need your help guys in this.
any idea will be appreciated,
thanks in advance.
update: is there any way to save final price in database after saving finishing editing the product?
you need to add observer on event catalog_product_get_final_price. please check my below answer.
Magento : Add Product to cart with custom price
you misunderstood the concept i guess:
the purpose of the getFinalPrice method is to find the price respecting tier price, special price, and the regular price.
If you just want a final price, give the product a regular price and don't use tier_prices, special prices for the product. It is not for saving but for loading...
Update:
"is there any way to save final price in database after saving finishing editing the product?"
You don´t save the final price, the final price is built dynamically. If you want additional prices you need to add a price attribute and use the final price event to check your additional prices and use them - if needed - as final price.

magento add extra product to cart automatically

I want to add products automatically to my magento cart.
The situation is I have product Y and it needs extra components to be assembled, so I want that the assembly parts to be added automatically to the cart when product Y is added.
I have googled a lot and could only find extensions for promotion product and free gifts but I need a solution which just adds the product to the cart and that it adds in the same quantity of product Y.
Are there extensions which I can use or how can I alter my code to get my desired behaviour?
You can use that kind of extension or make you own with a listener on the sales_quote_item_set_product event to check if the product added is yours and need a new product to be added.
I had a similar request that was abandonned as it is illegal in France (and in most of the part of Europe) to sell a product linked to another and that was not requested. Moreover, the customer can already have this second product and do not want it. We added a popup that said which products can be needed with this one. This second part is just an advice.
Well, if you looking programming solutions:
Write Custom module that observe event checkout_cart_add_product_complete.
In your observer, add products by following code.
The code is:
$prouctToAdd = Mage::getModel('catalog/product')->load('product_id ');
$qty = 'currently_added_produt'
$cart =Mage::getSingleton('checkout/cart');
$cart->addProduct($product, array('qty'=>$qty));
$cart->save();

Creating a new product while creating a new order and also add that product in order

Is it possible, in Magento, to build a button to add a new simple product while creating an order in the backend?
It is not important that the product would be saved in the catalog: it should to be saved into the order. Only two of the product's attributes would be needed: name and tax rate (optionally others). Qty e price could be edited from the order cart.
Yes, it's posible, you need a controller in order to do that.
Remember that you need to provide us the code that you're working on, and don't expect that we program all for you.
Thanks
Thanks to Beto Castillo I went into the spirit to think more clearly about this. The project is yet under construction. Anyway starting steps could be:
Create the right inputs to add a new product in a ChildHtml of sales/order/create/data.phtml, and adjust the layout files. Along with the inputs there will be a button to trigger the creation of the product. In one of this inputs I have to load options for tax rates.
There is plenty of ways to create a new product, i.e. Magento: Adding new products programmatically. I just need to get the above inputs.
The product should be added to order quotes in the database (I could start with this: Create order programmatically in Magento)
Update the create order view, calling an ajax function to reload the item grid. Part of the code already exists in Magento. All this steps should be packed into the same ajax call: here comes the controller action named by Beto Castillo.
So the answer is yes, it is possible.

Drupal/Ubercart Check if a certain product is in the cart on checkout

I have one certain product that needs to be in the cart under certain circumstances. I have been looking at the Ubercart api documentation and I don't see any hooks that would be the obvious place to see if a certain item exists prior to checkout.
I could use the hook_add_to_cart hook to add the special item whenever the first item is added, but I'm concerned that the visitor may remove the item and then complete the purchase without the required item.
Any suggestions how to make sure the special item is in the cart on checkout?
You can have a module and run something like:
function mymodule_init() {
if (preg_match('/checkout/', request_uri()) {
$items = uc_cart_get_contents();
foreach ($items as $item) {
// code
}
}
}
That will fire up on the checkout page, and fetch the cart contents. Everytime they hit the checkout page, uc_cart_get_contents() returns the cart contents.
http://www.ubercart.org/docs/api/uc_cart_get_contents
There are probably better ways to do what you want to do though, like using a Conditional Action to prevent checkout if Item B is in the cart but Item A is not. You can also look at Product Kits, but I don't have much experience with that.
From what you have said it sounds as though the product kit module might be very much worth looking into as a way of ensuring any items associated with the main product are kept in the cart.
Product kit comes as part of ubercart and you will find it on the modules page under 'Ubercart - extra'. If this is no good then we can see about using the API :)
An old question, but I found a great solution.
hook_uc_cart_item_delete() functions specifically on certain entities when they are removed. You can just set this hook in your module, check for the specific entity being removed that is dependent on the other item, and then use uc_cart_remove_item() on the item you want to remove.

Where is the setCouponCode function is defined in Magento

I know that setCouponCode is catch inside __call in Varien_Object but I can't figure out where it is defined.
I need it because I want to show the coupon code even if the discount equal zero.
I believed it's done inside this function.
So if anybody know where the function is defined or where I can modify the code to get the coupon code displayed all the time, please let me know.
Most text editors have a 'search in files/folders' option. Open the source folder and search for function __setCouponCode.
Ok I figured it out.
the SetCouponCode is setting the value for coupon_code in the magic function.
Related to this, did anybody notice that the quote is not being deleted properly? I say it is related to this because the coupon code variable keeps the value [if any] after you delete a product from cart.
Try:
1. Set some Shopping Price Cart Rule to a product and make it display a banner on the header section for example.
2. Add product to checkout/cart in order to trigger the rule and show the banner on the Cart.
3. Delete the product from cart and you will see the banner still showing on the header section.
Note. If you have another product in cart this will not work because when you delete the one that triggered the rule&banner the Coupon Code will get replaced with the one that belongs to this other product in cart.
SO this bug only works if only the product that triggers the rule&banner is in cart.
If anybody has a fix on this or can replicate these conditions: I'd love to have a conversation about magento's deficiency to make a proper product delete from cart - which implies a proper quote refresh.
Reference:
- app/code/core/Mage/Sales/Model/Quote.php -> public function removeItem($itemId)
And yes, magento core issue.

Categories