Page 1 of 1

[Resolved]Please help on custom pricing

Posted: Wed Dec 26, 2012 5:19 am
by pokemon007
Try to implement pricing activity for deal order as the price is based on some simple rules depending on the order info applying to all customers. I thought I can simply create an offer and an order item adjustment for the discount. But it seems not working. It throws following exception:

Code: Select all

avax.persistence.EntityNotFoundException: Unable to find org.broadleafcommerce.core.offer.domain.OrderItemAdjustmentImpl with id 351


I guess the way to create offer and adjustment may be incorrect. Here are the code snippet:

PriceDealOrderActivity

Code: Select all

public class PriceDealOrderActivity extends PriceOrderIfNecessaryActivity {
   
    @Autowired
    @Resource(name="dealOrderPricingService")
    private DealOrderPricingService dealOrderService;

    public ProcessContext execute(ProcessContext context) throws Exception {
        CartOperationRequest request = ((CartOperationContext) context).getSeedData();
        Order order = request.getOrder();
        dealOrderPricingService.createAdjustmentAndUpdateOrderPrice(order, request.isPriceOrder());
        request.setOrder(order);
       
        return context;
    }

}


DealOrderPricingService

Code: Select all

@Service("dealOrderService")
public class DealOrderPricingServiceImpl implements DealOrderService {

    @Resource(name="blOfferDao")
    private OfferDao offerDao;

    @Resource(name = "blOrderService")
    protected OrderService orderService;

    @Autowired
    @Resource(name="dealService")
    private DealService dealService;

    @Override
    @Transactional("blTransactionManager")
    public void createAdjustmentAndUpdateOrderPrice(Order order, boolean isPricingOrder) throws Exception {       
        List<OrderItem> orderItems = order.getOrderItems();
        if (orderItems != null) {
            for (OrderItem orderItem : orderItems) {
                Long discount = dealService.getDiscount(orderItem);
                if (discount != 0) {
                   List<OrderItemAdjustment> adjustments = orderItem.getOrderItemAdjustments();
                   if (adjustments == null) {
                       adjustments = new ArrayList<OrderItemAdjustment>();
                   }
                   if (adjustments.size() == 0) {
                       Offer offer = new OfferImpl();
                       offer.setName(orderItem.getDeal().getName());
                       offer.setValue(orderItem.getPrice().multiply(discount/100.0).getAmount());
                       offer.setType(OfferType.ORDER_ITEM);
                       offer.setDeliveryType(OfferDeliveryType.AUTOMATIC);
                       OrderItemAdjustment itemAdjustment = offerDao.createOrderItemAdjustment();
                       itemAdjustment.init(orderItem, offer, "Deal");
                       itemAdjustment.setRetailPriceValue(orderItem.getRetailPrice());
                       itemAdjustment.setSalesPriceValue(orderItem.getSalePrice());
                       itemAdjustment.setValue(orderItem.getPrice().multiply(discount/100.0));
                       adjustments.add(itemAdjustment);
                       orderItem.setOrderItemAdjustments(adjustments);
                   }
                   orderItem.updatePrices();
                   orderItem.assignFinalPrice();
                }
            }
        }
        order = orderService.save(order, isPricingOrder);
    }


What's wrong with the code? How to create a price adjustment? Or is there are better approach such as Dynamic Pricing?

Thank you!

-Charlie

Re: Please help on custom pricing

Posted: Thu Dec 27, 2012 5:16 am
by pokemon007
The hibernate exception has been eliminated, but I still can't get order item adjustment applied to the order item. Offer object is saved to database, but OrderItemAdjustment is not saved along with order and order item. I can't figure out why. Should I directly save OrderItemAdjustment to database alone?

Any advice is appreciated.

-Charlie

Re: [Resolved]Please help on custom pricing

Posted: Wed Jan 02, 2013 12:51 pm
by phillipuniverse
Is there any reason that you couldn't just create this offer via the admin? Why do you need to do it programmatically?

To answer your specific question, it doesn't look like you are saving the orderItem after adding the adjustment. Try calling orderItemService.merge(orderItem) after calling orderItem.setOrderItemAdjustments(adjustments).

Re: [Resolved]Please help on custom pricing

Posted: Thu Jan 03, 2013 9:40 pm
by pokemon007
The adjustment itself is based on the purchase that can't be created ahead of time. I was doing something wrong by only customizing addItem and updateQuantity workflow, not pricing workflow, so later when pricing workflow comes into play it wipe out order item adjustment saved during addItem because the pricing context is null (don't know how to set up pricing context).

At any rate, item adjustment can be saved now. Another related problem is that the total discount/promotion in order object doesn't include the item adjustment. What I want is to include all adjustments in an order so we can show it to the end customer how much they save. I can override Total activity, but doesn't solve the problem as there is no where to store this total discount unless I create another object.

Indeed, I tried to extend Order object just like extending other entity beans I did, but since OrderDaoImpl hard code the entity class to "OrderImpl.class", the usual Broadleaf entity extension won't work. I tried to override OrderDao, and redefine blOrderDao to my class, but I guess the entire Broadleaf code auto wire the OrderDao, it doesn't work. My current work around is to do some additional calculation and set the value as an additional model attribute. This is not pretty. Let me now if you have better solution.

Thank you!

-Charlie