0

I created a rule that checks for the quantity of a product while adding to cart and while proceeding to the second step of checking out. The action sends a warning message to the site that says you can't have over one qty of the 'gift card' product type and is also supposed to set the quantity back to 1. The problem is that while the message appears when it is supposed to, the quantity is not being set back to 1 at any point. I made sure that I also save the cart after changing quantity as well.

Here is the rule export:

{ "rules_1_gift_card_per_cart" : {
    "LABEL" : "1 Gift Card per cart",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "rules_conditional", "commerce_cart", "commerce_rules_extra" ],
    "ON" : [ "commerce_cart_product_add", "line_item_quantity_changed" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-product:type" ], "value" : "gift_certificate" } },
      { "AND" : [
          { "data_is" : {
              "data" : [ "commerce-line-item:quantity" ],
              "op" : "\u003E",
              "value" : "1"
            }
          }
        ]
      }
    ],
    "DO" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "data_is" : {
                "data" : [ "commerce-line-item:quantity" ],
                "op" : "\u003E",
                "value" : "1"
              }
            },
            "DO" : [
              { "data_set" : { "data" : [ "commerce-line-item:quantity" ], "value" : "1" } },
              { "entity_save" : { "data" : [ "commerce-order" ] } },
              { "drupal_message" : { "message" : "Only one gift card per order.", "type" : "warning" } }
            ]
          }
        ]
      }
    ]
  }
}

EDIT: I dpm($commerce_line_item) in the action as well and it actually says the quantity is 1 while the cart display is still showing 2, though I believe this is only per item qty in the cart.

The total of the order is still being updated when the gift card quantity is being increased.

EDIT 2: I created a custom rules action to manually update the quantity in the commerce_line_item table which also does not seem to work:

function commerce_giftcards_action_change_qty_one($current_order, $current_line_item) {
  dpm('Current Order ID: ' . $current_order->order_number);
  dpm('Updated Line Item ID: ' . $current_line_item->line_item_id);
  dpm('Updated Line Item Type: ' . $current_line_item->type);

  db_update('commerce_line_item')
    ->fields(array('quantity' => '1.00'))
    ->condition('line_item_id', $current_line_item->line_item_id, '=')
    ->execute();

}

The correct fields are printing in the messages but the database is not updating unless I am going back to the product's page and adding another to the cart. It only keeps the quantity at 1 if I hit add to cart more than once. If I change the quantity of the item on the cart page it still increases the quantity in the database while also displaying the message that says only 1 qty of that item is allowed.

2 Answers 2

0

I played around with the rules actions and got it to work with these settings & the function I created to manually update the table:

    { "rules_1_gift_card_per_cart" : {
    "LABEL" : "1 Gift Card per cart",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [
      "rules",
      "rules_conditional",
      "commerce_giftcards",
      "commerce_cart",
      "commerce_rules_extra",
      "entity"
    ],
    "ON" : [
      "commerce_cart_product_add",
      "line_item_quantity_changed",
      "commerce_line_item_update"
    ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-line-item:type" ], "value" : "gift_certificate" } },
      { "AND" : [] },
      { "data_is" : {
          "data" : [ "commerce-line-item:quantity" ],
          "op" : "\u003E",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "data_is" : {
                "data" : [ "commerce-line-item:quantity" ],
                "op" : "\u003E",
                "value" : "1"
              }
            },
            "DO" : [
              { "data_set" : { "data" : [ "commerce-line-item:quantity" ], "value" : "1" } },
              { "drupal_message" : {
                  "message" : "Only one gift card per order.",
                  "type" : "warning",
                  "repeat" : 0
                }
              },
              { "commerce_giftcards_action_change_qty_one" : {
                  "order" : [ "commerce-line-item:order" ],
                  "line_item" : [ "commerce-line-item" ]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}
0

October 2022: Running Commerce Core on Drupal 7

So I struggled with this. On my website we sell digital products so the quantity never needed to be larger than one. I tried using spinners to prevent double clicking on the "Add to Cart" buttons but somehow users still found ways to double click. I still don't know how but they did. I started my solution from trying to follow the drupal commerce user guide.

https://docs.drupalcommerce.org/commerce1/user-guide/shopping-cart/working-with-cart-rules-events

The problem I ran into was that when I tried to set the condition to

commerce-line-item:quantity > 1 it tried to force me to use data selectors which doesn't give me any value option.

The only thing I could do was set

commerce-line-item:quantity = 2 and then the routine would work. But if someone triple and went really crazy with the clicks this would not work because the QTY would get to three before the rule would finish firing. I then tried adding more conditions in an OR statement but found the performance of the rule was unreliable even in non stress situations. THEN THE EUREKA MOMENT.

I didn't need any condition since everything on my website is to be set to 1. So no product type conditions, no pre-existing quantity conditions, etc... needed to be checked.

So here is what appears to be working for me. I'll come back and edit this if it fails at any point.

  1. Create new Rule
  2. Set Event to "After adding a product to the cart"
  3. Set Actions to
    1st Action: Set a Data Value commerce-line-item:quantity = 1 2nd Action: Set Save Entity commerce-line-item:order I left "Force Immediate Save to false (or unchecked).

Comments: On my website you must be logged in to make a purchase. It appears to work for regular registered users and the admin role. I'll come back and update if I have any new problems with this running. What this routine does is make sure that after the user has finally stopped clicking the "Add to Cart" button, line item quantity is set back to 1 and the cart is recalculated. I imagine it increases the "Add to Cart" time a little bit. But it should be worth it not to have calls from people that didn't realize they had overpaid because they added the product to the cart twice. I'm a bit surprised but it happens all the time. Especially on my website cause when I was on drupal 6 I used a different commerce solution and forcing a quantity of 1 was already coded in. So many customers never bothered to check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.