首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Magento 2访问和修改$this对象的插件/拦截器

Magento 2访问和修改$this对象的插件/拦截器
EN

Stack Overflow用户
提问于 2018-07-23 19:44:28
回答 0查看 1.4K关注 0票数 0

我有一个插件,我想修改Magento 2中特定类中方法的功能,但是不太确定如何访问原始对象并返回修改后的数据。

原创方法

代码语言:javascript
运行
复制
protected function _initTotals()
{
    $source = $this->getSource();

    $this->_totals = [];
    $this->_totals['subtotal'] = new \Magento\Framework\DataObject(
        ['code' => 'subtotal', 'value' => $source->getSubtotal(), 'label' => __('Subtotal')]
    );

    /**
     * Add shipping
     */
    if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
        $this->_totals['shipping'] = new \Magento\Framework\DataObject(
            [
                'code' => 'shipping',
                'field' => 'shipping_amount',
                'value' => $this->getSource()->getShippingAmount(),
                'label' => __('Shipping & Handling'),
            ]
        );
    }

    /**
     * Add discount
     */
    if ((double)$this->getSource()->getDiscountAmount()) {
        if ($this->getSource()->getDiscountDescription()) {
            $discountLabel = __('Discount (%1)', $source->getDiscountDescription());
        } else {
            $discountLabel = __('Discount');
        }
        $this->_totals['discount'] = new \Magento\Framework\DataObject(
            [
                'code' => 'discount',
                'field' => 'discount_amount',
                'value' => $source->getDiscountAmount(),
                'label' => $discountLabel,
            ]
        );
    }

    $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
        [
            'code' => 'grand_total',
            'field' => 'grand_total',
            'strong' => true,
            'value' => $source->getGrandTotal(),
            'label' => __('Grand Total'),
        ]
    );

    /**
     * Base grandtotal
     */
    if ($this->getOrder()->isCurrencyDifferent()) {
        $this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
            [
                'code' => 'base_grandtotal',
                'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
                'label' => __('Grand Total to be Charged'),
                'is_formated' => true,
            ]
        );
    }
    return $this;
}

我已经设置了一个插件来用di.xml修改上面方法的功能:

代码语言:javascript
运行
复制
<type name="Magento\Sales\Block\Order\Totals">
    <plugin disabled="false" name="Harrigo_EverDiscountLabel_Plugin_Magento_Sales_Block_Order_Totals" sortOrder="10" type="Harrigo\EverDiscountLabel\Plugin\Magento\Sales\Block\Order\Totals"/>
</type>

插件

代码语言:javascript
运行
复制
class Totals
{

    public function after_initTotals(
        \Magento\Sales\Block\Order\Totals $subject,
        $result
    ) {
        if ((double)$subject->getSource()->getDiscountAmount() != 0 OR $subject->getSource()->getDiscountDescription() != null) {
            if ($subject->getSource()->getDiscountDescription()) {
                $discountLabel = __('Offer (%1)', $source->getDiscountDescription());
            } else {
                $discountLabel = __('Offer');
            }
            $subject->_totals['discount'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'discount',
                    'field' => 'discount_amount',
                    'value' => $source->getDiscountAmount(),
                    'label' => $discountLabel,
                ]
            );
        }
        return $subject;
    }
}

我在插件中使用了$subject而不是$this,但是这对我不起作用。如何访问插件中的$this对象以添加/覆盖$this->_totals['discount']并从插件中返回更新后的$this对象。我有一个标准的首选项可以很好地工作,但如果可能的话,我宁愿使用插件。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51478003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档