Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion Controller/LoyaltyRewards/ApplyPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ public function execute()
]);
}
}
if ($rewardData["rule"] === "flexible_points_reward") {
$quote = $this->helper->getQuote();
$subTotal = $quote->getSubtotal();
$conversion_rate = $rewardData['conversion_rate'];
$redeemPoints = (int) $this->request->getPost('redeem_points');
if ( $redeemPoints >= $rewardData['minimum_redemption_limit'] ) {
$redeemPoints = min($subTotal*$conversion_rate, max($rewardData['minimum_redemption_limit'], min($redeemPoints, $rewardData['maximum_redemption_limit'])));
}
else {
$this->unsetRewardRules($zinreloQuote);
$this->messageManager->addError(__("Please enter the correct number of points to be redeemed."));
return $resultJson->setData([
'success' => false
]);
}
$rewardData["points_to_be_redeemed"] = $redeemPoints;
$rewardRules[$redeemReward]['points_to_be_redeemed'] = $redeemPoints;
$zinreloQuote->setRewardRulesData($this->helper->jsonSerialize($rewardRules));
$zinreloQuote->save();
}
$response = $this->getApiResponse($rewardData);

if ($response["success"] && !empty($response["result"]["data"])) {
Expand Down Expand Up @@ -227,7 +247,7 @@ public function getApiResponse($rewardData)
public function getParamsData($rewardData)
{
$customerEmail = $this->customerSession->getCustomer()->getEmail();
return [
$params = [
"member_id" => $customerEmail,
"reward_id" => $rewardData["reward_id"],
"transaction_attributes" => [
Expand All @@ -239,6 +259,12 @@ public function getParamsData($rewardData)
],
"status" => "pending"
];

if ($rewardData["rule"] === "flexible_points_reward" && isset($rewardData["points_to_be_redeemed"])) {
$params["points_to_be_redeemed"] = (int)$rewardData["points_to_be_redeemed"];
}

return $params;
}

/**
Expand Down
21 changes: 19 additions & 2 deletions Controller/LoyaltyRewards/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Firebase\JWT\JWT;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\SessionFactory;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\App\Action\HttpPostActionInterface;
Expand All @@ -29,6 +30,10 @@ class Index implements HttpPostActionInterface
* @var CustomerFactory
*/
private $customerFactory;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var SerializerInterface
*/
Expand Down Expand Up @@ -61,6 +66,7 @@ class Index implements HttpPostActionInterface
* @param CountryFactory $countryFactory
* @param Resolver $store
* @param Data $helper
* @param CustomerRepositoryInterface $customerRepository
*/
public function __construct(
ResultFactory $resultFactory,
Expand All @@ -70,7 +76,8 @@ public function __construct(
SessionFactory $sessionFactory,
CountryFactory $countryFactory,
Resolver $store,
Data $helper
Data $helper,
CustomerRepositoryInterface $customerRepository
) {
$this->resultFactory = $resultFactory;
$this->jwt = $jwt;
Expand All @@ -80,6 +87,7 @@ public function __construct(
$this->countryFactory = $countryFactory;
$this->store = $store;
$this->helper = $helper;
$this->customerRepository = $customerRepository;
}

/**
Expand Down Expand Up @@ -108,6 +116,7 @@ public function execute()
$lang = "";
$isSetCookies = false;

$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$jsonConfigLanguage = $this->helper->getConfigLanguage();
if ($jsonConfigLanguage) {
$lang = $this->store->getLocale() ?? "";
Expand All @@ -120,6 +129,15 @@ public function execute()
}
}
$customerId = $this->sessionFactory->create()->getCustomerId();
if ($customerId && !$this->helper->isAutoEnrollmentEnabled() ) {
$customerData = $this->customerRepository->getById($customerId);
$opt_in_attribute = $this->helper->getOptInAttributeCode();
$zinreloOptedIn = $customerData->getCustomAttribute($opt_in_attribute);
if (!$zinreloOptedIn || !$zinreloOptedIn->getValue()) {
return $resultJson->setData([]);
}
}

$customer = $this->customerFactory->create()->load($customerId);
if ($customer->getEntityId()) {
$billingAddress = $customer->getDefaultBillingAddress() ?
Expand Down Expand Up @@ -157,7 +175,6 @@ public function execute()
];

$data = $this->jwt->encode($payload, $key, 'HS256');
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
if ($customerEmail) {
$this->helper->setCookie($data);
$isSetCookies = true;
Expand Down
91 changes: 89 additions & 2 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ class Config extends AbstractHelper
public const XML_PATH_REWARDS_POINTS_AT_PDP = "zinrelo_loyaltyRewards/settings/product_page_rewards_point_enable";
public const XML_PATH_FREE_SHIPPING_LABEL = "zinrelo_loyaltyRewards/settings/free_shipping_label";
public const XML_PATH_PRODUCT_PAGE_REWARD_LABEL = "zinrelo_loyaltyRewards/settings/product_page_reward_label";
public const XML_PATH_PDP_CART_PAGE = "zinrelo_loyaltyRewards/settings/cart_page_rewards_point_enable";
public const XML_PATH_PDP_CART_PAGE_REWARD_LABEL = "zinrelo_loyaltyRewards/settings/cart_page_reward_label";
public const XML_PATH_CART_PAGE_REWARD_DROPDOWN_LABEL =
"zinrelo_loyaltyRewards/settings/cart_page_reward_dropdown_label";
public const XML_PATH_LANGUAGES = 'zinrelo_loyaltyRewards/settings/languages_mapping';
public const XML_PATH_AUTO_ENROLLMENT = 'zinrelo_loyaltyRewards/settings/auto_enrollment';
public const XML_PATH_OPT_IN_FIELD_NAME = 'zinrelo_loyaltyRewards/settings/opt_in_field_name';

/**
* Cookie life time
*/
Expand Down Expand Up @@ -490,7 +495,39 @@ public function isRewardDropDownEnable()
*/
public function isModuleEnabled()
{
return $this->getConfig(self::XML_PATH_LOYALTY_REWARDS_ACTIVE) ? true : false;
if($this->getConfig(self::XML_PATH_LOYALTY_REWARDS_ACTIVE)) {
if(!$this->isAutoEnrollmentEnabled()) {
$customerId = $this->customerSession->create()->getCustomerId();
if ($customerId) {
$zinreloOptedIn = $this->getOptInCustomAttributeValue($customerId);
if (!$zinreloOptedIn) {
return false;
}
}
}
return true;
}
return false;
}

/**
* Check auto enrollment is enabled or disabled
*
* @return bool
*/
public function isAutoEnrollmentEnabled()
{
return $this->getConfig(self::XML_PATH_AUTO_ENROLLMENT) ? true : false;
}

/**
* Get Opt In Field Name
*
* @return mixed
*/
public function getOptInAttributeCode()
{
return $this->getConfig(self::XML_PATH_OPT_IN_FIELD_NAME) ?? '';
}

/**
Expand Down Expand Up @@ -529,6 +566,32 @@ public function getRewardLabelAtProductPage(): string
return $this->getConfig(self::XML_PATH_PRODUCT_PAGE_REWARD_LABEL);
}

/**
* Check Reward Points can show at Cart Page
*
* @return bool
*/
public function isPDPAtCartEnabled()
{
$isModuleEnable = $this->isModuleEnabled();
$isRewardPointAtCartEnabled = $this->getConfig(self::XML_PATH_PDP_CART_PAGE) ?? false;
$isDashboardHiddenForGuests = $this->isDashboardHiddenForGuests();
if($isDashboardHiddenForGuests && !$this->getCustomerEmailBySession()) {
return false;
}
return ($isRewardPointAtCartEnabled && $isModuleEnable) ?? false;
}

/**
* Get Label for show at Cart Pages
*
* @return string
*/
public function getPDPLabelAtCartPage(): string
{
return $this->getConfig(self::XML_PATH_PDP_CART_PAGE_REWARD_LABEL);
}

/**
* Get Label for show at cart page reward list dropdown section
*
Expand Down Expand Up @@ -650,6 +713,12 @@ public function getRedeemRules()
"id" => '',
"reward_name" => $rule["reward_name"],
"reward_value" => !empty($rule["reward_value"]) ? $rule["reward_value"] : "",
"maximum_redemption_limit" => !empty($rule["extra_parameters"]["maximum_redemption_limit"])
? $rule["extra_parameters"]["maximum_redemption_limit"] : $availablePoint,
"minimum_redemption_limit" => !empty($rule["extra_parameters"]["minimum_redemption_limit"])
? $rule["extra_parameters"]["minimum_redemption_limit"] : "",
"conversion_rate" => !empty($rule["extra_parameters"]["conversion_rate"])
? $rule["extra_parameters"]["conversion_rate"] : "",
"product_id" => isset($rule["product_id"]) ? $rule["product_id"] : ""
];
}
Expand All @@ -670,6 +739,23 @@ public function getCustomerEmailBySession()
return $this->customerSession->create()->getCustomer()->getEmail() ?? '';
}

/**
* Get Customer Custom Attribute Value
*
* @return mixed
*/
public function getOptInCustomAttributeValue($customerId)
{
$customerData = $this->customerRepository->getById($customerId);
$opt_in_attribute = $this->getOptInAttributeCode();
$zinreloOptedIn = $customerData->getCustomAttribute($opt_in_attribute);
if ($zinreloOptedIn) {
return $zinreloOptedIn->getValue();
}
return null;
}


/**
* Get Free Product
*
Expand Down Expand Up @@ -816,7 +902,8 @@ public function getDefaultRewardTypes(): array
"product_redemption" => "Product Redemption",
"fixed_amount_discount" => "Fixed Amount Discount",
"percentage_discount" => "Percentage Discount",
"free_shipping" => "Free Shipping"
"free_shipping" => "Free Shipping",
"flexible_points_reward" => "Flexible Points Reward"
];
}

Expand Down
13 changes: 11 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ public function getRedeemRewardDiscountData($orderId)
$discountLabel = "";
if (isset($rewardData['rule'])
&& ($rewardData['rule'] == 'fixed_amount_discount'
|| $rewardData['rule'] == 'percentage_discount')) {
|| $rewardData['rule'] == 'percentage_discount'
|| $rewardData['rule'] == 'flexible_points_reward')) {
$totalAmount = $order->getSubtotal();
if ($rewardData['rule'] == 'fixed_amount_discount') {
$discountAmount = -$rewardData['reward_value'];
} else if ($rewardData['rule'] == 'flexible_points_reward') {
$discountAmount = -($rewardData['points_to_be_redeemed'] / $rewardData['conversion_rate']);
} else {
$discountAmount = -$totalAmount * $rewardData['reward_value'] / 100;
}
Expand Down Expand Up @@ -200,6 +203,9 @@ public function createZinreloOrder($id, $replacedOrderId)
$rewardData = $this->getRewardRulesData($quote, $redeemReward);
if ($rewardData) {
$discountValue = $rewardData['reward_value'];
if ($rewardData['rule'] == 'flexible_points_reward') {
$discountValue = ($rewardData['points_to_be_redeemed'] / $rewardData['conversion_rate']);
}
if ($rewardData['rule'] == 'percentage_discount') {
$discountAmount = $item->getDiscountAmount() + (($item->getPrice() * $item->getQtyOrdered()) * $discountValue / 100);
$baseDiscountAmount = $item->getBaseDiscountAmount() + (($item->getBasePrice() * $item->getQtyOrdered()) * $discountValue / 100);
Expand All @@ -210,7 +216,10 @@ public function createZinreloOrder($id, $replacedOrderId)
$item->save();
$totalDiscountAmount += $discountAmount;
$totalBaseDiscountAmount += $baseDiscountAmount;
} elseif ($rewardData['rule'] == 'fixed_amount_discount') {
} elseif (
$rewardData['rule'] == 'fixed_amount_discount' ||
$rewardData['rule'] == 'flexible_points_reward'
) {
$OrderTotal = $quote->getSubtotal();
$OrderBaseTotal = $quote->getBaseSubtotal();
$totalPercentage = ($item->getPrice() * $item->getQtyOrdered()) / $OrderTotal;
Expand Down
24 changes: 12 additions & 12 deletions Helper/OrderComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ public function getCompletedOrder($order)
$orderData['payment'] = $this->helper->setFormatedPrice($orderData['payment']);
$replacedOrderId = $this->helper->getReplacedOrderID($order->getEntityId());
unset($orderData['items']);
foreach ($order->getItems() as $item) {
$totalDiscountAmount = 0;
$totalBaseDiscountAmount = 0;
foreach ($order->getAllItems() as $item) {
if ($item->getParentItemId()) {
continue;
}
$discAmount[$item->getSku()] = (int)$item->getDiscountAmount();
$totalDiscountAmount += $discAmount[$item->getSku()];

$discBaseAmount[$item->getSku()] = (int)$item->getBaseDiscountAmount();
$totalBaseDiscountAmount += $discBaseAmount[$item->getSku()];
$totalDiscountAmount = 0;
$totalBaseDiscountAmount = 0;
foreach ($order->getAllItems() as $item) {
if ($item->getParentItemId()) {
continue;
}
$discAmount[$item->getSku()] = (int)$item->getDiscountAmount();
$totalDiscountAmount += $discAmount[$item->getSku()];

$discBaseAmount[$item->getSku()] = (int)$item->getBaseDiscountAmount();
$totalBaseDiscountAmount += $discBaseAmount[$item->getSku()];
}
foreach ($order->getItems() as $item) {
unset($item['product']);
$orderItemData = $item->debug();
$orderItemData['qty_ordered'] = (int)$orderItemData['qty_ordered'];
Expand Down
5 changes: 4 additions & 1 deletion Helper/Reward.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public function getCollectRewardValueData($orderId, $flag, $actionData)
$rewardData = $this->getRewardRulesData($quote, $redeemReward);
if (isset($rewardData['rule'])
&& ($rewardData['rule'] == 'fixed_amount_discount'
|| $rewardData['rule'] == 'percentage_discount')) {
|| $rewardData['rule'] == 'percentage_discount'
|| $rewardData['rule'] == 'flexible_points_reward')) {

$totalAmount = $order->getSubtotal();
if ($rewardData['rule'] == 'fixed_amount_discount') {
$totalAmount = $rewardData['reward_value'];
} else if ($rewardData['rule'] == 'flexible_points_reward') {
$totalAmount = ($rewardData['points_to_be_redeemed'] / $rewardData['conversion_rate']);
} else {
$totalAmount = $totalAmount * $rewardData['reward_value'] / 100;
}
Expand Down
9 changes: 5 additions & 4 deletions Model/Total/CreditMemo/LoyaltyRewardsDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public function __construct(
*/
public function collect(Creditmemo $creditMemo)
{
if ($creditMemo->getTotalQty() > 0) {
$orderId = $creditMemo->getOrderId();
return $this->helper->getCollectRewardValueData($orderId, 'creditmemeo', $creditMemo);
}
// if ($creditMemo->getTotalQty() > 0) {
// $orderId = $creditMemo->getOrderId();
// return $this->helper->getCollectRewardValueData($orderId, 'creditmemeo', $creditMemo);
// }
return $this;
}
}
11 changes: 6 additions & 5 deletions Model/Total/Invoice/LoyaltyRewardsDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public function __construct(
*/
public function collect(Invoice $invoice)
{
if (!$invoice->getOrderId()) {
return $this;
}
$orderId = $invoice->getOrderId();
return $this->helper->getCollectRewardValueData($orderId, 'invoice', $invoice);
// if (!$invoice->getOrderId()) {
// return $this;
// }
// $orderId = $invoice->getOrderId();
// return $this->helper->getCollectRewardValueData($orderId, 'invoice', $invoice);
return $this;
}
}
Loading