PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 27, 2022

[FIXED] PHP check payment method in returned object

 January 27, 2022     php     No comments   

Issue

I'm not so good in PHP because of the way it prints arrays. I return a var called $order, and I get the following:

{
    "id":14770,
    "parent_id":0,
    "status":"on-hold",
    "currency":"EUR",
    "version":"3.0.7",
    "prices_include_tax":true,
    "date_created":{
        "date":"2017-06-08 12:11:24.000000",
        "timezone_type":1,
        "timezone":"+00:00"
    },
    "date_modified":{
        "date":"2017-06-08 12:11:24.000000",
        "timezone_type":1,
        "timezone":"+00:00"
    },
    "discount_total":"0",
    "discount_tax":"0",
    "shipping_total":"4.09",
    "shipping_tax":"0.8589",
    "cart_tax":"1.1107",
    "total":"11.35",
    "total_tax":"1.9696",
    "customer_id":8,
    "order_key":"wc_order_59393eec03f4a",
    "payment_method":"bacs",
    "payment_method_title":"Bankoverschrijving",
    "transaction_id":"",
    "customer_user_agent":"mozilla\/5.0 (windows nt 10.0; win64; x64) applewebkit\/537.36 (khtml, like gecko) chrome\/58.0.3029.110 safari\/537.36",
    "created_via":"checkout",
    "customer_note":"",
    "date_completed":null,
    "date_paid":null,
    "cart_hash":"c517e3080e56d6d341bc93d4ff219b31",
    "number":"14770",
    "meta_data":[
        {
            "id":257891,
            "key":"_wc_customer_order_xml_export_suite_is_exported",
            "value":"0"
        },
        {
            "id":257892,
            "key":"_wc_customer_order_xml_export_suite_customer_is_exported",
            "value":"0"
        },
        {
            "id":257938,
            "key":"_billing_email-2"
        },
        {
            "id":257939,
            "key":"vat_number",
            "value":"NL234077232B02"
        },
        {
            "id":257940,
            "key":"_vat_country",
            "value":"NL"
        },
        {
            "id":257941,
            "key":"_vat_number_validated",
            "value":"valid"
        },
        {
            "id":257942,
            "key":"_customer_location_self_certified",
            "value":"no"
        },
        {
            "id":257943,
            "key":"_eu_vat_data",
            "value":{
                "eu_vat_assistant_version":"1.7.8.170421",
                "exchange_rates_provider_label":"BitPay",
                "invoice_currency":"EUR",
                "taxes":{
                    "25":{
                        "label":"21% NL BTW",
                        "vat_rate":"21.0000",
                        "country":"NL",
                        "tax_rate_class":"",
                        "tax_payable_to_country":"NL",
                        "amounts":{
                            "items_total":1.1107,
                            "shipping_total":0.8589
                        }
                    }
                },
                "vat_currency":"EUR",
                "vat_currency_exchange_rate":1,
                "vat_currency_exchange_rate_timestamp":1493135026
            }
        },
        {
            "id":257944,
            "key":"_eu_vat_evidence",
            "value":{
                "eu_vat_assistant_version":"1.7.8.170421",
                "location":{
                    "is_eu_country":1,
                    "billing_country":"NL",
                    "shipping_country":"NL",
                    "self_certified":"no"
                },
                "exemption":{
                    "vat_country":"NL",
                    "vat_number_validated":"valid"
                }
            }
        },
        {
            "id":257947,
            "key":"_wcpdf_proforma_date",
            "value":"2017-06-08 14:11:25"
        },
        {
            "id":257948,
            "key":"_wcpdf_proforma_number",
            "value":"513"
        },
        {
            "id":257949,
            "key":"_wcpdf_formatted_proforma_number",
            "value":"89000513"
        },
        {
            "id":257951,
            "key":"_order_stock_reduced",
            "value":"yes"
        }
    ],
    "line_items":{
        "17631":{

        }
    },
    "tax_lines":{
        "17633":{

        }
    },
    "shipping_lines":{
        "17632":{

        }
    },
    "fee_lines":[

    ],
    "coupon_lines":[

    ]
}

I would like to do a check on the payment method, so I can create arguments based on that. Right now, the payment method is bacs, but how can I check that? Any idea what I need to return for that?


Solution

First of all: You have a JSON encoded object. If you want to access it, you have to decode it via json_decode():

$array = json_decode($order, true);
// If you pass true as second parameter,
// you will get an associative array instead of an object.

What you are looking for is then

$payment_method = $array['payment_method'];


Answered By - Tobias F.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing