diff --git a/alma/controllers/hook/paymentOptions.php b/alma/controllers/hook/paymentOptions.php index fe9000653..b10cf63ae 100644 --- a/alma/controllers/hook/paymentOptions.php +++ b/alma/controllers/hook/paymentOptions.php @@ -111,8 +111,10 @@ public function run($params) private function createPaymentOption($forEUComplianceModule, $ctaText, $action) { + $baseDir = _PS_MODULE_DIR_ . $this->module->name; + if ($forEUComplianceModule) { - $logo = Media::getMediaPath(_PS_MODULE_DIR_ . $this->module->name . '/views/img/alma_payment_logos.svg'); + $logo = Media::getMediaPath($baseDir . '/views/img/alma_payment_logos.svg'); $paymentOption = array( 'cta_text' => $ctaText, 'action' => $action, @@ -120,7 +122,7 @@ private function createPaymentOption($forEUComplianceModule, $ctaText, $action) ); } else { $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); - $logo = Media::getMediaPath(_PS_MODULE_DIR_ . $this->module->name . '/views/img/tiny_alma_payment_logos.svg'); + $logo = Media::getMediaPath($baseDir . '/views/img/tiny_alma_payment_logos.svg'); $paymentOption ->setModuleName($this->module->name) ->setCallToActionText($ctaText) diff --git a/alma/includes/AlmaPaymentValidation.php b/alma/includes/AlmaPaymentValidation.php index 241a117ad..8fa904b5c 100644 --- a/alma/includes/AlmaPaymentValidation.php +++ b/alma/includes/AlmaPaymentValidation.php @@ -194,9 +194,11 @@ public function validatePayment($almaPaymentId) "merchant_reference" => $order->reference, )); } catch (RequestError $e) { - AlmaLogger::instance()->error("[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}"); + $msg = "[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}"; + AlmaLogger::instance()->error($msg); } catch (PrestaShopException $e) { - AlmaLogger::instance()->error("[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}"); + $msg = "[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}"; + AlmaLogger::instance()->error($msg); } $extraRedirectArgs = ''; diff --git a/alma/includes/CartData.php b/alma/includes/CartData.php index d2bb4cd36..a3e64b9c8 100644 --- a/alma/includes/CartData.php +++ b/alma/includes/CartData.php @@ -39,7 +39,8 @@ class CartData * @throws PrestaShopDatabaseException * @throws PrestaShopException */ - public static function cartInfo($cart) { + public static function cartInfo($cart) + { return array( "items" => self::getCartItems($cart), "discounts" => self::getCartDiscounts($cart), @@ -50,7 +51,8 @@ public static function cartInfo($cart) { * @param Cart $cart * @return bool|mixed */ - private static function includeTaxes($cart) { + private static function includeTaxes($cart) + { if (version_compare(_PS_VERSION_, '1.7', '>=')) { $taxConfiguration = new TaxConfiguration(); return $taxConfiguration->includeTaxes(); @@ -74,7 +76,8 @@ private static function includeTaxes($cart) { * @throws PrestaShopDatabaseException * @throws PrestaShopException */ - private static function getCartItems($cart) { + private static function getCartItems($cart) + { $items = array(); $summaryDetails = $cart->getSummaryDetails($cart->id_lang, true); @@ -82,7 +85,7 @@ private static function getCartItems($cart) { $productsDetails = self::getProductsDetails($products); $combinationsNames = self::getProductsCombinations($cart, $products); - foreach ($products as $idx => $productRow) { + foreach ($products as $productRow) { $product = new Product(null, false, $cart->id_lang); $product->hydrate($productRow); @@ -94,15 +97,36 @@ private static function getCartItems($cart) { $manufacturer_name = $productsDetails[$pid]['manufacturer_name']; } + $unitPrice = self::includeTaxes($cart) ? (float)$productRow['price_wt'] : (float)$productRow['price']; + $linePrice = self::includeTaxes($cart) ? (float)$productRow['total_wt'] : (float)$productRow['total']; + + if (isset($productRow['gift'])) { + $isGift = (bool)$productRow['gift']; + } else { + $isGift = isset($productRow['is_gift']) ? (bool)$productRow['is_gift'] : null; + } + + $pictureUrl = $link->getImageLink( + $productRow['link_rewrite'], + $productRow['id_image'], + self::getFormattedImageTypeName('large') + ); + + if (isset($productRow['is_virtual'])) { + $requiresShipping = !(bool)($productRow['is_virtual']); + } else { + $requiresShipping = !(bool)($productsDetails[$pid]['is_virtual']); + } + $data = array( 'sku' => $productRow['reference'], 'vendor' => $manufacturer_name, 'title' => $productRow['name'], 'variant_title' => null, 'quantity' => (int)$productRow['cart_quantity'], - 'unit_price' => almaPriceToCents(self::includeTaxes($cart) ? (float)$productRow['price_wt'] : (float)$productRow['price']), - 'line_price' => almaPriceToCents(self::includeTaxes($cart) ? (float)$productRow['total_wt'] : (float)$productRow['total']), - 'is_gift' => isset($productRow['gift']) ? (bool)$productRow['gift'] : (isset($productRow['is_gift']) ? (bool)$productRow['is_gift'] : null), + 'unit_price' => almaPriceToCents($unitPrice), + 'line_price' => almaPriceToCents($linePrice), + 'is_gift' => $isGift, 'categories' => array($productRow['category']), 'url' => $link->getProductLink( $product, @@ -116,8 +140,8 @@ private static function getCartItems($cart) { false, true ), - 'picture_url' => $link->getImageLink($productRow['link_rewrite'], $productRow['id_image'], self::getFormattedImageTypeName('large')), - 'requires_shipping' => !(bool)(isset($productRow['is_virtual']) ? $productRow['is_virtual'] : $productsDetails[$pid]['is_virtual']), + 'picture_url' => $pictureUrl, + 'requires_shipping' => $requiresShipping, 'taxes_included' => self::includeTaxes($cart), ); @@ -135,7 +159,8 @@ private static function getCartItems($cart) { return $items; } - private static function getFormattedImageTypeName($name) { + private static function getFormattedImageTypeName($name) + { if (version_compare(_PS_VERSION_, '1.7', '>=')) { return ImageType::getFormattedName($name); } else { @@ -147,14 +172,15 @@ private static function getFormattedImageTypeName($name) { * @param array $products * @return array */ - private static function getProductsDetails($products) { + private static function getProductsDetails($products) + { $sql = new DbQuery(); $sql->select('p.`id_product`, p.`is_virtual`, m.`name` as manufacturer_name'); $sql->from('product', 'p'); $sql->innerJoin('manufacturer', 'm', 'm.`id_manufacturer` = p.`id_manufacturer`'); $in = array(); - foreach ($products as $idx => $productRow) { + foreach ($products as $productRow) { $in[] = $productRow['id_product']; } @@ -182,19 +208,34 @@ private static function getProductsDetails($products) { * @param array $products * @return array */ - private static function getProductsCombinations($cart, $products) { + private static function getProductsCombinations($cart, $products) + { $sql = new DbQuery(); $sql->select('CONCAT(p.`id_product`, "-", pa.`id_product_attribute`) as `unique_id`'); $combinationName = new DbQuery(); $combinationName->select('GROUP_CONCAT(DISTINCT CONCAT(agl.`name`, " - ", al.`name`) SEPARATOR ", ")'); $combinationName->from('product_attribute', 'pa2'); - $combinationName->innerJoin('product_attribute_combination', 'pac', 'pac.`id_product_attribute` = pa2.`id_product_attribute`'); + $combinationName->innerJoin( + 'product_attribute_combination', + 'pac', + 'pac.`id_product_attribute` = pa2.`id_product_attribute`' + ); $combinationName->innerJoin('attribute', 'a', 'a.`id_attribute` = pac.`id_attribute`'); - $combinationName->innerJoin('attribute_lang', 'al', 'a.id_attribute = al.id_attribute AND al.`id_lang` = ' . $cart->id_lang); + $combinationName->innerJoin( + 'attribute_lang', + 'al', + 'a.id_attribute = al.id_attribute AND al.`id_lang` = ' . $cart->id_lang + ); $combinationName->innerJoin('attribute_group', 'ag', 'ag.`id_attribute_group` = a.`id_attribute_group`'); - $combinationName->innerJoin('attribute_group_lang', 'agl', 'ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . $cart->id_lang); - $combinationName->where('pa2.`id_product` = p.`id_product` AND pa2.`id_product_attribute` = pa.`id_product_attribute`'); + $combinationName->innerJoin( + 'attribute_group_lang', + 'agl', + 'ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . $cart->id_lang + ); + $combinationName->where( + 'pa2.`id_product` = p.`id_product` AND pa2.`id_product_attribute` = pa.`id_product_attribute`' + ); /** @noinspection PhpUnhandledExceptionInspection */ $sql->select("({$combinationName->build()}) as combination_name"); @@ -205,12 +246,13 @@ private static function getProductsCombinations($cart, $products) { // DbQuery::where joins all where clauses with `) AND (` so for ORs we need a fully built where condition $where = ''; $op = ''; - foreach ($products as $idx => $productRow) { + foreach ($products as $productRow) { if (!isset($productRow['id_product_attribute']) || !(int)$productRow['id_product_attribute']) { continue; } - $where .= "{$op}(p.`id_product` = {$productRow['id_product']} AND pa.`id_product_attribute` = {$productRow['id_product_attribute']})"; + $where .= "{$op}(p.`id_product` = {$productRow['id_product']}"; + $where .= " AND pa.`id_product_attribute` = {$productRow['id_product_attribute']})"; $op = ' OR '; } $sql->where($where); @@ -235,14 +277,16 @@ private static function getProductsCombinations($cart, $products) { * @param Cart $cart * @return array of discount items */ - private static function getCartDiscounts($cart) { + private static function getCartDiscounts($cart) + { $discounts = array(); $cartRules = $cart->getCartRules(CartRule::FILTER_ACTION_ALL, false); foreach ($cartRules as $cartRule) { + $amount = self::includeTaxes($cart) ? (float)$cartRule["value_real"] : (float)$cartRule["value_tax_exc"]; $discounts[] = array( "title" => isset($cartRule["name"]) ? $cartRule["name"] : $cartRule["description"], - "amount" => almaPriceToCents(self::includeTaxes($cart) ? (float)$cartRule["value_real"] : (float)$cartRule["value_tax_exc"]) + "amount" => almaPriceToCents($amount) ); } diff --git a/alma/includes/ShippingData.php b/alma/includes/ShippingData.php index 18d8180dd..bfb36ad07 100644 --- a/alma/includes/ShippingData.php +++ b/alma/includes/ShippingData.php @@ -35,7 +35,8 @@ class ShippingData * @param Cart $cart * @return array|null */ - public static function shippingInfo($cart) { + public static function shippingInfo($cart) + { $addressId = $cart->id_address_delivery; $deliveryOption = $cart->getDeliveryOption(null, true); @@ -66,7 +67,7 @@ public static function shippingInfo($cart) { $shippingInfo["available_options"] = array(); $knownOptions = array(); - foreach($deliveryOptionList[$addressId] as $key => $carriers) { + foreach ($deliveryOptionList[$addressId] as $carriers) { foreach ($carriers['carrier_list'] as $id => $carrierOptionInfo) { $carrierOption = new Carrier($id, $cart->id_lang); if (!$carrierOption) { @@ -97,7 +98,8 @@ public static function shippingInfo($cart) { * @param $carrierInfo * @return array */ - private static function shippingInfoData($carrier, $carrierInfo) { + private static function shippingInfoData($carrier, $carrierInfo) + { return array( "amount" => almaPriceToCents((float)$carrierInfo["price_with_tax"]), "carrier" => $carrier->name, @@ -107,11 +109,13 @@ private static function shippingInfoData($carrier, $carrierInfo) { ); } - private static function isExpressShipping($carrierInfo) { + private static function isExpressShipping($carrierInfo) + { return null; } - private static function isPickupShipping($carrierInfo) { + private static function isPickupShipping($carrierInfo) + { return null; } }