Question

I have created the custom sales report to show required fields on the grid,

Used below code to show payment method,

Vendor/Module/view/adminhtml/ui_component/salesfinacereport_manage_listing.xml

<column name="payment_method" component="Magento_Ui/js/grid/columns/select">
    <settings>
        <filter>select</filter>
        <options class="Magento\Payment\Ui\Component\Listing\Column\Method\Options"/>
        <dataType>select</dataType>
        <label translate="true">Payment Method</label>
        <visible>true</visible>
    </settings>
</column>

here is my collection file

Vendor/Module/Model/Resource/Products/Collection.php

protected function _initSelect()
{
    parent::_initSelect();
    $this->getSelect()->joinLeft(
        ['secondTable' => $this->getTable('sales_order')],
        'main_table.order_id = secondTable.entity_id',
        '*'
    )->joinLeft( 
    ['thirdTable' => $this->getTable('sales_order_address')], 
    'main_table.order_id = thirdTable.parent_id', 
    '*' 
    )->where('thirdTable.address_type=?', \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING);

    $this->addFieldToFilter('status', [ 'neq' => 'canceled']); 
    return $this;
}

which is not showing the payment method used for the particular order,

Can anyone help me how can we show the payment method used for the order,

Was it helpful?

Solution

The payment data for the order is added under sales_order_payment table in the database. Yo need to use that table in your code to get the payments data in the collection.

I've updated your code for the same.

protected function _initSelect()
{
    parent::_initSelect();
    $this->getSelect()->joinLeft(
        ['secondTable' => $this->getTable('sales_order')],
        'main_table.order_id = secondTable.entity_id',
        '*'
    )->joinLeft( 
        ['thirdTable' => $this->getTable('sales_order_address')], 
        'main_table.order_id = thirdTable.parent_id', 
        '*' 
    )->joinLeft( 
        ['fourthTable' => $this->getTable('sales_order_payment')], 
        'main_table.order_id = fourthTable.parent_id', 
        'fourthTable.method' 
    )->where('thirdTable.address_type=?', \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING);

    $this->addFieldToFilter('status', [ 'neq' => 'canceled']); 
    return $this;
}

Please give it a try.

EDIT:

Get Payment method title from the method code

/**
 * Payment Helper Data
 *
 * @var \Magento\Payment\Helper\Data
 */
protected $_paymentHelper;

/**
 * @param \Magento\Payment\Helper\Data $paymentHelper
 */
public function __construct(
    \Magento\Payment\Helper\Data $paymentHelper
) {
    $this->_paymentHelper = $paymentHelper;
}

/**
 * Get key-value pair of all payment methods
 * key = method code & value = method name
 * 
 * @return array
 */ 
public function getAllPaymentMethodsList() 
{
    return $this->_paymentHelper->getPaymentMethodList();
}

Sample output of the getAllPaymentMethodsList() will like

Array
(
    [vault] => 
    [substitution] => 
    [banktransfer] => Bank Transfer Payment
    [cashondelivery] => Cash On Delivery
    [checkmo] => Check / Money order
    [payflowpro] => Credit Card
    [payflow_link] => Credit Card
    [payflow_advanced] => Credit Card
    [braintree] => Credit Card (Braintree)
    [authorizenet_directpost] => Credit Card Direct Post (Authorize.net)
    [free] => No Payment Information Required
    [braintree_paypal] => PayPal (Braintree)
    [paypal_billing_agreement] => PayPal Billing Agreement
    [payflow_express_bml] => PayPal Credit
    [paypal_express_bml] => PayPal Credit
    [paypal_express] => PayPal Express Checkout
    [payflow_express] => PayPal Express Checkout Payflow Edition
    [hosted_pro] => Payment by cards or by PayPal account
    [purchaseorder] => Purchase Order
    [braintree_cc_vault] => Stored Cards (Braintree)
    [payflowpro_cc_vault] => Stored Cards (Payflow Pro)
)

You can get the payment method title by checking the method code in the array.

Edit:

Updating collection to get Invoice Id in the custom collection.

protected function _initSelect()
{
    parent::_initSelect();
    $this->getSelect()->joinLeft(
        ['secondTable' => $this->getTable('sales_order')],
        'main_table.order_id = secondTable.entity_id',
        '*'
    )->joinLeft( 
        ['thirdTable' => $this->getTable('sales_order_address')], 
        'main_table.order_id = thirdTable.parent_id', 
        '*' 
    )->joinLeft( 
        ['fourthTable' => $this->getTable('sales_order_payment')], 
        'main_table.order_id = fourthTable.parent_id', 
        'fourthTable.method' 
    )->joinLeft( 
        ['fifthTable' => $this->getTable('sales_invoice_grid')], 
        'main_table.order_id = fifthTable.parent_id', 
        'fifthTable.increment_id' 
    )->where('thirdTable.address_type=?', \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING);

    $this->addFieldToFilter('status', [ 'neq' => 'canceled']); 
    return $this;
}

Hope it helps!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top