Introduction

One of the biggest things that has been missing from E-Commerce is the ability to handle product variations, SKU's or sub products.

In developing this I based it partly upon how invesntory systems work. Basically even though the products are more or less the same they are still treated as a separate product, listed under a different code. Such attributes like stock on hand must be kept independant of each other. Even in the pick and pack stage these items are still treated as separate products.

The only time that these products are treated as the same product is during the product selection process.

Modifications

Basically what I did was to create a parent/child relationship between the products. The is a single parent product which is used to give the complete overview of the product, and allow the customer to choose which child product that is going to be selected. This method of choosing the exact product can be done by anyway that the your require from the theme function product_$ptype_view.

The 2 new product api hooks are "product_subtypes" which returns a list a ptypes that can valid sub-products of the current product, and indicates that this product can be a parent product.

the second hook "cart get subproduct" is used in the cart_add_item() to work out which sub-product to choose and the place this into the cart. This allows us to get around the problem of not being able to enter multiple products of the same nid into the cart.

summary

This is a first draft, and any questions or concerns please post them. I feel this is a good solutions which can be used to implement any type of product variations that you could require.

At this stage none of the standard products do not have this capibility, but I have created an apparel product which can be used for size and colour for choosing clothing.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gordon’s picture

FileSize
2.72 KB

here is the apparel product which demonstrates the implementation of the new productapi.

kbahey’s picture

Gordon

Well done ...

Please contribute it to the modules/ecommerce/contrib directory, since it is really useful for many.

molly_n’s picture

The install file says to apply apparel.mysql -- is there an apparel.mysql? There wasn't such a file in apparel2.tar.gz

I applied the subproduct patch to the cvs version of ecommerce (running on Drupal 4.6), and uploaded apparel.module and updated the db (ALTER TABLE ec_product ADD parent INT(10) NOT NULL, ADD children VARCHAR(255) NOT NULL, ADD variation LONGTEXT NOT NULL) and i'm seeing the option to create an apparel product (as opposed to a tangible product...?). Once the product is created, i'm seeing the "add sub product" link. When I click on "add sub product", it takes me to a product creation form with the fields prefilled with the parent product's information, throwing up an alert: "The path is already in use." So I change the path, submit the form, and it takes me back to the list of products, with a message at the top that says, "Your was created", but I don't see my new subproduct linked anywhere in my list of products.

If I go to administer > content, i see the "sub products" i've created in the list of nodes. If I click to edit them, there isn't a product tab to apply any kind of product information to them.

So, is this about a mystery apparel.mysql? Is it a theme issue where I'm just not seeing the subproducts with the parent products? I'm having a hard time envisioning how this is supposed to work -- do you have a working example up anywhere?

Thanks for working on this super-important piece of ecommerce.......

gordon’s picture

The apparel module actually uses to the ec_product_tangible table to store the information. One of the changes allows for basic variation data to be stored in the ec_product table, in the variation column.

The biggest thing to remember this that each variation is a product in their own right. What makes the parent different is that it knows how to display the product, and variations so that the customer can select exactly which product they have.

I do not have an example on the web. I may set up a site later on today which has my development account running.

gordon’s picture

FileSize
13.02 KB

I have fixed the issue with the path, here is the patch.

molly_n’s picture

So I've realized that when I click "add sub product" and create the product (although all the form lets me do is make a new path alias, title, but no product info -- are sub-products supposed to be able to have different prices as well?), it's not adding anything to ec_product or ec_product_tangible -- it creates a new node, but doesn't appear to impact any ec tables.

I am using flexinode content types as products, might that make any difference? If I try to create a regular "product" product, i get a screwy form that doesn't allow me to create a title or price, and then when i submit just returns an error that says "you must specify a title"....?

I think I will try a totally fresh install somewhere and see if I can narrow down where it's going awry for me. Could you let me know what versions of Drupal and ecommerce you've got this running on, if you've applied any other patches, etc?

gordon’s picture

The main implementation of this is done in the productapi. Other node types like flexinode types that are using the nodeapi to become product cannot be used as sub-products.

Saying that you maybe able to create a small custom product that allows flexinode sub-products, but it will need to know how to display these products.

Product Variations can be quite a complex thing. At this stage only the apparel module makes use of these hooks to allow the entry of sub-products.

With your flexinode products are you seeing the add sub-product link? because you should not be.

gordon’s picture

I have taken a look at you product, and it seems that when viewing a product that is created via the nodeapi the product_$ptype_view is not called and the additional form is not added to the bottom of the display. This is key because it displays a form that allows you to choose which product to add to the cart.

Try just creating a standard product.

molly_n’s picture

Yep, definitely seeing the "add sub product" link with flexinode products.

I'm also unsure of what your interpretation of "sub-products" and "variations" is. Are you using the two interchangeably -- i.e. they're the same thing? If I had to define them, I would say sub-products are children of a parent product with their own unique SKUs, but variations would be product options not requiring a unique SKU.

For example, if the product is Coffee:
Decaf and Organic would be sub-products, whereas Grind Options (espresso, drip, french press) would be variations.....

anyways, I'm going to set up a fresh install and try to get the patch working with regular 'product' products for now...

Thanks again..

gordon’s picture

You are seeing the add sub-product with the flexinode because it is an apparel product type. The problem is that when view option in the nodeapi is called the product module should then call the theme option product_$ptype_view so that the additional product information like price should be added.

Yes you are correct with my definiation of variations and sub-products. E-Commerce can do variations now. The donation module is a very basic example of this. But you could create a product that has more options that can be selected from and use the variations column to store this information.

gordon’s picture

FileSize
13.05 KB

Here is an updated patch. It fixes a problem with some versions of the node permissions api.

crunchywelch’s picture

on line 247 of product.module after this patch is applied the table definition for node is missing, causing a n sql error, it should read:

if (db_result(db_query(db_rewrite_sql("SELECT COUNT(n.nid) FROM {ec_product} p INNER JOIN {node} n ON p.nid = n.nid WHERE p.nid = %d AND p.children <> ''"), arg(1))) > 0) {

gordon’s picture

Thanks, I actually found this, and is in the latest patch.

gordon’s picture

FileSize
13.15 KB

Sorry, I now see the issue you identified, I have fixed this and also I have made it possible to use a nodeapi product as a master product.

It doesn't let you use it as a sub-product, but I do not know if there is a need.

Please correct me if I am wrong.

matt westgate’s picture

Gordon, you did a great job on this. Thanks so much getting in touch with before you started this project. I'm very pleased with the way it turned out.

My current gameplan for 4.6 is to backport ecommerce HEAD into the 4.6 branch. HEAD contains a bunch of bugfixes and other features I want to see in this release. I plan to do this either today or tomorrow, after which I plan to commit this patch.

Sound like a plan?

mfb’s picture

This is a really good start. Can you add some permissioning to the sub-product creation? We want some users to be able to add sub-products only to their own products. Other (admin) users may need to add sub-products to any products.

For a multi-user/multi-store site it could be problematic if all users who are allowed to create products can also add sub-products to other user's products.

gordon’s picture

Matt:-

This sounds great. What you could do it commit the backported version and then I will create a new patch based upon the new version. Once this has been done it should not be too hard to port this to cvs.

mfb:-

We are doing something like this for Civicspace. If you talk to chx who is doing this side of things he will be able to fill you in one how we are doing it.

Other:-

There are a couple of areas that I would like some feedback from.

1. Should you be able to create a sub-product by creating the product through node/add/product and then like the book module select the parent product?

2. Would it be useful when adding product to the cart which has a sub-product, being able to select more that 1 product. eg. Selling software, you have a check box to allow the user to both download and ship the software. So in a single selection both sub-products will be added.

syllance’s picture

currently testing the patch, i'm getting SQL error for an UPDATE on table ec_product_apparel, when the stock is updated after order. seems like this should be moved to ec_product_tangible, and it does not prevent the thing to work. this is at line #60, let me know if you want a patch.

otherwise your patch seems to work fine :-) my feedback is :

1. Should you be able to create a sub-product by creating the product through node/add/product and then like the book module select the parent product?

yes, please :-)

2. Would it be useful when adding product to the cart which has a sub-product, being able to select more that 1 product. eg. Selling software, you have a check box to allow the user to both download and ship the software. So in a single selection both sub-products will be added.

could be useful too, although if we take XS/S/M/XL/XXL + red/blue/green/yellow t-shirts, the interface for selecting subproducts should be carefully thinked.

thanks

gordon’s picture

FileSize
2.58 KB

I have updated the apparel module to fix the sql error in the stock control.

The problem with working out what possible parent a product can have is that I need to go through all the productapi's and find the ones that can have this product as a sub-type. Not impossible, but just a little awkward. I will look into it. This mod should allow you to use nodeapi products as sub-products.

The rendering of the page that is presented to the customer is entirely themable. So this is a task of the person who is developing the shop. The apparel module just needs to know the size and colour so it can pick the product.

If I was developing a shop using this I would create some js so that when the customer picked the size the colour fields would be updated with the list of valid colours.

rjung’s picture

As a fellow Drupal e-commerce variations developer (see here), I'm interested in trying out Gordon's implementation. I ran into some problems when trying it last night, though:

1. Do I need to apply all the patches in this thread, or is applying subproduct_2.patch sufficient?

2. In a related vein, it looks like the latest "4.6" version of the e-commerce module has a bug -- the payment.module version 1.23.2.2 has a call to function form_group_collapsible(), which AFAIK is not a Drupal 4.6 API.

3. There is no step 3. ;-)

--R.J.
http://www.electric-escape.net/

gordon’s picture

You only need to install the latest patch.

Also Matt in in the process of backporting cvs to 4.6, so there is most likely some more things that needs to be fixed.

mfb’s picture

What is the feasibility of adding images to subproducts? Is this possible by hacking on the apparel module? I've already added additional options, manufacturer and style, that was easy enough.

The parent product has an image, but some of our subproducts could use their own image for marketing purposes. I could use img_assist, but ideally I would add an image upload to the module, just like the image module has.

gordon’s picture

Yes you can do anything that you can do to a normal product. Using the product_sub_products() which will return an array of all the sub-product nodes.

Then you can theme the master product exactly the way you want to.

rjung’s picture

I'm still getting bugs using the latest e-commerce suite with the latest patch and latest apparel module -- the Title field appears twice, and every attempt to put something in the cart results in a "the product with this color and size cannot be found."

Is this happening for anyone else, or is it just me?

gordon’s picture

FileSize
2.59 KB

I am not getting any of the problems that you are having with the ecommerce module. Howevewr I did find that the data array is now an object so I have made some changes for this.

This could be stopping you from selecting a sub-product.

syllance’s picture

the double title thing is not related to variations, i just send a patch that fixes it : http://drupal.org/node/36285

gordon’s picture

FileSize
13.45 KB

I have added a couple of performace enhancments. The main one is for the product_get_variations() which now uses the cache to reduce the number of hits on the database.

kbahey’s picture

FileSize
529 bytes

I could not get the apparel module to add products to the shopping cart until I changed $data->size to $data['size'].

Here is a patch to fix this.

gordon’s picture

This was fixed back at #25, I found that from the backport of the ecommerce module changed the data to be an object and not n array as it use to be.

nigma3d’s picture

is there anyone that can post the successfully patched files for those who, for the life of them, cannot ever get patches to work. or, you can send them to me: nigma3d2@hotmail.com -- thanks

kbahey’s picture

Gordon,

Just to clarify:

I used the latest apparel tar ball (from #25) as well as the subproduct patch 3 from #27, on a 4.6 install (from several weeks ago). I still got the error I mentioned, and it was only fixed when I patched it.

gordon’s picture

I have checked the archive from #25 and my copy on my server and it has your patch.

averageyoungman’s picture

Gordon, excellent work. I was in the process of hacking E-Comm and found this. Thanks a ton for submitting it.

Just thought I'd let everyone know that, in my case, it is the object format ($data->size) that works for adding products to the cart. I had actually applied the patch (#28) and was getting the error "unable to find the correct product based upon the size and colour combination." I then changed the format back to $data->size manually and the issue was resolved. This is within a 4.6.0 installation. I have not tested it with 4.6.3. Hope that helps.

gordon’s picture

FileSize
15.62 KB

I have extended this patch so that the creation of subproducts can be done via the normal product creation. ie. node > add > product. This also means that you can now have any type of node as a sub-product or a parent product.

I think that now unless there is some big error, or problem this is ready to be commited to cvs. Can you people please test this and make sure we are ready.

Also this needs to be applied to the very latest version of ecommerce for 4.6 as Matt had backported the cvs version most things past around are objects.

If you have any problems with php compaining about objects then check your version of ecommerce.

gordon’s picture

FileSize
2.62 KB

and a new version of the apparel module

averageyoungman’s picture

Thanks Gordon. I will install and test the additions.

You may want to take a look at the following thread, here.

There is a problem with the checkout process for anonymous users. I have taken a quick look and identified some of the problem ( I followed up in the thread with the info) but I am still having problems with that portion of the checkout pocess. It appears that checkout for anon users has not been completed yet. I will submit it as a separate bug if it has not been submitted already.

averageyoungman’s picture

The additions work for me within an install of 4.6.0.

Create product-->create sub product-->add sub product as child of new or existing product.

I added the updated code by hand though, because I didn't want to revert back to the unpatched ecomm module in order to apply the patch. Gordon, would it be possible for you make a new patch that will work for an ecomm install that has already been patched? Or will the new subproduct_4 patch only recognize the new additions if a previous patch has already been applied?

gordon’s picture

FileSize
2.68 KB

here is an updated apparel module which checks the stock levels

nedjo’s picture

I've been working extensively with this patch (though I have yet to test the latest version--will ASAP and report back) and find it performs very well--fine work, Gordon.

Two comments at this point:

1. Storing the parent-child relationship in two places (on both the parent and child sides) isn't optimal. I'd prefer instead to load the $node->children dynamically as an array. Something like:


$node->children = array();
$result = db_query(db_rewrite_sql("SELECT nid FROM {ec_product} WHERE parent = %d"), $node->nid);
while($child = db_fetch_object($result)) {
  $node->children[] = $child->nid;
}

2. The variation handling through serialized arrays is limited. I've been working on an approach that would store variations in normalized tables, currently in my sandbox at:

http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/nedjo/modules...

We could consider leaving the variations out of this patch and instead leaving variation handling to contributed modules.

mfb’s picture

This patch should also add the parent=0 clause to all sql queries in the stores module. Otherwise the stores pages and blocks are filled with subproducts.

xopher’s picture

What's the status on this for users of Drupal 4.6.3/4?

Seems like everyone here is using/testing with old versions? Or am I just not following what's going? I've been able to create sub-products but not add them to the cart. What exactly is the reason for this? Something going from data array to data object? Is it safe to presume then, that I need it to work the "other" way, whichever that might be?

I'll try to update apparel.module myself with the suggestions above $data->size becomes $data['size'] or vice versa, just wondering why the chatter stopped on the 15th since this is such a huge development in the ecomm/drupal world.

If anyone has the time to help me debug this, feel free to contact me through the site... thanks, I'll report back on success/failure after trying to update apparel.module.

xopher’s picture

Okay, changed $data->size to $data['size'] and now I can add products to cart. New problem is I was doing quite a bit of tricky formatting on product detail page, and now there's a SHOPPING CART embedded beneath my product description (which isn't good.) Also, after adding to cart, the link to the product in cart goes to the node (which ignores my styling) instead of the path_alias set for the product. Any ideas how to change this behavior? I'll admit, I've put my products section together with spit and glue, so it's not surprising that a fundamental change such as adding sub-products has blown the display to pieces. All of my products are IMAGEs (not sure how anyone could create a store with products that didn't have images?!?) but basically I'm just wondering what approach everyone else is using. Flexinode??? Haven't tried it yet, sounds like it's the cat's meow, I've just been under a pretty tight schedule and haven't had the time to check it out...

Any ideas? Bottom line is I'd like the sub-products in the cart to link back to the PARENT's product page when clicked. I'll probably sort the mystery shopping cart in my detail page myself... although I'm not sure why it's there. Is my detail page no longer being rendered by node-image.tpl.php? Could that be the prob? Any help appreciated.

xopher’s picture

I'm pretty sure I'm using the follow-up posts here too much like a forum, when in fact it seems we should instead be tracking issues for the developer and not debugging our own hacked implementations. I just thought one last follow up was necessary to answer my own questions above. Basically, I've solved all of the above problems, by digging deeper into Drupal than I ever have in the past. This was a rewarding experience, I suggest other Drupal newbs do the same if time permits. I feel like I'm getting close to that ah-ha moment, when I fully realize all of Drupal's power. To date, I've accomplished everything I needed by going in and "nudging" bits and pieces as needed. After my recent foray into Drupal's plumbing, the way modules share information and invoke each other's methods is starting to crystalize. I hope at some point in the near future to be able to get the results I want for any given project by doing things "the Drupal way."

If anyone wants to discuss the ecommerce.module and subproducts in particular, feel free to contact me through the site. And to those of you who have posted answers to my problems in forum threads, thanks.

gingibash’s picture

Category: feature » support

hello,

i have one stupid question, how do i apply this patch?

Timotheos’s picture

Category: support » feature

That's not a stupid question ;-)
You can read about how to patch here http://drupal.org/diffandpatch

Eglish’s picture

How is the progress of this patch? I'm a bit confused with the status of this patch. I'm sure plently of people are looking forward to this patch just as much as me.

gordon’s picture

Basic what is happening is that we are waiting on the completion of the work to upgrade E-Commerce to 4.7, and then Nedjo is going to port his patch to 4.7.

So once the formapi has been done, What was done for GoodStorm, which this patch and some more. is going to be merged, and released with 4.7

Once this has been done and is stable is may be back-ported to 4.6

paul-c’s picture

Hi,

I've tried installing the subproducts module using the latest patch but keep getting

Cannot use object of type stdClass as array in /../drupal-4.7.0/modules/ecommerce/subproducts/subproducts.module on line 1245

Any suggestions on how to get the module to work?

nedjo’s picture

Version: 4.6.x-1.x-dev » master
Status: Needs review » Closed (duplicate)

This issue has been moved to http://drupal.org/node/49118; apologies for the confusion. In that issue there is a current patch, but there are remaining issues with the upgrade and testing is ongoing. Please try that patch and report back (on that issue). Thanks!

paul-c’s picture

Thanks for the clarification. Seems the reason for the problem was that I was running PHP 5 on my test server. Reverting back to 4 enabled the original subproducts module to work.

antinomia_erik’s picture

Just a quick question...
Hoping someone can point me in the right direction...

I'm using Flexinodes to create a specific product line...
And I'm working on using the Subproducts functionality to add the option of purchasing the product in small or large sizes...
Which is quite similar to the way apparel works out...
I have found that everything works fine except it does not INSERT the children nodes into the ec_product table as new rows when you 'generate sub products'...

The children nodes get created in the node table just fine...
And their nid's get added into the children field for the parent node on the ec_product table brilliantly...
To test everything out, I added the rows for the children into the ec_product table manually and everything worked out...

So...
I've been trying to track down where in the code it adds the children nodes as new rows in the ec_product table so I can tweak it to work...
Can anyone help me find that spot...?

Thanks for the hand...!!!