woothemes/woocommerce — pull request #5678 by coenjacobs (2014-06-14 21:44:09+00:00)
Title: Order factory refactor implementation (take two)

Alright, here is a polished version of my original #5558 pull request. I went the `WC_Abstract_Order` way in this one and it's also fully backwards compatible, as `WC_Order` remains the actual order object and it can be instantiated as it always has been. I'll get to this in more detail, further in this thread.

## Questions answered from old thread
Let's answer the questions from the old thread (#5558) first and see how this works now:

@thenbrent
> I did notice in the WC_Order_Factory:: get_order() that the only way to instantiate an instance of our custom order type for now is to hook to the ''woocommerce_order_class'. Is there a reason orders shouldn't use a 'order_type' taxonomy, in much the same way custom products use a 'product_type' taxonomy? Then the factory could automatically create an instance of the product class based on that taxonomy.

As WooCommerce core doesn't use any custom order types yet, I haven't decided on how to do this. The way the Products factory does it is great, maybe we should go that way, but I'd like to discuss that some more. It might also be worth to have it just be filterable, but a 'Product Type' like select box might work out well if there are some more extensions implementing new Order extended classes, to keep things unified.

@maxrice
> Is there any harm in soft-deprecating it now? If the long-term plan is to prevent it from being instantiated directly, I think it's better to be intentional about it from the start.

I agree, soft-deprecating is fine, as long as all current code (mainly in themes and plugins) doesn't start spitting out tons of notices with deprecated notices. I've added a comment on the constructor of `WC_Order` that should discourage all developers to use it, recommending they use `get_order()` or the `WC_Order_Factory` class going forward.

> Finally, do refunds really need to be their own order type as a "credit note"? It would seem that maybe a better solution for supporting refunds is maybe both payments and refunds order item types? You could then support partial payments and refunds on an order and keep track of the information that way.

Probably not, or maybe not. I haven't given the refunds much thought, but it was what sparked this idea originally. I think this Orders factory is a good thing to have, as soon as we start jumping on the actual refunds implementation, we can decide on wether or not we want to use this in separate order types for it.

## Remaining questions
Pretty much the only questions that I have right here, is wether @mikejolley agrees with this implementation and how we're gonna handle the different order types in the `WC_Order_Factory` class. If we're gonna go for the taxonomy (very much like the Product factory) that's easy to implement. If we're going for the filter only, there is only a slight tweak to this pull request required.

Thank you all for the great feedback so far, this will make WooCommerce a little bit better and extensible. :smile: