Access array data inside of an object (Wordpress) - wordpress

WC_Product_Attribute Object
(
[data:protected] => Array
(
[id] => 1
[name] => pa_color
[options] => Array
(
[0] => 26
[1] => 27
[2] => 30
)
[position] => 0
[visible] => 1
[variation] => 1
)
)
How can I access values of name, options etc

A WC_Product_Attribute object should have access to methods that will let you retrieve the protected data.
Say you have your WC_Product_Attribute stored in a variable $attribute. You should be able to use methods like $attribute->get_options() or $attribute->get_name() to get the protected data.
I would recommend reviewing the WooCommerce docs for a more comprehensive list of methods you can use:
https://woocommerce.github.io/code-reference/classes/WC-Product-Attribute.html

Related

Parsing cybersourse Response in PHP

I Need to parse the below array and retrieve values;status,id.
Below is the PHP Array
Array ( [0] => CyberSource\Model\PtsV2PaymentsPost201Response Object ( [container:protected] =>
Array ( [links] => CyberSource\Model\PtsV2PaymentsPost201ResponseLinks Object ( [container:protected] =>
Array ( [self] => CyberSource\Model\PtsV2PaymentsPost201ResponseLinksSelf
Object ( [container:protected] => Array ( [href] => /pts/v2/payments/621258854395 [method] => GET ) )
[reversal] => [capture] => [customer] => [paymentInstrument] => [shippingAddress] => [instrumentIdentifier] => ) )
[id] => 621258854395
[submitTimeUtc] => 2021-05-17T13:40:55Z
[status] => AUTHORIZED
[reconciliationId] => 621258854395
[errorInformation] =>...............
Below is my php code
$parseApiResponse = new CyberSource\Model\PtsV2PaymentsPost201Response($apiResponse);
print_r("Status"." ".$parseApiResponse->getStatus());
Please assist in resolving this.
Want to do a similar thing as I am able to do in java as below
String status = result.getStatus();
I hope this helps someone. It took me a while to figure this out. The original poster was so close. It's just...
print_r("Status"." ".$apiResponse[0]->getStatus());
No need to instantiate a new "PtsV2PaymentsPost201Response()". The response element ([0]) has already done that when you call, "createPayment()", etc.
lib/Model/PtsV2PaymentsPost201Response.php lists all the available getters. getId(), getStatus(), getLinks(),etc.
$parseApiResponse = new CyberSource\Model\PtsV2PaymentsPost201Response($apiResponse);
$result = \GuzzleHttp\json_decode( $parseApiResponse[0] , true );
return = $result['links'];

How push into array without creating numbered indexes in a multidimensional array in PHP?

I want to use array_push() to add to an array, but it always adds a [0] => Array level. How can I prevent that, or take that out afterwards?
For example, I'm trying to push into the '[addOns] => Array' with this:
$addOnid='gcl1';
$addOnid_arr=array('inheritedFromId' => $addOnid);
array_push($result['addOns'], $addOnid_arr);
The array_push is resulting in this:
Array
(
[addOns] => Array
(
[inheritedFromId] => gcl2
)
[0] => Array
(
[inheritedFromId] => gcl1
)
)
And want to make it:
Array
(
[addOns] => Array
(
[inheritedFromId] => gcl2
)
(
[inheritedFromId] => gcl1
)
)
...basically just getting rid of all the [0] => Array, moving all the subarrays up a level.
Maybe I haven't been using the right queries, but I haven't been able to find out how to do this.
Simple just use this instead:
$addOnid = 'gcl1';
$addOnid_arr['addOns'][] = ['inheritedFromId' => $addOnid];

Override functionalities in Woocommerce Reports section

I need to customize the Reports in WooCommerce, so I have to edit the core file class-wc-report-sales-by-date.php, such file doesn't use any hook.
Check this in WC source code: includes/admin/reports/class-wc-report-sales-by-date.php
I have to edit the 393 line, this variable: $this->report_data->total_sales
I need to customize the Total Sales amount to add it another value.
How can I override WooCommerce this core file?
Never override core files… There is other ways made for that. If you look at line 411, you have woocommerce_admin_report_data filter hook to make changes, this way (example):
add_filter( 'woocommerce_admin_report_data', 'custom_admin_report_data', 10, 1 );
function custom_admin_report_data( $report_data ){
// HERE you make your calculations and changes
// New amout to set (example)
$new_calculated_amount = 100;
// Set the new amounts for "total_sales" key
$report_data->total_sales = $new_calculated_amount;
// Raw data output just for testing, to get the keys and the structure of the data
// to be removed
echo '<pre>'; print_r($report_data); echo '</pre>';
// Return the changed data object
return $report_data;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
I have included a line of code that outputs the raw data that you should remove… It's just to see the data structure and the changes made by the function on the "total_sales" value…
The Raw data output is something like this (that gives you the data structure to better make changes):
stdClass Object
(
[order_counts] => Array
(
[0] => stdClass Object
(
[count] => 1
[post_date] => 2017-11-21 16:45:43
)
)
[coupons] => Array
(
)
[order_items] => Array
(
[0] => stdClass Object
(
[order_item_count] => 1
[post_date] => 2017-11-21 16:45:43
)
)
[refunded_order_items] => 0
[orders] => Array
(
[0] => stdClass Object
(
[total_sales] => 48
[total_shipping] => 15
[total_tax] => 5
[total_shipping_tax] => 3
[post_date] => 2017-11-21 16:45:43
)
)
[full_refunds] => Array
(
)
[partial_refunds] => Array
(
)
[refund_lines] => Array
(
)
[total_tax_refunded] => 0
[total_shipping_refunded] => 0
[total_shipping_tax_refunded] => 0
[total_refunds] => 0
[total_tax] => 5.00
[total_shipping] => 15.00
[total_shipping_tax] => 3.00
[total_sales] => 48.00
[net_sales] => 25.00
[average_sales] => 3.57
[average_total_sales] => 6.86
[total_coupons] => 0.00
[total_refunded_orders] => 0
[total_orders] => 1
[total_items] => 1
)
So as you can see you also need to make changes on the "orders" object data as you also have the "total_sales" key…

Wordpress custom query comparing two timestamp meta data

I am trying to query posts by comparing meta value.
I have set two meta with the posts. i.e. 'start_date' and 'end_date'.
they are stored as UNIX timestamp.
Now I want to query posts on following conditions:
timestamp of current momment is after(greater than) 'start_date'.
timestamp of current momment is before(smaller than) 'end_date'.
In this case, I want both conditions to fulfill. So, I have used 'relation'=>'AND'.
So here is the print_r of the query:
Array
(
[post_type] => ads
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[kye] => start_date
[compare] => <=
[value] => 1352054503
[type] => NUMERIC
)
[1] => Array
(
[kye] => end_date
[compare] => >=
[value] => 1352054503
[type] => NUMERIC
)
)
)
Note: ads is a custom post type.
And here is meta of a post:
Array
(
[start_date] => Array
(
[0] => 1352160000
)
[end_date] => Array
(
[0] => 1352246400
)
)
I see absolutely no reason why this post should show up.
The start date timestamp(1352160000) is bigger than the current timestamp(1352054503). Which breaks condition #1.
So, why is this still showing up?
What do you think?
In your meta_query, you're using "kye" instead of "key".

WP_List_Table not properly handling returned data?

So following the WPEngineer guide on WP_List_Table (excellent guide by the way), I managed to put together a basic table for the backend of a plugin I'm working on. Using an 'example' array, it works great. Problem I'm running into is that I can't figure out for the life of me how to replace that sample data with actual data! Adding the query where I thought it was supposed to be results in the query variable having the correct data, but the table still returns no contents. The referenced pastebin is what I have so far... any thoughts?
http://pastebin.com/f0DCacfF
CORRECTION: It IS pulling the data (if I manually add a row to the database, the table count gets updated), but it's displaying a blank table.
NOTE: It seems that the sample data is an array, whereas $wpdb->get_results is returning as a stdClass Object.
Sample data setup:
var $api_key_list = array(
array( 'id' => 1,'userid' => 'Quarter Share', 'key' => 'Nathan Lowell', 'desc' => '978-0982514542' )
);
Sample data return:
Array ( [0] => Array ( [id] => 1 [userid] => 1 [key] => 098f6bcd4621d373cade4e832627b4f6 [desc] => Test API key ) )
Query setup:
$api_key_list_query = "SELECT * from $wpapi_db_table_name";
$this->api_key_list = $wpdb->get_results($api_key_list_query);
Query return:
Array ( [0] => stdClass Object ( [id] => 1 [userid] => 1 [key] => 098f6bcd4621d373cade4e832627b4f6 [desc] => Test API key ) [1] => stdClass Object ( [id] => 2 [userid] => 1 [key] => 098f6bcd4621d373cade4e832627b4f6 [desc] => Test API key 2 ) )
Adding the following solved my problem.
$this->api_key_list = array();
$i = 0;
foreach($api_key_list_return as $obj) {
foreach($obj as $key => $val) {
$this->api_key_list[$i][$key] = $val;
}
$i++;
}

Resources