aa

aa

aa

aa

5a474b31beb00ef7f878e12bb7

994406ba475deac533d37df332

/**

 * Adds a bonus product to the cart.

 *

 * Parses the httpParameterMap and adds the bonus products in it to an array.

 *

 * Gets the bonus discount line item. In a transaction, removes the bonus discount line item. For each bonus product in the array,

 * gets the product based on the product ID and adds the product as a bonus product to the cart.

 *

 * If the product is a bundle, updates the product option selections for each child product, finds the line item,

 * and replaces it with the current child product and selections.

 *

 * If the product and line item can be retrieved, recalculates the cart, commits the transaction, and renders a JSON object indicating success.

 * If the transaction fails, rolls back the transaction and renders a JSON object indicating failure.

 */

function addBonusProductJson() {

    var h, i, j, cart, data, productsJSON, bonusDiscountLineItem, product, lineItem, childPids, childProduct, foundLineItem, Product;

    cart = app.getModel('Cart').goc();

    Product = app.getModel('Product');


    // parse bonus product JSON

    data = JSON.parse(request.httpParameterMap.getRequestBodyAsString());

    productsJSON = new ArrayList();


    for (h = 0; h < data.bonusproducts.length; h += 1) {

        // add bonus product at index zero (front of the array) each time

        productsJSON.addAt(0, data.bonusproducts[h].product);

    }


    bonusDiscountLineItem = cart.getBonusDiscountLineItemByUUID(request.httpParameterMap.bonusDiscountLineItemUUID.stringValue);


    Transaction.begin();

    cart.removeBonusDiscountLineItemProducts(bonusDiscountLineItem);


    for (i = 0; i < productsJSON.length; i += 1) {


        product = Product.get(productsJSON[i].pid).object;

        lineItem = cart.addBonusProduct(bonusDiscountLineItem, product, new ArrayList(productsJSON[i].options), parseInt(productsJSON[i].qty));


        if (lineItem && product) {

            if (product.isBundle()) {


                childPids = productsJSON[i].childPids.split(',');


                for (j = 0; j < childPids.length; j += 1) {

                    childProduct = Product.get(childPids[j]).object;


                    if (childProduct) {




                        var UpdateProductOptionSelections = require('app_storefront_core/cartridge/scripts/cart/UpdateProductOptionSelections');

                        UpdateProductOptionSelections.update({

                            SelectedOptions: new ArrayList(productsJSON[i].options),

                            Product: childProduct

                        });


                        foundLineItem = cart.getBundledProductLineItemByPID(lineItem.getBundledProductLineItems(),

                            (childProduct.isVariant() ? childProduct.masterProduct.ID : childProduct.ID));


                        if (foundLineItem) {

                            foundLineItem.replaceProduct(childProduct);

                        }

                    }

                }

            }

        } else {

            Transaction.rollback();


            let r = require('~/cartridge/scripts/util/Response');

            r.renderJSON({

                success: false

            });

            return;

        }

    }


    cart.calculate();

    Transaction.commit();


    let r = require('~/cartridge/scripts/util/Response');

    r.renderJSON({

        success: true

    });

}


Report Page