Magento cart/order validation and manipulation - php

I'm completely new on working with Magento and I'm going to create a module in order to validate and in some cases manipulate some cart/order information.
To be specific I'm gonna restrict the customer from buying an item more than X times.
I've started working on this a bit, but I'm not so satisfied with the solution.
This is how I've done it so far:
I've created a new module with a controller which subclasses Mage_Checkout_CartController and there I've implemented the addAction-method. So every time a product is added to the cart I search through the user's order history and look for previous orders containing this product. Then I prevent it from being added and trigger an error-message.
It has a lot of shortcomings. For instance, if the customer isn't logged in at the time he can add the product, you can update the cart with too many... etc.
I would be me comfortable if I could hook on events, but I don't know where to start. Haven't found so good guides about this.
I want to do this verification when listing cart, updating cart and before submitting order.
So, my questions are:
How do I add observers on these events in my module? I couldn't get config.xml-configuration for event observing to work. I also need to know the names of these events.
How do I manipulate the quantity of an item in cart / delete it? When updating cart with too many of the products I want to change the quantity and trigger an error.
If you have any other ideas on a better solution for this, you're very welcome to comment.
I appreciate any help. Thanks.

I think this article should answer on all your questions. See events list at the bottom of this article. Do not forget delete cache after each change made to config.xml and other xml files in your module etc folder.

Related

Magento Multi-Step Product Configuration

I'm currently attempting to extend the inherent Magento product view to divide the custom options belonging to a configurable product into sections loaded by ajax. The end goal is to create a step by step process guiding the user through each step of the product configuration process before they would add the product to the cart.
Advanced Custom Product Options
We are also utilizing Mageworx's Advanced Custom Product Options to allow for the larger customization of products.
Simple Products Linked to Configurable
Our current environment is set up to handle simple products as configurators for their parent configurable product. When a user navigates to the simple product, they are directed to the configurable product with the option represented by the simple product pre-selected.
End Goal
The hopeful endpoint would be that once a user clicks on a simple product, they would be redirected to the configurable product with a single pre-selected option depending on the simple product. The user would then be able to customize their order, or jump directly to adding it to the cart as is. The customization process would re-evaluate at each step to determine if there is a concurrent step, for example, if the user were to select to add trim, there would be an additional step to customize that trim, whereas if they opted out, the step would be skipped.
The idea is simply a product configurator within the Magento framework, something similar, in function, as to what appears below. The checkout steps being Style, Color, and Size, each which have their own page.
The Overarching Question
I am fairly new to the development of Magento, and am wondering what the best method of attacking this problem is. I have thought of using an observer to hook into before add to cart, but I am unsure it would allow me to refresh the blocks and load in new data. My other idea was to tap into view.phtml and set up some ajax loading within the page itself, but that seemed a bit 'hacky'.
I have spent quite a while searching for examples on how some have overcome this problems, but haven't found much outside of some very vague ideas on what could work. If any one has insight onto a solution to my problem, their thoughts would be much appreciated.
Current Approach
So far I've tried creating a Router for a Controller to handle POST requests to change out the Product Option sidebar when a value is changed. It seems a little bit slow, however.

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.

Custom Add to Cart

I want to add an item to the cart, but if this item is already in the cart, Magento by default just increases the quantity of this item. I want to add a new row in the database table sales_flat_quote_item where magento saves its cart items. The reason why I want to do this is to show in the cart page the 2 or more items in different rows, for some reason.
I tried to track the flow of magento in saving the cart, but I ended up with this code: Mage::dispatchEvent('sales_quote_product_add_after', array('items' => $items));
I'm trying to find where is this method, but I cant find it. I hope that the solution that I've been searching for is here. Does anyone know where this method is located? Or, if I'm wrong with my assumption, what might be the solution to my problem?
This is the call of magento event which calls other module methods one by one that have registered for listen to that event. So you need to look every config.xml file of every module for sales_quote_product_add_after string under <events> tag. For more about events and observers in magento go to:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
Default magento implementation has no observers for this event and it wouldn't lead you to the solution. Check this blog post about the cart flow, especially the part entitled 'Quote Model Adds a New Item or Changes Existing One Qty'

Magento - How to set a per day inventory for an item

First post on stack overflow! Here we go…
OK, so I have Magento community edition v1.5.1.0 setup on my server and have a single product all ready to go. The product has a date attribute which is the date the customer would like the product delivered. The product is an item that I make when a customer orders it. There are only so many of this product that I can physically make a day. So my question is, how do I keep track of the total orders that have been set for delivery on a certain date and disable that date from being selected on the front end when the cap has been met.
I've tried looking for plugins but haven't found anything that suits this situation yet. Any advice/pointers would be great.
I'm quite happy to start prodding at the internal code if need be. Just wanted to make sure I wasn't missing an easy option before diving in!
Thanks in advance
Justyn
Yes this is quite an unusual module and I can understand why there is no pre made plugin yet.
First of all - don't prod the internal code!!! You must extend the platform by writing a custom module, otherwise your application will not be future proof.
Here is a quick plan of attack to show you some Magento techniques for achieving your aim.
Add an attribute to the products which you wish to have this constraint apply to, "delivery_date_capacity"
Create a custom module which listens for orders and stores a record of product and date to be delivered, as well as the threshold orders per delivery day, which is fired when product has the identifying attribute in( "delivery_date_capacity" ).
Depending on the user experience you require, (when the daily threshold is met), when customer clicks "Add to Cart" the module could redirect with a message saying this delivery date is not available, and suggest ones which are close to it before and after.
You may wish to add reverse functionality to automatically add capacity if orders are cancelled. Add an observer which is fired when a product is loaded in the admin which checks if the product availability should be checked again by referencing "delivery_date_capacity" attribute, when an order is cancelled.
You could also load a calendar which show the available dates to reduce the click rate of user, especially if you anticipate suggested dates not being easy for a customer to choose.

Ubercart add to cart form with quantity option per attribute

I need the add to cart form on each product to display separate quantity fields so items of different attributes can be added at the same time. This is for a photography site, so it makes sense for the customer to order 2 6x4 prints and one 10x8.
Right now the add to cart form is something like this:
Here is a mockup of what I want to have:
I have no idea which hooks to use, or if it's even possible to add multiple product variants in one POST. Is anyone familiar enough with the Ubercart api to tell me how I can go about doing this.
I found this googling for a solution to the same problem. Unfortunately I couldn't find anything.
So I made a patch and sent it in to an issue with a similar request - http://drupal.org/node/782154#comment-3925090
The patch isn't tested very well yet, but I will be going live with it or a subsequent solution in a couple of weeks.
Hope it helps.

Categories