Ubercart's Cart Links module actually gets a fair bit of use. It would've been a lot better if we'd worked it all through Workflow-ng / CA, though, instead of inventing our own scripting system in the URL. At least I think so... I'd like us to explore it at least.

What I envision is a URL that passes arguments to Rules to add products to the cart. I'm not sure yet if Rules can pull conditions directly from the URL or how we can pass arguments (like the product ID to add) to the Rule. I'm open to ideas on how the URLs should be constructed and how we should interact with Rules.

This should be explored, and this will likely require some additional actions to be written to manipulate shopping carts. Once this is enabled, a follow-up issue will be to create a display formatter for product reference fields to display a cart link for the referenced product instead of an add to cart form.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mertskeli’s picture

Sorry, I'm not a programmer. Just a user. But have a question.
Why is it so necessary for such a contrib module like Commerce to rely on other contrib modules?
I tried to download it. It requires at least 3 other modules to be downloaded. It ended up not to work at all. Entry API in its current development produces errors right after installation. And Commerce relies on it... How can we help with the development if we just are not able to run it?

rszrama’s picture

Well, it's necessary as a development philosophy but also from a development resources standpoint. It's either integrate with them or not have the features at all. And integration with Rules gets us instant integration with every other major contributed module that already integrates with Rules.

Right now everything is in flux - that's the nature of -dev branches and alpha versions. It won't last long, though, and in the past I've been bundling up working versions of the various modules. I'm not aware of any current bugs b/c I haven't updated to the modules' latest dev versions, but I've used Entity's alpha 5 with no problems at all.

mertskeli’s picture

Thank you for your reply.
Advantages of integration are obvious. On the other hand, Ubercart, for instance, tends to reduce its dependancies from other modules. Right now it can be run just with core modules. D7 core is pretty powerful itself.

Besides, some other things are not so clear. After installation, there is no such content type as "Product". It is actually present but in a strange place - in Store configuration. I don't mind creating it there, but who will see it? Admin only? How to make a page (a product page) with just "add to cart" button?
Shopping cart is actualy a pretty simple idea:
1). To create product pages.
2). To let people add them to cart by button.
3). To have all that been e-mailed and stored in a database.
All the other things are secondary. And can be extended by core modules as well (by field module or others).

Entity gives errors - get_info errors. But ok, alpha is alpha.

I do appologize if I'm wrong. Let's wait some more, until D7 is getting beta at least.

rszrama’s picture

Yeah, with Ubercart we tried to trim away the dependencies, but it didn't depend on distributions (installation profiles) as Drupal Commerce will in targeting the average newbie. That said, while Views wasn't a dependency, it was a near necessity for every site. And while Rules was unnecessary because we had Conditional Actions, one of the first things they decided when I stopped working on UC was to jettison CA in favor of Rules on Drupal 7. That means it now has no dependency advantages and will have to put up with the same alphaness. : P

Regarding the lack of a "Product" content type, that's correct - the definition of a product (i.e. through the store menus) is different from the display of a product (i.e. through a node with a product reference field. The creation of such a node type isn't facilitated through the Commerce modules themselves, but it is through the proof-of-concept installation profile I've been maintaining at:

http://github.com/rszrama/commercedev

The add to cart button comes from the product reference field - it's one of its display formatters.

mertskeli’s picture

As a developer you know better what your module should be. If it works fine it's ok. Although I've never needed neither Views nor CCK to run a site.

Absolutely no objections regarding product creation in admin area. That's a correct place, I believe. It doesn't matter what link will lead you to that process and where this link will be located. But sorry, how to display a list of products for a site visitor?

Ok, I have tried once again.
Used latest D7dev + latest Views, Rules and Entity.
1. Clean install. Stark theme. Enabled Entity CRUD API and Entity Metadata. No errors. After Rules activation - errors. See attached image1.
2. Ok, refreshed. Got everything of Drupal Commerce without major faults (just the same entity_get_info errors). See attached image2.
3. Created a test product. See attached image3.
4. After clicking on product title in products list got these beautiful lines. See attached image4.
So, how to display a normal product list with clickable "Add to cart" buttons?

In general, Drupal Commerce seems to be more logical than Ubercart, and that's why I worry so much.

mertskeli’s picture

FileSize
40.27 KB
41.02 KB
55.94 KB
95.38 KB
rszrama’s picture

The best thing will be to wait until I can get an installation profile up or a tutorial. I don't wanna derail this issue too gloriously. Also, the code you're on might be a little stale. What you really is a new node type with a product reference field. Go drop one on a node and play around with that to reference the product you've created.

mertskeli’s picture

What you really is a new node type with a product reference field.

I'd better wait for a tutorial.

rszrama’s picture

I'll hook you up soon. : D

fago’s picture

An interesting idea.. :) Pretty similar to flags, but more generic I guess. However probably this would need protection from CSRF attacks too.

Accessing path arguments with Rules would be easily possible by adding such properties to the 'system' variable via entity metadata. However I don't feel like this is a very clean solution. Perhaps just do a small module that allows assigning rule sets (or generically rule components) to paths, such that you could turn rules variables into arguments. We could even support leaving some of the parameters unconfigured, such that the remaining parameters can be configured via a form!

rszrama’s picture

I like your second plan there. Would be happy to chip in when the time comes. Damien's been doing some work lately to get our payment method forms working through Action configuration forms... so we're diving into Rules head first. : )

rszrama’s picture

Status: Active » Postponed

I'm marking this postponed, as it's hardly a priority to get 1.0 out the door... however, I do intend to revisit this, as cart links are a big part of Ubercart... just not sure the solution is going to be in Commerce or in Rules. : )

aidanlis’s picture

Is there any intermediary solution to this at the moment? It doesn't look like this can be achieved with Rules as it is now, there's no "Add product to cart" action.

I'm happy to work on this, but I'd like to know how you want to tackle it.

aidanlis’s picture

I need this module -- should I just run Coder on it and try to port it that way?

rszrama’s picture

Nah, what existed in Ubercart is insufficient for what we're doing. It should really just require a module that can execute Rules on a page load at a specific URL... but it's going to require custom work. Not that much work, mind you, but at least a menu item you use to route the page request to execute the proper Rules invoke... I haven't thought through whether or not it should execute an entire event's Rules or just map to a single one, though, and there are bound to be other unanswered questions.

aidanlis’s picture

Status: Postponed » Active

I can start playing with this, however at the moment there's no "add item to users cart" action in Rules. Is that something you want to add first?

rszrama’s picture

I'm not sure if that's necessary or not, but for simplicity's sake it probably is. Technically you should be able to create a product line item and add it to the order, but being able to do that in a single action is likely preferable. That would be the place to start, with the action requiring a product, a quantity, and perhaps optionally a user ID whose cart the product should be added to.

fago’s picture

I'd suggest making use of Rules components (-> rules_get_components()) for that, i.e. allow assigning action components to an URL. One could support mapping additional URL parts to arguments, e.g. base_path/arg1/arg2/... . -> use entity_property_verify_data_type() to ensure the data type of the argument really fits and bail out if not. For entities, just passing the id should work.

For execution, best retrieve the component manually from cache. (see rules_invoke_component()). Then check whether the data types match the parameters of the component (-> $component->parameterInfo()) and if just execute it via $component->executeByArgs($args).
You could also easily support showing an input form for any missing parameters. Rules recently got an "execute component" functionality, what shows you how you can easily do that. But probably that would be more useful for binding components as operation into a tab of a node or another entity, so you could use node/x/operation to get the component form for execution on the node.

rszrama’s picture

Oooh, thanks for the excellent tips, fago. : )

BenK’s picture

Subscribing... interesting idea to do this through Rules.

--Ben

ahimsauzi’s picture

Any progress on the interaction with Rules?

Any suggestion where to look further?

Thanks,

~Uzi

Yuri’s picture

sub

mariomc’s picture

sub

fago’s picture

rszrama’s picture

Ahh, great. Guess all we really need then is an action to add a product to the cart.

aidanlis’s picture

I am some what making progress on this, see attached images.

The rules_link allows us to attach a rule to a bundle, but I suspect this is only for generating the link - as we have no product pages this isn't really necessary. I suspect this module will need some changes too.

aidanlis’s picture

Status: Active » Needs review
FileSize
46.93 KB
1.1 KB

Here we go ... was pretty simple in the end, the hard work was modifying the rules_link module.

Here's the patch for rules_link: http://drupal.org/node/1283734

aidanlis’s picture

Note: You can set your destination like so: /purchase/1?destination=checkout or /purchase/2?destination=cart

rszrama’s picture

Status: Needs review » Needs work

It'd be good to get two additional options in there: 1) how many quantity should be added? and 2) should it combine into like product line items?

Also, let's make sure we have an action that empties the cart as well - there's an API function for that we can use.

aidanlis’s picture

The action to empty the card is already there, I'll add those extra options now. Am I using commerce_cart_product_add($order->uid, $line_item) correctly? It seems a little dodgy to provide it with the $order->uid instead of the actual $order?

rszrama’s picture

Ahh, thanks for asking. I didn't check that, but yeah, you should have it take a User account as the first argument instead of an Order. Then you can pass $account->uid to commerce_cart_product_add(). Glad there's already a cart empty action - shows what I know. : P

aidanlis’s picture

How's this looking?

eric.smith’s picture

This patch is great. It's exactly what I was looking for.

I am getting one notice error:

Notice: Trying to get property of non-object in commerce_cart_rules_add() (line 175 of [my site path]/sites/all/modules/contrib/commerce/modules/cart/commerce_cart.rules.inc).

The notice doesn't appear to cause any other problems. The checkout workflow continues as expected.

My rule is:

{ "rules_user_creates_researcher_account" : {
"LABEL" : "User creates researcher account",
"PLUGIN" : "reaction rule",
"WEIGHT" : "1",
"REQUIRES" : [ "rules", "commerce_cart" ],
"ON" : [ "user_insert" ],
"IF" : [
{ "data_is" : { "data" : [ "account:field-registration-type" ], "value" : "1" } }
],
"DO" : [
{ "commerce_cart_add" : {
"user" : [ "site:current-cart-order:owner" ],
"commerce_product" : "1",
"quantity" : "1",
"combine" : 1
}
},
{ "redirect" : { "url" : "checkout" } }
]
}
}

aidanlis’s picture

Status: Needs work » Needs review

@eric.smith I'm not sure how to fix that,

  commerce_cart_product_add($user->uid, $line_item, $combine);

During registration apparently $user is not an object, which perplexes me ... can you add in a dvm($user) on line 174? Any ideas Ryan?

aidanlis’s picture

Also Ryan could you weigh in on #1283734: Make link-types pluggable

rszrama’s picture

Issue tags: +1.1 blocker

Needs review before 1.1 methinks.

spineless’s picture

I just attempted to install the patch and I have an error.

I am using the following:

Drupal 7.8
commerce 7.x-1.x-dev
rules 7.x-2.0-rc2
rules-links 7.x.1.x-dev (downloaded today)
patch from http://drupal.org/node/1283734
commerce cart patch noted in 32 above.

I was able to patch the rules-link ok.
Here is the error i received when patching commerce module.

user@ubuntu11.4:~/sites/all/modules/commerce$ git apply -v commerce_cart_rules_addtocart.patch
Checking patch modules/cart/commerce_cart.rules.inc...
error: while searching for:
}

/**
* @}
*/

error: patch failed: modules/cart/commerce_cart.rules.inc:139
error: modules/cart/commerce_cart.rules.inc: patch does not apply

I know this is bleeding edge stuff. But I thought you would want to know.

Noemi’s picture

I installed the patch in note 32 with:
Drupal 7.8
commerce 7.x-1.0
rules 7.x-2.0-rc2

I needed an action for rules to add products to cart and it works! Thank you!

aidanlis’s picture

Assigned: Unassigned » aidanlis

The rules_link author thinks that cart links are an edge case and won't support them in his module, so I suppose I'm stuck creating a rules_betterlinks module.

aidanlis’s picture

Assigned: aidanlis » Unassigned

Actually next action is for Ryan (review patch).

aidanlis’s picture

I've written a module which is more flexible than Rules Link and not tied to entities, it's also somewhat more intuitive to use as accessing a link simply becomes an event: http://drupal.org/project/rules_linkevent

rszrama’s picture

Oooh, awesome news.

aidanlis’s picture

Any suggestion on how we can solve the anonymous user use case Ryan?

rszrama’s picture

Status: Needs review » Fixed

Alrighty, made a few modifications: changed the name of the action to commerce_cart_product_add_by_sku. Instead of having to select a product, you can now enter a product SKU in plain text (or switch to the data selector and specify the SKU of some product available to the Rule). To get around the $user bug in Rules, I'm just checking to see if $user is empty before trying to access $user->uid.

Tested this and all seems well. I was even able to take advantage of a new function, commerce_cart_product_add_by_id(). The only drawback here is that you can't easily specify the display path. I found I can mitigate this somewhat, though, by providing back from the action the line item used to add the product to the cart. This will make it easy to update other field data on the line item that is either created or updated to add the product to the cart.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/f256d1c

Kirschbaum’s picture

Sub

aidanlis’s picture

Nice one, thanks Ryan!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

spineless’s picture

Any idea when or if this patch will be included in Drupal commerce and rules links modules?

rszrama’s picture

It's all in, which is why this is marked closed (fixed). Check out the module posted by aidanlis above and look for the Add to Cart action in 7.x-1.x-dev or 7.x-1.1 once it comes out. : )

Zac_JH’s picture

@aidanlis

I've installed your module and got it working but I cannot figure out to pass the sku through the cart link. I can manually set the sku in rszrama add a product by sku - but I'd like to pass the sku by url instead of have a rule for every product!

Sorry for asking but I've looked everywhere I can think of and turned up nothing. Rules doesn't seem to be able to grab a value from the url that I can see.

Thank you for any help.