Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLUGIN1CC-4024] Magic buy now logic update based on config #514

Open
wants to merge 2 commits into
base: magic_integration_latest
Choose a base branch
from
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
20 changes: 12 additions & 8 deletions Controller/OneClick/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ public function execute()
} else {
/** @var QuoteBuilder $quoteBuilder */
$quoteBuilder = $this->quoteBuilderFactory->create();
$quote = $quoteBuilder->createQuote();
$buyNowCartPushAction = $this->config->getBuyNowAction();
if ($buyNowCartPushAction === '1') {
$quote = $quoteBuilder->createOrUpdateQuote();
} else {
$quote = $quoteBuilder->createQuote();
}
$quoteId = $quote->getId();
$quote = $this->quoteFactory->create()->load($quoteId);
$totals = $quote->getTotals();
Expand Down Expand Up @@ -231,10 +236,10 @@ public function execute()
$productImageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
}

$imagewidth=200;
$imageheight=200;
$imagewidth = 200;
$imageheight = 200;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');
$productImageUrl = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl();

$productUrl = $product->getProductUrl();
Expand All @@ -249,7 +254,7 @@ public function execute()
}

$categoriesIds = $product->getCategoryIds(); /*will return category ids array*/
foreach($categoriesIds as $categoryId){
foreach ($categoriesIds as $categoryId) {

$cat = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$catName = $cat->getName();
Expand Down Expand Up @@ -316,8 +321,7 @@ public function execute()
];
$customerEmail = $this->getCustomerEmailFromQuote();

if($customerEmail !== false)
{
if ($customerEmail !== false) {
$customerEmailNotes = [
'website_logged_in_email' => $customerEmail
];
Expand All @@ -337,7 +341,7 @@ public function execute()

if (null !== $razorpay_order && !empty($razorpay_order->id)) {
$this->logger->info('graphQL: Razorpay Order ID: ' . $razorpay_order->id);
$catalogRzpKey = static::QUOTE_LINKED_RAZORPAY_ORDER_ID.'_'.$maskedId;
$catalogRzpKey = static::QUOTE_LINKED_RAZORPAY_ORDER_ID . '_' . $maskedId;
$this->logger->info('graphQL: Razorpay Order ID stored catalogKey: ' . $catalogRzpKey);

$this->checkoutSession->setData($catalogRzpKey, $razorpay_order->id);
Expand Down
6 changes: 6 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Config
const KEY_PRIVATE_KEY = 'key_secret';
const KEY_MERCHANT_NAME_OVERRIDE = 'merchant_name_override';
const KEY_PAYMENT_ACTION = 'rzp_payment_action';
const KEY_BUY_NOW_CART_PUSH = 'magic_buy_now_cart_push';
const KEY_AUTO_INVOICE = 'auto_invoice';
const KEY_NEW_ORDER_STATUS = 'order_status';
const ENABLE_WEBHOOK = 'enable_webhook';
Expand Down Expand Up @@ -122,6 +123,11 @@ public function getPaymentAction()
return $this->getConfigData(self::KEY_PAYMENT_ACTION);
}

public function getBuyNowAction()
{
return $this->getConfigData(self::KEY_BUY_NOW_CART_PUSH);
}

public function getNewOrderStatus()
{
return $this->getConfigData(self::KEY_NEW_ORDER_STATUS);
Expand Down
48 changes: 44 additions & 4 deletions Model/QuoteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Customer\Model\Session;
use Razorpay\Magento\Model\QuoteBuilder\ItemBuilderFactory;
use Razorpay\Magento\Model\QuoteBuilder\ItemBuilder;
use Magento\Checkout\Model\Session as CheckoutSession;

class QuoteBuilder
{
Expand All @@ -29,6 +30,7 @@ class QuoteBuilder
* @var ItemBuilderFactory
*/
protected $itemBuilderFactory;
protected $checkoutSession;

/**
* QuoteBuilder constructor.
Expand All @@ -38,15 +40,18 @@ class QuoteBuilder
* @param ItemBuilderFactory $itemBuilderFactory
*/
public function __construct(
QuoteFactory $quoteFactory,
QuoteFactory $quoteFactory,
StoreManagerInterface $storeManager,
Session $session,
ItemBuilderFactory $itemBuilderFactory
) {
Session $session,
CheckoutSession $checkoutSession,
ItemBuilderFactory $itemBuilderFactory
)
{
$this->quoteFactory = $quoteFactory;
$this->storeManager = $storeManager;
$this->session = $session;
$this->itemBuilderFactory = $itemBuilderFactory;
$this->checkoutSession = $checkoutSession;
}

/**
Expand Down Expand Up @@ -74,4 +79,39 @@ public function createQuote()

return $quote;
}

public function createOrUpdateQuote()
{
/** @var \Magento\Quote\Model\Quote $quote */
$storeId = $this->storeManager->getStore()->getId();

$quote = $this->checkoutSession->getQuote();

// Check if a cart already exists for the customer
if ($quote->getId()) {

// Existing quote found, load it
$quote->load($quote->getId());

} else {
$quote = $this->quoteFactory->create();

// Guest user flow
$quote->setStoreId($storeId);
$quote->setCustomerIsGuest(1);
}

/** @var ItemBuilder $itemBuilder */
$itemBuilder = $this->itemBuilderFactory->create(['quote' => $quote]);
$itemBuilder->addItems();

$quote->setIsActive(1);
$quote->setTotalsCollectedFlag(false)->collectTotals()->save();

$this->session->setQuoteId($quote->getId());
$this->checkoutSession->setQuoteId($quote->getId());

return $quote;
}

}
16 changes: 14 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,19 @@
</depends>
</field>

<field id="activate_magic_mini_cart" translate="label" type="select" sortOrder="27" showInDefault="0" showInWebsite="0" showInStore="0">
<field id="magic_buy_now_cart_push" translate="label" type="select" sortOrder="27" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Update Product to Existing Cart on Buy Now </label>
<comment>If you enable this configuration then on click of buy now button on PDP, magic will push the item to existing cart</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/razorpay/magic_buy_now_cart_push</config_path>
<depends>
<field id="activate_magic">1</field>
<field id="activate_magic_buy_now">1</field>
</depends>
</field>


<field id="activate_magic_mini_cart" translate="label" type="select" sortOrder="28" showInDefault="0" showInWebsite="0" showInStore="0">
<label>Magic Mini Cart Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/razorpay/activate_magic_mini_cart</config_path>
Expand All @@ -133,7 +145,7 @@
</depends>
</field>

<field id="allow_coupon_apply_magic" translate="label" type="select" sortOrder="28" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="allow_coupon_apply_magic" translate="label" type="select" sortOrder="29" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Allow Coupon Apply on Magic </label>
<comment>Setup coupon widget on Magic Checkout modal</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<custom_paid_order_status>processing</custom_paid_order_status>
<activate_magic>0</activate_magic>
<activate_magic_buy_now>0</activate_magic_buy_now>
<magic_buy_now_cart_push>0</magic_buy_now_cart_push>
<activate_magic_mini_cart>1</activate_magic_mini_cart>
<allow_coupon_apply_magic>1</allow_coupon_apply_magic>
<enable_update_order_cron_v1>1</enable_update_order_cron_v1>
Expand Down