Zen Cart Logo
Forums / Contribution-Writing Guidelines / trying to create an extra cart action

trying to create an extra cart action

Locked

Views: 8,390

Results 1 to 11 of 11
This thread is locked. New replies are disabled.
20 Nov 2007, 11:12 AM
#1
possum avatar

possum

New Zenner

Join Date:
Sep 2007
Posts:
58
Plugin Contributions:
0

trying to create an extra cart action

Hi all,

Sorry if you've seen this question before. I think I picked the wrong thread when I first posted it.

I'm trying to add an extra cart action that will allow my visitors to update the parameters assigned to a product after it is already in the cart. At the moment, they have to delete it and add it again.

My goal was to keep this as simple as possible, so while I'm sure there are better ways of doing this, what I have done is this

  1. Create a new page that just shows the product and the attribute controls.
    a. This is just a copy of product_info_display with the unwanted bits deleted.
    b. I've also copied the required language and modules/pages files
    c. Added an "update" button that calls my new action (see 3)

  2. Put a new link in the shopping cart from the attribute fields to my new page

  3. Create an extra cart action called update_product_attributes in the extra_cart_actions directory.

1 and 2 are working OK, but 3 is a problem. I had just planned to make my extra action do a delete of the current product and add a new instance with the updated parameters. I went for this plan because it would use existing functions delete and add functions. My extra_cart_action looks like:
Code:

if ($_GET['action'] == 'update_product_attributes') {
/* how do I remove the existing product with the old attributes??? */
$_SESSION['cart']->actionAddProduct($goto, $parameters);
}

Any pointers that you could give would be greatly appreciated.

Thanks,

Possum

20 Nov 2007, 11:41 AM
#2
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: trying to create an extra cart action

:rotflmao:

you're a few hours too late, or in luck, depending upon how you're felling about this. Here's why.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

20 Nov 2007, 1:48 PM
#3
justinhunt avatar

justinhunt

New Zenner

Join Date:
Sep 2007
Posts:
77
Plugin Contributions:
1

Re: trying to create an extra cart action

yes, Kuroi is right. I have just uploaded a mod that does almost exactly what you want done and in pretty much the same way.

I am pretty much scratching my head as to how we could have been both making the same mod on the same day, and a mod that until now doesn't seem to have been made.

Anyway there is a cart class function called remove. If you use that then your item will be removed from the cart. My code looks like this:

// intercept add_product actions if the edit_item_id paramater is set, 
//remove that item from cart, and proceed to add it again
if (($_GET['action'] == 'add_product') && isset($_POST['edit_item_id']) ) {
	$_SESSION['cart']->remove($_POST['edit_item_id']);

I passed in a separate paramater from the product_info page, edit_item_id, which the above code checks for.

I noticed that it was possible to tell from the product_id on the product_info page if it had been referred by the shopping cart page. So I did not change the shopping cart at all.

Anyway have a look at my module "Edit Cart," available from downloads page. And you will see how I did it anyway.

============
Kuroobiya - Personalized Karate Black Belts and Dogi
Kanebo From Japan - Kanebo Cosmetics Direct From Japan

20 Nov 2007, 9:57 PM
#4
possum avatar

possum

New Zenner

Join Date:
Sep 2007
Posts:
58
Plugin Contributions:
0

Re: trying to create an extra cart action

you're a few hours too late, or in luck, depending upon how you're felling about this

Definitely in luck! Thanks justin (and kuroi). Not sure if it's a matter of great minds thinking alike, but definitely good news for me.

I'll go and check it out now.

Possum.

24 Nov 2007, 4:02 AM
#5
justinhunt avatar

justinhunt

New Zenner

Join Date:
Sep 2007
Posts:
77
Plugin Contributions:
1

Re: trying to create an extra cart action

Hi Possum

I am going to continue my thoughts on this, in this thread. Because the other thread is really for Edit Cart mod specific enquiries.

Some really useful thoughts on alternative approaches to my gift wrapping problems. Thanks. FYI, I'm selling children's books, so you're right that the wrapping is not truly an attribute of the product itself.

In short I think the challenge is that there is no wholly satisfactory way of doing gift wrapping at the moment. Attributes have the problems that you have stated above. Products suffer from the fact that the additional "gift wrap" product can't actually be connected to the book(s) that are to be wrapped. This would stop someone from ordering a book for themselves plus a book as a gift because i wouldn't know which book or books were supposed to be wrapped.

The Cross Sell and Better Together mods provide the option to offer the gift wrapping, but lose the connection once the products are in the cart.

The Gift Wrapping mod (which I'm using) does nice things with offering flexible pricing options and separates the gift wrapping costs from the books themselves, but it is based on attributes, which again has the compromises you have mentioned.

BTW, Amazon inserts an optional extra page in the checkout process where you assign wrapping options to each book.

I note Dr Byte's concerns about removing the safeties. Assuming that I can figure out how, I was planning to update the logic so that it queried whether there were any mandatory attributes, rather than just looking for any attributes.

Actually I think you should heed Dr Bytes concerns. For 2 reasons,
i) You are going into uncharted territory and it is sure to be frustrating work
ii) I don't think you need to go into that area.

I don't think you need to because the method I suggested will probably do the same job, for far less coding and with far fewer hidden gotchas.

I would change the code for the buy now button on the product listing page. How about changing it such that it submits the same information as the add_product action of the cart_quantity form on the product info page? You would need to hard code hidden parameters for the quantity, gift wrapping attribute at least, into the buy now button code.

Also I still feel you may be unnecessarily complicating your requirements. It may be that some users will combine books that require gift wrapping and books that do not in the same order. But these will surely be an exception to the main. My guess is that you are in a hurry to get this ready for Xmas. (We all are:shocking::shocking:)

Managing exceptions is always problematic, and it will slow you down. If the number of exceptions is not excessive, the value of the lost sales before you go live, and amount of your dev. time may not warrant trying to manage exceptions programmatically. In this case, I would advise your customers to make 2 orders, one for the gift wrapping package (which may have a different shipping address anyway) and one for the non gift wapping books.
If that is not acceptable somewhere in the gift wrapping product description, explain that if they want some books gift wrapped and others not gift wrapped, they should note this in the notes area of the checkout process.

Justin

============
Kuroobiya - Personalized Karate Black Belts and Dogi
Kanebo From Japan - Kanebo Cosmetics Direct From Japan

24 Nov 2007, 4:25 AM
#6
possum avatar

possum

New Zenner

Join Date:
Sep 2007
Posts:
58
Plugin Contributions:
0

Re: trying to create an extra cart action

Hi Justin,

Thanks for taking the time to respond.

Actually, you were all right and I was wrong :oops:

I went back into the gift wrapping mod thread and found that swguy has made a major release change. I haven't tried to install it yet but it looks like it does everything I want.

My PHP is definitely not up to "major uncharted territory" as you put it.

And again, congrats and thanks on the edit cart mod. I'm sure you'll find a LOT of people who'll love it.

Cheers,

Possum.

24 Nov 2007, 12:57 PM
#7
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,717
Plugin Contributions:
56

Re: trying to create an extra cart action

possum:

I went back into the gift wrapping mod thread and found that swguy has made a major release change. I haven't tried to install it yet but it looks like it does everything I want.

I was never satisfied with the "attribute" based solution of Release 1 - I think you'll like Release 2.

Good luck,
swguy

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

21 Mar 2010, 6:56 PM
#8
johnofyork avatar

johnofyork

New Zenner

Join Date:
Mar 2010
Posts:
3
Plugin Contributions:
0

Re: trying to create an extra cart action

I have been trying to do something related (I think) but slightly different. In the shop I'm working on, some of the products are associated, and need to be bought as a 'bundle'. I also need the products to be removed from the cart as a bundle, so that if the shopper removes any one item of a bundle, it removes all associated items of that bundle. I believe it has to be this way, since I wish to keep stock level of the components of each 'bundle'.

I aim to achieve this by adding a custom cart action bundles.php in /includes/extra_cart_actions/ with the following effects

  1. When adding bundles to the cart, the extra cart actions will generate a unique bundleId and write this id into a text attribute (bundleId) for each product in the bundle, thereby making it possible to identify which products belong to which bundle.

  2. When removing an item from the cart, the extra cart actions will search the attributes of each product in the cart, and remove those products which have the same bundleId.

3)When modifying the quantity of an item in the cart, the extra cart actions will search the attributes of each product in the cart, and make the same quantity modification for each item with a matching bundleId attribute.

Bundles are being added to the cart perfectly well, however I am having no luck at all trying to set and get attributes of the cart contents form within the extra_cart_actions/bundles.php. My plan hinges on this, and I'm totally stumped.

I've been trying all sorts of variations on :-

$_SESSION['cart']->contents['$productId']['attributes_values']['bundleId'] = 'someId';

None of which actually seem to affect the bundleId attribute value.

Pehaps I'm barking up the wrong tree :blush:

Any ideas would be much appreciated.

John

21 Mar 2010, 8:36 PM
#9
lankeeyankee avatar

lankeeyankee

Totally Zenned

Join Date:
Jan 2007
Posts:
1,514
Plugin Contributions:
1

Re: trying to create an extra cart action

johnofyork:

I have been trying to do something related (I think) but slightly different. In the shop I'm working on, some of the products are associated, and need to be bought as a 'bundle'. I also need the products to be removed from the cart as a bundle, so that if the shopper removes any one item of a bundle, it removes all associated items of that bundle. I believe it has to be this way, since I wish to keep stock level of the components of each 'bundle'.

I aim to achieve this by adding a custom cart action bundles.php in /includes/extra_cart_actions/ with the following effects

  1. When adding bundles to the cart, the extra cart actions will generate a unique bundleId and write this id into a text attribute (bundleId) for each product in the bundle, thereby making it possible to identify which products belong to which bundle.

  2. When removing an item from the cart, the extra cart actions will search the attributes of each product in the cart, and remove those products which have the same bundleId.

3)When modifying the quantity of an item in the cart, the extra cart actions will search the attributes of each product in the cart, and make the same quantity modification for each item with a matching bundleId attribute.

Bundles are being added to the cart perfectly well, however I am having no luck at all trying to set and get attributes of the cart contents form within the extra_cart_actions/bundles.php. My plan hinges on this, and I'm totally stumped.

I've been trying all sorts of variations on :-

$_SESSION['cart']->contents['$productId']['attributes_values']['bundleId'] = 'someId';

None of which actually seem to affect the bundleId attribute value.

Pehaps I'm barking up the wrong tree :blush:

Any ideas would be much appreciated.

John

It would be helpful if you posted the bundle.php file to look at the code to see what's going on.


Zen Cart and it's community are the best!!

22 Mar 2010, 3:06 PM
#10
johnofyork avatar

johnofyork

New Zenner

Join Date:
Mar 2010
Posts:
3
Plugin Contributions:
0

Re: trying to create an extra cart action

Thank you for taking the time out to reply LankeeYankee,

My bundle.php is in a state of flux at the moment, as I try out different ways of accessing and mutating the attributes values. I seem to be having some luck using the function

$_SESSION['cart']->get_products()

as this returns an array of product details of the products in the cart. I'm still experimenting with this, and am hoping it might lead me to a way to access and also mutate the attributes within the cart.

I'll post bundle.php when it is in better shape (its terribly hacky at the moment, probably like my approach ...)

Unfortunately php isn't a langage I've used much. That in conjunction with getting to know the zen cart architecture, is having me digging holes all over the place :-) I'm sure there must be a 'right' way to access and mutate cart contents (including attributes) from within an added extra_cart_actions script, but its eluding me.

I will of course post my findings when/if I work this out, or hopefully someone will point me in the right direction.

Kind regards,

John

23 Mar 2010, 1:17 AM
#11
justinhunt avatar

justinhunt

New Zenner

Join Date:
Sep 2007
Posts:
77
Plugin Contributions:
1

Re: trying to create an extra cart action

Hi John

What you are doing sounds interesting, and I am sure you are not the only one who would like do something like this.

I have a couple of things to say about this. One is that the value of the attributes seems to get passed around in the product id field. While this was always curious to me, I have never had the need to go and see exactly how this worked , or why it was done this way. I figured it was a hash of some information that was stored elsewhere, or some clever arrangement of the attribute values in a determined sequence.

That may be why you can't see any attribute values in session variable. Try looking at the productid of a product in the cart, with attributes, and of a product without attributes, and I think you will see what I mean.

If I am right, you may have to delve into the mystery of that, to do what you want.

The second thing that I have to say is, do you need to do it this way? My first approach would be to create a product to represent each bundle, and then decrement the relevant component product's inventories once the transaction had gone through. Of course this will only work if there are no attributes on the bundled products that need the customer to consciously set. But if only the main product in the bundle has attributes, then perhaps this might be a simpler solution that also does the job?

That is my 2c worth. I am interested in seeing how you get on.

Justin

============
Kuroobiya - Personalized Karate Black Belts and Dogi
Kanebo From Japan - Kanebo Cosmetics Direct From Japan