Step 9: Remove items with voice

We can also allow users to remove items from the cart with voice.

  1. To the dialog script, add a new voice command. This command is similar to the command for adding items, except for sending a negative number with the updateOrder command.

    Dialog script
    intent(`Remove $(NUM: 1, 2, 3...) $(ITEM: ${itemList}), please-`, p => {
        p.play(`Removing ${p.ITEM.value}`);
        let number = p.NUM ? p.NUM.value : 1;
        p.play({command: 'updateOrder', item: p.ITEM.value, quantity: -number});
    });
    
  2. In the app, update the changeOrder() function to the following:

    HTML file
    function changeOrder(item, quantity) {
        let number = (order[item] ? order[item] : 0) + quantity;
        // Removing the item or updating the item quantity
        if (number <= 0) {
            delete order[item];
        } else {
            order[item] = number;
        }
        updateCart();
    }
    

Now, when the number of items is less or equal to 0, the item is removed from the order; otherwise the number for the named position is updated.

In the app, click the Alan AI button and try adding and removing items from the cart with voice.