代码之家  ›  专栏  ›  技术社区  ›  harri

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

  •  0
  • harri  · 技术社区  · 7 年前

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

    原始方法

    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修改上述方法的功能:

    <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>
    

    插件

    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->_totals['discount'] 并返回更新的 $此 插件中的对象。我有一个标准的首选项,但如果可能的话,我宁愿使用一个插件。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Pallavi    7 年前

    我认为您应该在实现上述代码之前检查这一点。 http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html 根据DevDocs对magento2的保护功能不能被拦截,所以我们不能使用插件。

    可能是这导致了你的问题。 希望这有帮助!