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.

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" ]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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