Esempio n. 1
0
 /** Coupon discounts implementation. */
 protected static function byCoupons(&$order, $contact, $apply)
 {
     $currency = isset($order['currency']) ? $order['currency'] : wa('shop')->getConfig()->getCurrency(false);
     $checkout_data = wa('shop')->getStorage()->read('shop/checkout');
     if (empty($checkout_data['coupon_code'])) {
         return 0;
         // !!! Will this fail when recalculating existing order?
     }
     $cm = new shopCouponModel();
     $coupon = $cm->getByField('code', $checkout_data['coupon_code']);
     if (!$coupon || !shopCouponsAction::isEnabled($coupon)) {
         return 0;
     }
     switch ($coupon['type']) {
         case '$FS':
             $order['shipping'] = 0;
             $result = 0;
             break;
         case '%':
             $result = max(0.0, min(100.0, (double) $coupon['value'])) * $order['total'] / 100.0;
             break;
         default:
             // Flat value in currency
             $result = max(0.0, (double) $coupon['value']);
             if ($currency != $coupon['type']) {
                 $crm = new shopCurrencyModel();
                 $result = (double) $crm->convert($result, $coupon['type'], $currency);
             }
             break;
     }
     if ($apply) {
         $cm->useOne($coupon['id']);
         if (empty($order['params'])) {
             $order['params'] = array();
         }
         $order['params']['coupon_id'] = $coupon['id'];
         $order['params']['coupon_discount'] = $result;
     }
     return $result;
 }