Skip to content

Commit

Permalink
Merge pull request #51 from mageprince/develop-1.4.0
Browse files Browse the repository at this point in the history
Develop 1.4.0
  • Loading branch information
mageprince authored May 12, 2024
2 parents df2561d + 0176ba9 commit 189d275
Show file tree
Hide file tree
Showing 27 changed files with 635 additions and 324 deletions.
22 changes: 17 additions & 5 deletions Block/Adminhtml/System/Config/Field/Disable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

/**
* MagePrince
* Copyright (C) 2020 Mageprince <[email protected]>
*
* @package Mageprince_BuyNow
* @copyright Copyright (c) 2020 Mageprince (http://www.mageprince.com/)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License,version 3 (GPL-3.0)
* @author MagePrince <[email protected]>
* NOTICE OF LICENSE
*
* This source file is subject to the mageprince.com license that is
* available through the world-wide-web at this URL:
* https://mageprince.com/end-user-license-agreement
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category MagePrince
* @package Mageprince_BuyNow
* @copyright Copyright (c) MagePrince (https://mageprince.com/)
* @license https://mageprince.com/end-user-license-agreement
*/

namespace Mageprince\BuyNow\Block\Adminhtml\System\Config\Field;
Expand All @@ -18,6 +28,8 @@
class Disable extends Field
{
/**
* Disable element
*
* @param AbstractElement $element
* @return string
*/
Expand Down
26 changes: 20 additions & 6 deletions Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

/**
* MagePrince
* Copyright (C) 2020 Mageprince <[email protected]>
*
* @package Mageprince_BuyNow
* @copyright Copyright (c) 2020 Mageprince (http://www.mageprince.com/)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License,version 3 (GPL-3.0)
* @author MagePrince <[email protected]>
* NOTICE OF LICENSE
*
* This source file is subject to the mageprince.com license that is
* available through the world-wide-web at this URL:
* https://mageprince.com/end-user-license-agreement
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category MagePrince
* @package Mageprince_BuyNow
* @copyright Copyright (c) MagePrince (https://mageprince.com/)
* @license https://mageprince.com/end-user-license-agreement
*/

namespace Mageprince\BuyNow\Block\Product;
Expand Down Expand Up @@ -47,7 +57,11 @@ public function __construct(
*/
public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
{
$url = $this->getAddToCartUrl($product);
$url = str_replace(
'checkout/cart/add',
'buynow/cart/add',
$this->getAddToCartUrl($product)
);
return [
'action' => $url,
'data' => [
Expand Down
33 changes: 24 additions & 9 deletions Controller/Cart/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

/**
* MagePrince
* Copyright (C) 2020 Mageprince <[email protected]>
*
* @package Mageprince_BuyNow
* @copyright Copyright (c) 2020 Mageprince (http://www.mageprince.com/)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License,version 3 (GPL-3.0)
* @author MagePrince <[email protected]>
* NOTICE OF LICENSE
*
* This source file is subject to the mageprince.com license that is
* available through the world-wide-web at this URL:
* https://mageprince.com/end-user-license-agreement
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category MagePrince
* @package Mageprince_BuyNow
* @copyright Copyright (c) MagePrince (https://mageprince.com/)
* @license https://mageprince.com/end-user-license-agreement
*/

namespace Mageprince\BuyNow\Controller\Cart;

use Magento\Framework\Filter\LocalizedToNormalized;
use Mageprince\BuyNow\ViewModel\BuyNow as BuyNowViewModel;

class Add extends \Magento\Checkout\Controller\Cart\Add
{
/**
Expand All @@ -33,7 +46,7 @@ public function execute()

try {
if (isset($params['qty'])) {
$filter = new \Zend_Filter_LocalizedToNormalized(
$filter = new LocalizedToNormalized(
['locale' => $this->_objectManager->get(
\Magento\Framework\Locale\ResolverInterface::class
)->getLocale()]
Expand All @@ -51,7 +64,7 @@ public function execute()
return $this->goBack();
}

$buyNowHelper = $this->_objectManager->create(\Mageprince\BuyNow\Helper\Data::class);
$buyNowHelper = $this->_objectManager->create(BuyNowViewModel::class);
$cartProducts = $buyNowHelper->keepCartProducts();
if (!$cartProducts) {
$this->cart->truncate(); //remove all products from cart
Expand All @@ -61,7 +74,6 @@ public function execute()
if (!empty($related)) {
$this->cart->addProductsByIds(explode(',', $related));
}

$this->cart->save();

/**
Expand Down Expand Up @@ -96,7 +108,10 @@ public function execute()
}
return $this->goBack($url);
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.'));
$this->messageManager->addExceptionMessage(
$e,
__('We can\'t add this item to your shopping cart right now.')
);
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
return $this->goBack();
}
Expand Down
83 changes: 0 additions & 83 deletions Helper/Data.php

This file was deleted.

80 changes: 50 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
# Magento 2 BuyNow
[![Latest Stable Version](https://poser.pugx.org/mageprince/module-buynow/v)](//packagist.org/packages/mageprince/module-buynow)
[![Total Downloads](https://poser.pugx.org/mageprince/module-buynow/downloads)](//packagist.org/packages/mageprince/module-buynow)
[![Monthly Downloads](https://poser.pugx.org/mageprince/module-buynow/d/monthly)](//packagist.org/packages/mageprince/module-buynow)
[![License](https://poser.pugx.org/mageprince/module-buynow/license)](//packagist.org/packages/mageprince/module-buynow)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mageprince/magento2-buynow/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mageprince/magento2-FAQ/?branch=master)
[![Code Intelligence Status](https://scrutinizer-ci.com/g/mageprince/magento2-buynow/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)

This module add "BuyNow" button on product view page and list page to process directly checkout.
# Magento 2 Buy Now

BuyNow Configuration: Stores->Configuration->MagePrince->Buy Now
The Buy Now extension for Magento 2 enhances the shopping experience by allowing customers to make instant purchases with a single click. It adds a "Buy Now" button to product pages, enabling customers to bypass the cart and proceed directly to the checkout page.

Admin Configuration: `Admin > Stores > Configuration > MagePrince > Buy Now`

# How to install

### 1. Install via composer (packagist.org)

Run the following command in the Magento 2 root folder:

composer require mageprince/module-buynow
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy

### 2. Install manually (Not recommended)

Copy the content of the repo to the <b>app/code/Mageprince/BuyNow</b> folder and run the following command in the Magento 2 root folder:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy

# Notice

<b>We don't support Buy Now button on related, upsell, wishlist or any other places because it needs override core phtml files which is not the recommended solution. Please keep in note that most of the paid or free version of the Buy Now module overrides the core phtml files.</b>
We do not provide support for placing the Buy Now button on related, upsell, wishlist, or any other locations, as it requires overriding core phtml files, which isn't a good idea for an extension. <b>Please be aware that many paid or free versions of the Buy Now module override these core files</b>. Instead, use this piece of code to add the Buy Now button to custom product templates.

# Copy below code to add Buy Now button in custom product sliders, widget, static blocks etc.
$buyNowBtnHtml = $this->getLayout()
->createBlock(\Mageprince\BuyNow\Block\Product\ListProduct::class)
->setProduct($_item)
->setButtonTitle('Buy Now')
->setTemplate('Mageprince_BuyNow::buynow.phtml')
->toHtml();
echo $buyNowBtnHtml;

``````
$buyNowHtml = $this->getLayout()
->createBlock('Mageprince\BuyNow\Block\Product\ListProduct')
->setProduct($_item)
->setTemplate('Mageprince_BuyNow::buynow-list.phtml')
->toHtml();
echo $buyNowHtml;
``````
<b>Change `$_item` to current product object.</b>

You can use above code where you want to show buy now button in product. Please make sure don't copy this code to addtocart or any other form. Put this code after any `</form>`. Here is the screenshot of sample code of usage

<img src="https://user-images.githubusercontent.com/24751863/93671613-00aa9480-fac2-11ea-833b-5bd2c1d2a2fb.png" width="500"/>
You can use the code above to display the Buy Now button wherever you want on your product page. Just remember not to paste this code into the add to cart form or any other form. Put the code after `</form>` tag. Below is a screenshot showing how to use the code.

**Sample template:** _vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml_

# Installation Instruction
<img src="https://github.com/mageprince/magento2-buynow/assets/24751863/5ad4baf6-5897-4ea4-adda-8244126524c3" width="500"/>

* Copy the content of the repo to the <b>app/code/Mageprince/BuyNow</b> folder
* Run command:
<b>php bin/magento setup:upgrade</b>
* Run Command:
<b>php bin/magento setup:static-content:deploy</b>
* Now Flush Cache: <b>php bin/magento cache:flush</b>

# Contribution

Expand All @@ -42,16 +59,19 @@ Want to contribute to this extension? The quickest way is to <a href="https://he

If you encounter any problems or bugs, please <a href="https://github.com/mageprince/magento2-buynow/issues">open an issue</a> on GitHub.

<b>PRODUCT VIEW PAGE</b>
# Screenshots

### Product view page

<img width="687" alt="Product List Page" src="https://github.com/mageprince/magento2-buynow/assets/24751863/02ca3bcf-76cf-4226-bc57-9618e765abb7">

### Product list page

<img src="https://raw.githubusercontent.com/mageprince/all-module-screenshots/master/BuyNow/listpage.png" alt="View Page" border="0">
<img width="687" alt="Product List Page" src="https://github.com/mageprince/magento2-buynow/assets/24751863/dfb6ac6c-dcde-4103-b0ab-497971763eef">

<b>PRODUCT LIST PAGE</b>
### Custom product template with buy now code

<img src="https://raw.githubusercontent.com/mageprince/all-module-screenshots/master/BuyNow/viewpage.png" alt="list page" border="0" />
<img width="687" alt="Custom Product Template" src="https://github.com/mageprince/magento2-buynow/assets/24751863/cce268e2-e2ea-465b-82ca-afc3e0f8d209">

# How To Find Addtocart Form Id - Useful For Custom Theme

Go to product view page and right click on addtocart button and click on inspect element. Then scroll up and find addtocart form id.

<img src="https://raw.githubusercontent.com/mageprince/all-module-screenshots/master/BuyNow/formid.png" alt="Form ID" border="0" />
57 changes: 57 additions & 0 deletions ViewModel/BuyNow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Mageprince\BuyNow\ViewModel;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\ScopeInterface;

class BuyNow implements ArgumentInterface
{
/**
* Buynow config paths
*/
public const BUYNOW_BUTTON_TITLE_PATH = 'buynow/general/button_title';
public const KEEP_CART_PRODUCTS_PATH = 'buynow/general/keep_cart_products';

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* BuyNow constructor.
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

/**
* Retrieve button title
*
* @return string
*/
public function getButtonTitle()
{
return $this->scopeConfig->getValue(
self::BUYNOW_BUTTON_TITLE_PATH,
ScopeInterface::SCOPE_STORE
);
}

/**
* Check if keep cart products
*
* @return string
*/
public function keepCartProducts()
{
return $this->scopeConfig->isSetFlag(
self::KEEP_CART_PRODUCTS_PATH,
ScopeInterface::SCOPE_STORE
);
}
}
Loading

0 comments on commit 189d275

Please sign in to comment.