Magento2:更新核心文件中的一行代码
Magento2: Updating a line of code in core file
这是我需要的:
在文件 vendor/magento/module-sales-rule/Model/Rule/Action/Discount/CartFixed.php 中有一个 public 函数计算
我想在不修改此核心文件的情况下评论此函数内的 If 条件。
if ($availableDiscountAmount > 0) {
我在自定义模块中创建了首选项,但它没有按预期工作。如果有人可以帮助我,我将不胜感激。
像这样使用
code/Magento/* any name or module you need from core file
If you need some more details let me know it is just like Magento 1 but only folder is changed
要自定义 Magento 核心文件,您可以使用首选项或 Plugin。此处有更多详细信息
对于您的情况,您可以按照以下步骤使用首选项:
- 在di.xml中可以添加参考配置:
<preference for="Magento\SalesRule\Model\Rule\Action\Discount\CartFixed" type="Training\Test\Model\Rule\Action\Discount\CartFixed" />
创建Training\Test\Model\Rule\Action\Discount\CartFixed.php:
- @作者 Bach Lee
*/
命名空间Training\Test\Model\Rule\Action\Discount;
使用Magento\Framework\App\ObjectManager;
使用 Magento\Framework\Pricing\PriceCurrencyInterface;
使用 Magento\SalesRule\Model\DeltaPriceRound;
使用 Magento\SalesRule\Model\Validator;
使用 Magento\SalesRule\Model\Rule\Action\DiscountFactory;
class CartFixed 扩展 \Magento\SalesRule\Model\Rule\Action\Discount\CartFixed
{
/**
* @var 字符串
*/
私人静态 $discountType = 'CartFixed';
/**
* @var DeltaPriceRound
*/
私人 $deltaPriceRound;
/**
* @param Validator $validator
* @param DataFactory $discountDataFactory
* @param PriceCurrencyInterface $priceCurrency
* @param DeltaPriceRound $deltaPriceRound
*/
public function __construct(
Validator $validator,
DataFactory $discountDataFactory,
PriceCurrencyInterface $priceCurrency,
DeltaPriceRound $deltaPriceRound = null
) {
$this->deltaPriceRound = $deltaPriceRound ?: ObjectManager::getInstance()->get(DeltaPriceRound::class);
parent::__construct($validator, $discountDataFactory, $priceCurrency, $deltaPriceRound);
}
/**
* @param \Magento\SalesRule\Model\Rule $rule
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param float $qty
* @return \Magento\SalesRule\Model\Rule\Action\Discount\Data
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function calculate($rule, $item, $qty)
{
/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
$discountData = $this->discountFactory->create();
$ruleTotals = $this->validator->getRuleItemTotalsInfo($rule->getId());
$quote = $item->getQuote();
$address = $item->getAddress();
$itemPrice = $this->validator->getItemPrice($item);
$baseItemPrice = $this->validator->getItemBasePrice($item);
$itemOriginalPrice = $this->validator->getItemOriginalPrice($item);
$baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item);
/**
* prevent applying whole cart discount for every shipping order, but only for first order
*/
if ($quote->getIsMultiShipping()) {
$usedForAddressId = $this->getCartFixedRuleUsedForAddress($rule->getId());
if ($usedForAddressId && $usedForAddressId != $address->getId()) {
return $discountData;
} else {
$this->setCartFixedRuleUsedForAddress($rule->getId(), $address->getId());
}
}
$cartRules = $address->getCartFixedRules();
if (!isset($cartRules[$rule->getId()])) {
$cartRules[$rule->getId()] = $rule->getDiscountAmount();
}
$availableDiscountAmount = (float)$cartRules[$rule->getId()];
$discountType = self::$discountType . $rule->getId();
$store = $quote->getStore();
if ($ruleTotals['items_count'] <= 1) {
$quoteAmount = $this->priceCurrency->convert($availableDiscountAmount, $store);
$baseDiscountAmount = min($baseItemPrice * $qty, $availableDiscountAmount);
$this->deltaPriceRound->reset($discountType);
} else {
$ratio = $baseItemPrice * $qty / $ruleTotals['base_items_price'];
$maximumItemDiscount = $this->deltaPriceRound->round(
$rule->getDiscountAmount() * $ratio,
$discountType
);
$quoteAmount = $this->priceCurrency->convert($maximumItemDiscount, $store);
$baseDiscountAmount = min($baseItemPrice * $qty, $maximumItemDiscount);
$this->validator->decrementRuleItemTotalsCount($rule->getId());
}
$baseDiscountAmount = $this->priceCurrency->round($baseDiscountAmount);
$availableDiscountAmount -= $baseDiscountAmount;
$cartRules[$rule->getId()] = $availableDiscountAmount;
if ($availableDiscountAmount <= 0) {
$this->deltaPriceRound->reset($discountType);
}
$discountData->setAmount($this->priceCurrency->round(min($itemPrice * $qty, $quoteAmount)));
$discountData->setBaseAmount($baseDiscountAmount);
$discountData->setOriginalAmount(min($itemOriginalPrice * $qty, $quoteAmount));
$discountData->setBaseOriginalAmount($this->priceCurrency->round($baseItemOriginalPrice));
$address->setCartFixedRules($cartRules);
return $discountData;
}
}
或者您可以使用 cweagans/composer-patches following 编辑核心 Magento 文件
此致
App/Code/Magento/SalesRule/model/*
请这样使用
添加后请使用编译命令
对我有用,对你也有用
首先添加文件夹
App/Code/Magento/SalesRule/model/folder/file.php
并感谢使用 "setup:upgrade" 更新文件。
比命令"setup:di:compile"。
问候
纳西姆
这是我需要的: 在文件 vendor/magento/module-sales-rule/Model/Rule/Action/Discount/CartFixed.php 中有一个 public 函数计算 我想在不修改此核心文件的情况下评论此函数内的 If 条件。
if ($availableDiscountAmount > 0) {
我在自定义模块中创建了首选项,但它没有按预期工作。如果有人可以帮助我,我将不胜感激。
像这样使用
code/Magento/* any name or module you need from core file If you need some more details let me know it is just like Magento 1 but only folder is changed
要自定义 Magento 核心文件,您可以使用首选项或 Plugin。此处有更多详细信息
对于您的情况,您可以按照以下步骤使用首选项:
- 在di.xml中可以添加参考配置:
<preference for="Magento\SalesRule\Model\Rule\Action\Discount\CartFixed" type="Training\Test\Model\Rule\Action\Discount\CartFixed" />
创建Training\Test\Model\Rule\Action\Discount\CartFixed.php:
- @作者 Bach Lee */
命名空间Training\Test\Model\Rule\Action\Discount;
使用Magento\Framework\App\ObjectManager; 使用 Magento\Framework\Pricing\PriceCurrencyInterface; 使用 Magento\SalesRule\Model\DeltaPriceRound; 使用 Magento\SalesRule\Model\Validator; 使用 Magento\SalesRule\Model\Rule\Action\DiscountFactory;
class CartFixed 扩展 \Magento\SalesRule\Model\Rule\Action\Discount\CartFixed { /** * @var 字符串 */ 私人静态 $discountType = 'CartFixed'; /** * @var DeltaPriceRound */ 私人 $deltaPriceRound;
/** * @param Validator $validator * @param DataFactory $discountDataFactory * @param PriceCurrencyInterface $priceCurrency * @param DeltaPriceRound $deltaPriceRound */ public function __construct( Validator $validator, DataFactory $discountDataFactory, PriceCurrencyInterface $priceCurrency, DeltaPriceRound $deltaPriceRound = null ) { $this->deltaPriceRound = $deltaPriceRound ?: ObjectManager::getInstance()->get(DeltaPriceRound::class); parent::__construct($validator, $discountDataFactory, $priceCurrency, $deltaPriceRound); } /** * @param \Magento\SalesRule\Model\Rule $rule * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item * @param float $qty * @return \Magento\SalesRule\Model\Rule\Action\Discount\Data * @throws \Magento\Framework\Exception\LocalizedException */ public function calculate($rule, $item, $qty) { /** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */ $discountData = $this->discountFactory->create(); $ruleTotals = $this->validator->getRuleItemTotalsInfo($rule->getId()); $quote = $item->getQuote(); $address = $item->getAddress(); $itemPrice = $this->validator->getItemPrice($item); $baseItemPrice = $this->validator->getItemBasePrice($item); $itemOriginalPrice = $this->validator->getItemOriginalPrice($item); $baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item); /** * prevent applying whole cart discount for every shipping order, but only for first order */ if ($quote->getIsMultiShipping()) { $usedForAddressId = $this->getCartFixedRuleUsedForAddress($rule->getId()); if ($usedForAddressId && $usedForAddressId != $address->getId()) { return $discountData; } else { $this->setCartFixedRuleUsedForAddress($rule->getId(), $address->getId()); } } $cartRules = $address->getCartFixedRules(); if (!isset($cartRules[$rule->getId()])) { $cartRules[$rule->getId()] = $rule->getDiscountAmount(); } $availableDiscountAmount = (float)$cartRules[$rule->getId()]; $discountType = self::$discountType . $rule->getId(); $store = $quote->getStore(); if ($ruleTotals['items_count'] <= 1) { $quoteAmount = $this->priceCurrency->convert($availableDiscountAmount, $store); $baseDiscountAmount = min($baseItemPrice * $qty, $availableDiscountAmount); $this->deltaPriceRound->reset($discountType); } else { $ratio = $baseItemPrice * $qty / $ruleTotals['base_items_price']; $maximumItemDiscount = $this->deltaPriceRound->round( $rule->getDiscountAmount() * $ratio, $discountType ); $quoteAmount = $this->priceCurrency->convert($maximumItemDiscount, $store); $baseDiscountAmount = min($baseItemPrice * $qty, $maximumItemDiscount); $this->validator->decrementRuleItemTotalsCount($rule->getId()); } $baseDiscountAmount = $this->priceCurrency->round($baseDiscountAmount); $availableDiscountAmount -= $baseDiscountAmount; $cartRules[$rule->getId()] = $availableDiscountAmount; if ($availableDiscountAmount <= 0) { $this->deltaPriceRound->reset($discountType); } $discountData->setAmount($this->priceCurrency->round(min($itemPrice * $qty, $quoteAmount))); $discountData->setBaseAmount($baseDiscountAmount); $discountData->setOriginalAmount(min($itemOriginalPrice * $qty, $quoteAmount)); $discountData->setBaseOriginalAmount($this->priceCurrency->round($baseItemOriginalPrice)); $address->setCartFixedRules($cartRules); return $discountData; }
}
或者您可以使用 cweagans/composer-patches following
此致
App/Code/Magento/SalesRule/model/*
请这样使用
添加后请使用编译命令
对我有用,对你也有用
首先添加文件夹 App/Code/Magento/SalesRule/model/folder/file.php
并感谢使用 "setup:upgrade" 更新文件。 比命令"setup:di:compile"。
问候 纳西姆