Magento 2.2 पर मैं काम करने के लिए MagestyApps जवाब नहीं दे सका। मुझे कुछ अतिरिक्त फाइलें जोड़ने की जरूरत थी। इसलिये:
- भुगतान विधि के लिए कार्ट मूल्य नियम को व्यवस्थापक से हटा दिया गया था (जैसा कि DaFunkyAlex द्वारा बताया गया है);
- मैगेंटो 2.2 में उद्धरण पर छूट लागू नहीं की जा रही थी, क्योंकि विधि
\Magento\AdvancedSalesRule\Model\Rule\Condition\FilterTextGenerator\Address\PaymentMethod::generateFilterText
(वास्तव में यह वापस गिर जाती है \Magento\AdvancedSalesRule\Model\Rule\Condition\FilterTextGenerator\Address::generateFilterText
), payment_method
उद्धरण चिह्नों पर डेटा सेट होने की उम्मीद कर रहा था ;
- यहां तक कि MagestyApps से नियंत्रक को बदलने से उद्धरण चिह्नों
payment_method
पर डेटा सेट करने का उत्तर मिलता है , जब उद्धरण एक आदेश बन गया तो काम नहीं किया, क्योंकि यह जारी नहीं है payment_method
;
निम्नलिखित मॉड्यूल ने मेरे लिए काम किया (MagestyApps उत्तर के लिए धन्यवाद, यह उस पर आधारित था):
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_SalesRulesPaymentMethod',
__DIR__
);
etc / module.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_SalesRulesPaymentMethod" setup_version="1.0.0">
<sequence>
<module name="Magento_AdvancedSalesRule"/>
<module name="Magento_Checkout"/>
<module name="Magento_SalesRules"/>
<module name="Magento_Quote"/>
</sequence>
</module>
</config>
etc / di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\AdvancedSalesRule\Model\Rule\Condition\FilterTextGenerator\Address\PaymentMethod"
type="Vendor\SalesRulesPaymentMethod\Model\Rule\Condition\FilterTextGenerator\Address\PaymentMethod"/>
<type name="Magento\SalesRule\Model\Rule\Condition\Address">
<plugin name="addPaymentMethodOptionBack" type="Vendor\SalesRulesPaymentMethod\Plugin\AddPaymentMethodOptionBack" />
</type>
</config>
etc / दृश्यपटल / routes.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="salesrulespaymentmethod" frontName="salesrulespaymentmethod">
<module name="Vendor_SalesRulesPaymentMethod"/>
</route>
</router>
</config>
नियंत्रक / चेकआउट / ApplyPaymentMethod.php
<?php
namespace Vendor\SalesRulesPaymentMethod\Controller\Checkout;
use Magento\Checkout\Model\Cart;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\ForwardFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\LayoutFactory;
use Magento\Quote\Model\Quote;
class ApplyPaymentMethod extends Action
{
/**
* @var ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var LayoutFactory
*/
protected $layoutFactory;
/**
* @var Cart
*/
protected $cart;
/**
* @param Context $context
* @param LayoutFactory $layoutFactory
* @param ForwardFactory $resultForwardFactory
*/
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory,
LayoutFactory $layoutFactory,
Cart $cart
) {
$this->resultForwardFactory = $resultForwardFactory;
$this->layoutFactory = $layoutFactory;
$this->cart = $cart;
parent::__construct($context);
}
/**
* @return ResponseInterface|ResultInterface|void
* @throws \Exception
*/
public function execute()
{
$pMethod = $this->getRequest()->getParam('payment_method');
/** @var Quote $quote */
$quote = $this->cart->getQuote();
$quote->getPayment()->setMethod($pMethod['method']);
$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
$quote->save();
}
}
मॉडल / नियम / स्थिति / FilterTextGenerator / पता / PaymentMethod.php
<?php
namespace Vendor\SalesRulesPaymentMethod\Model\Rule\Condition\FilterTextGenerator\Address;
use Magento\AdvancedSalesRule\Model\Rule\Condition\FilterTextGenerator\Address\PaymentMethod as BasePaymentMethod;
class PaymentMethod extends BasePaymentMethod
{
/**
* @param \Magento\Framework\DataObject $quoteAddress
* @return string[]
*/
public function generateFilterText(\Magento\Framework\DataObject $quoteAddress)
{
$filterText = [];
if ($quoteAddress instanceof \Magento\Quote\Model\Quote\Address) {
$value = $quoteAddress->getQuote()->getPayment()->getMethod();
if (is_scalar($value)) {
$filterText[] = $this->getFilterTextPrefix() . $this->attribute . ':' . $value;
}
}
return $filterText;
}
}
प्लगइन / AddPaymentMethodOptionBack.php
<?php
namespace Vendor\SalesRulesPaymentMethod\Plugin;
use Magento\SalesRule\Model\Rule\Condition\Address;
class AddPaymentMethodOptionBack
{
/**
* @param Address $subject
* @param $result
* @return Address
*/
public function afterLoadAttributeOptions(Address $subject, $result)
{
$attributeOption = $subject->getAttributeOption();
$attributeOption['payment_method'] = __('Payment Method');
$subject->setAttributeOption($attributeOption);
return $subject;
}
}
देखें / दृश्यपटल / requirejs-config.js
var config = {
map: {
'*': {
'Magento_Checkout/js/action/select-payment-method':
'Vendor_SalesRulesPaymentMethod/js/action/select-payment-method'
}
}
};
देखें / दृश्यपटल / वेब / js / कार्रवाई / चयन भुगतान न-method.js
define(
[
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/full-screen-loader',
'jquery',
'Magento_Checkout/js/action/get-totals',
],
function (quote, fullScreenLoader, jQuery, getTotalsAction) {
'use strict';
return function (paymentMethod) {
quote.paymentMethod(paymentMethod);
fullScreenLoader.startLoader();
jQuery.ajax('/salesrulespaymentmethod/checkout/applyPaymentMethod', {
data: {payment_method: paymentMethod},
complete: function () {
getTotalsAction([]);
fullScreenLoader.stopLoader();
}
});
}
}
);