这是我需要的: 在文件vendor / magento / module-sales-rule / Model / Rule / Action / Discount / CartFixed.php文件中,有一个公共函数计算 我想在此函数中注释一个If条件,而不修改此核心文件。
如果($ availableDiscountAmount> 0){
我已经在自定义模块中创建了一个首选项,但是它没有按预期工作。如果有人可以帮助我,我真的很感激。
答案 0 :(得分:1)
要自定义Magento核心文件,可以使用首选项或Plugin。这里有更多细节
根据您的情况,可以将首选项用作以下步骤:
<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:
命名空间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;
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之后使用this question编辑核心Magento文件
致谢
答案 1 :(得分:0)
像这样使用
code / Magento / *核心文件中需要的任何名称或模块 如果您需要更多详细信息,请让我知道它就像Magento 1一样,但是只更改了文件夹
答案 2 :(得分:0)
应用程序/代码/ Magento / SalesRule /模型/ *
请这样使用
答案 3 :(得分:0)
添加此命令后,请使用编译命令
答案 4 :(得分:0)
对我有用,对您也有用
首先添加文件夹 App / Code / Magento / SalesRule / model / folder / file.php
感谢使用setup:grade 比命令setup:di:compile
问候 纳西姆