Question

I created reward_point in the sales_order table, whenever someone purchased a few points stored in that field.

Now I need to display that point into the admin order view page, I already override sales_order_view and create the block, but failed to get the value of the reward point in view page.

sales_order_view.xml

  <referenceContainer name="order_additional_info">
      <block class="SimpleMagento\RewardPoint\Block\Sales\Order\ShowReward" name="sales_order_view_custom" template="order/view/custom.phtml" />
  </referenceContainer>

ShowReward.php

class ShowReward extends \Magento\Sales\Block\Adminhtml\Order\Totals
{
   public function _initTotals(){

    $this->_totals['reward_point'] = new \Magento\Framework\DataObject(
        [
            'code' => 'reward_point',
            'strong' => true,
            'value' => $this->getSource()->getReward(),
            'label' => __('Reward Point'),
            'area' => 'footer',
        ]
    );
    return $this;
   }
}

for the value of the reward_point I have override Order.php, which similarly look like:

public function getReward()
{
    return $this->getData(OrderInterface::reward_point);
}

but for that, I have to override OrderInterface, any other way to achieve this functionality?

Was it helpful?

Solution

You can try below code in your block, it might be helpful to get Reward point custom field value.

protected $orderRepository;

public function __construct(
 \Magento\Sales\Model\OrderRepository $orderRepository
) {
   $this->orderRepository = $orderRepository;
}

public function getReward()
{
   $order_id = $this->getRequest()->getParam('order_id');
   $order =  $this->orderRepository->get($order_id);
   return $order->getRewardPoint(); // your reward point
}

OTHER TIPS

Other way is

protected $factory;

public function __construct(Context $context,
                            OrderFactory $factory,
                            array $data = [])
{
    parent::__construct($context, $data);
    $this->factory = $factory;
}

public function getReward(){
    $id = $this->getRequest()->getParam('order_id');
    $getOrder = $this->factory->create()->load($id);
    return $getOrder['reward_point'];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top