Override functionalities in Woocommerce Reports section - wordpress

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…

Related

Dynamically pull values from nested form using Gravity Forms & Gravity Wiz Nested Forms

So I've been going through a restructure build of an entire site, and part of that involved switching from Formidable Forms to Gravity Forms. We did this because we wanted to use the Nested Form feature, so that we could automate multiple attendees without having to create a new form for each.
Here's the problem - on our old site that did have a separate form per attendee via Formidable, we had a code using the Canvas API to send name + email info to Canvas and automatically register users for the online courses this company offers. In trying to convert sections of this code to work with my nested forms, I'm running into a snag:
The main issue is that the value is being spit out as all of the information from the nested form entry, not by name/ email/ etc.
The info is being spit out twice, perhaps because of the way the forms are structured? There are a couple calculations happening in the forms/ nested forms so I'm chalking it up to that.
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 212
[key] => Attendee Registration
[value] =>
Name
Test Name
Email Address
courses#email.com
Cell Phone
(333) 333-3333
Would you like to receive text message reminders for this registration?
No
Post-class notification is required for the following states, please identify if you will be using this class to fulfill any state license requirements:
N/A
You'll receive a hard copy and digital certificate upon course completion. Additional options are available here:
All live classes include a hard copy manual and regulations. To join our effort to save paper, please also add any of the following options to take your books home:
)
[data:protected] => Array
(
[id] => 212
[key] => Attendee Registration
[value] =>
Name
Test Name
Email Address
courses#email.com
Cell Phone
(333) 333-3333
Would you like to receive text message reminders for this registration?
No
Post-class notification is required for the following states, please identify if you will be using this class to fulfill any state license requirements:
N/A
You'll receive a hard copy and digital certificate upon course completion. Additional options are available here:
All live classes include a hard copy manual and regulations. To join our effort to save paper, please also add any of the following options to take your books home:
)
)
Also: I was playing around with grabbing the ID of the main entry via [_gravity_form_linked_entry_id], and grabbing the nested info from that via [_gravity_form_lead].
The best I was able to get from that was this... so yeah kind of lost on how to progress here if anyone has any pointers! Thanks so much!
[data:protected] => Array
(
[id] => 211
[key] => _gravity_forms_history
[value] => Array
(
[_gravity_form_cart_item_key] => 72201a9586fb30895b8fb5cac2a796b9
[_gravity_form_linked_entry_id] => 125
[_gravity_form_lead] => Array
(
[form_id] => 1
[source_url] => https://chcv2.flywheelstaging.com/product/air-monitoring-specialist-live/
[ip] => 75.151.95.41
[42.1] => Course Price
[42.2] => $580.00
[42.3] => 1
[21] => 122
[40.1] => Add-On Fees
[40.2] => $0.00
[40.3] => 1
)
[_gravity_form_data] => Array
(
[id] => 1
[bulk_id] => 0
[display_title] =>
[display_description] =>
[disable_woocommerce_price] => no
[price_before] =>
[price_after] =>
[disable_calculations] => no
[disable_label_subtotal] => yes
[disable_label_options] => yes
[disable_label_total] => no
[disable_anchor] => no
[label_subtotal] => Course Fee
[label_options] => Additional Attendees + Selected Options
[label_total] => Attendee Registration + Add-Ons:
[use_ajax] => no
[enable_cart_edit] => no
[enable_cart_edit_remove] => no
[keep_cart_entries] => no
[send_notifications] => no
[enable_cart_quantity_management] => stock
[cart_quantity_field] =>
[update_payment_details] => yes
[display_totals_location] => after
[structured_data_override] => no
[structured_data_low_price] =>
[structured_data_high_price] =>
[structured_data_override_type] => overwrite
)
)
)
Update: Here's how I've incorporated the code from Rochelle's comment below, where I'm getting an error
add_action( 'woocommerce_thankyou', 'canvas_enroll', 20, 2 );
function canvas_enroll($orders) {
$query = new WC_Order_Query( array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
foreach($orders as $order){
foreach ($order->get_items() as $item_id => $item_data) {
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
$linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$name[$n] = $entry_nest[$n]['12.3'].''.$entry_nest[$n]['12.6'];//replace 1.3 and 1.6 with nested field id of name
$email[$n] = $entry_nest[$n]['11']; //2 is the GF nested field id of email
}
}
}
}
}
}
}
Finally got this figured out! Something that was super helpful was to echo the item meta data ($value, in my case) for that to display all the ids and such, that's how I was able to figure out that I needed 21 in that ID for the child entries.
I'm not really sure why I had to switch to wc_get_order instead of wc_order_query, but it solved the errors I was getting.
function canvas_enroll($order_id) {
$order = wc_get_order( $order_id);
$order_id = array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
);
if(!empty($order) && isset($order)){
// Loop through order line items
foreach( $order->get_items() as $key => $value ){
// get order item data (in an unprotected array)
if(isset($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$value->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21']) && !empty($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'])) { //21 was the id for my child form
$linked_nested_value = $value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$firstname[$n] = $entry_nest[$n]['12.3'];//replace 12.3 with nested field id of first name
$lastname[$n] = $entry_nest[$n]['12.6'];//replace 12.6 with nested field id of last name
$email[$n] = $entry_nest[$n]['11']; //replace 11 with nested field id of email
}
}
}
}
}
}
}
I'm just going to paste the code I used for another project that I needed to pull the same type of data in case it puts you on the right track. You'd have to replace the numbers with the ids from your forms:
$query = new WC_Order_Query( array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
foreach($orders as $order){
foreach ($order->get_items() as $item_id => $item_data) {
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
$linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$name[$n] = $entry_nest[$n]['1.3'].''.$entry_nest[$n]['1.6'];//replace 1.3 and 1.6 with nested field id of name
$email[$n] = $entry_nest[$n]['2']; //2 is the GF nested field id of email
}
}
}
}
}
}
Ok. Try this first to see if it changes anything:
$query = new WC_Order_Query( array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
foreach($orders as $order){
if(!empty($order) && isset($order)){
foreach ($order->get_items() as $item_id => $item_data) {
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
$linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$name[$n] = $entry_nest[$n]['1.3'].''.$entry_nest[$n]['1.6'];//replace 1.3 and 1.6 with nested field id of name
$email[$n] = $entry_nest[$n]['2']; //2 is the GF nested field id of email
}
}
}
}
}
}
}

Access array data inside of an object (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

shortcode_atts not parsing custom values instead defaults loaded

I' making my own shortcode function and while the call of the shortcode works, and my page query within returns results - it never uses any settings but the defaults as if $att is null.
function test_shortcode( $atts ) {
$filter = shortcode_atts(
array(
'type' => 'major',
'sort' => 'name',
'size' => 'large',
'links' => 'yes',
),
$atts,
'customshortcode'
);
echo 'ATTS:';
print_r($atts);
echo'FILTER';
print_r($filter);
//code to query posts removed
}
add_shortcode( 'customshortcode', 'test_shortcode' );
In the post I can then add..
[customshortcode type:"other" size:"small" sort:"rand" links:"no"]
To see the result
ATTS
Array
(
[0] => type:"other"
[1] => size:"small"
[2] => sort:"rand"
[3] => links:"no"
)
FILTER
Array
(
[type] => major
[sort] => name
[size] => large
[links] => yes
)
and I can see the $atts values are received in the function but the $filter is not updated. I'm expecting both arrays to be the same at the point they are being printed out. As far as I can tell I'm following the coxed formatting here https://codex.wordpress.org/Function_Reference/shortcode_atts
You are passing attributes in the wrong way.
It should use = instead of :.
Please go through https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ to know more about shortcode with parameters.
Try with [customshortcode type="other" size="small" sort="rand" links="no"]

get_the_category() wordpress function stopped working after updating core version

As the title says - i have a single.php template which gets the category of the current post assigned.
$cat_post = get_the_category();
That function returns the category which is assigned to the post. After upgrading from 4.0.24 to 4.9.7 it stopped working. The only thing that comes to my mind is that the function is not supported but i haven't found something about this.
Any ideas?
you can use this
//get category
$category = get_the_category(get_the_ID());
//the above code will return an Array
Array
(
[0] => WP_Term Object
(
[term_id] => 2
[name] => video
[slug] => video
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[cat_ID] => 2
[category_count] => 1
[category_description] =>
[cat_name] => video
[category_nicename] => video
[category_parent] => 0
)
)
// you can access this array like this
$catID = $category[0]->term_id;
//for term id
$catName = $category[0]->name;
//for category name
I think you are trying to get a category from the current post which is the custom post type.
As per the documentation of the following function, it doesn't work for the custom post type. You can see it on the link below.
Click here to see the Code Reference
For custom post type, you can use get_the_terms() method. Here is the link of the Code Reference of the function.

Custom Taxonomy Template won't show any menus

This is how I can start describing this, it's a very strange error that maybe something else but this is the first symptom I have.
I have a custom template[taxonomy-location_types.php] that shows, a custom post type [locations] by it's custom taxonomy[location-types].
If you visit the home page you will see a menu editorial, travel, etc.
Now if you look here on this taxonomy page
The menu is gone, there are other queries that aren't firing but I'm focussing on this as a primary issue.
I have generated output from nav-menu-template.php like so:
if ( $menu && !is_wp_error($menu) && !isset($menu_items) )
{
write_log($menu);
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
write_log($menu_items);
}
and I get this:
[31-Aug-2017 15:27:52 UTC] WP_Term Object
(
[term_id] => 2
[name] => Header Menu
[slug] => header-menu
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[31-Aug-2017 15:27:52 UTC] Array
(
)
[31-Aug-2017 15:27:52 UTC] WP_Term Object
(
[term_id] => 3
[name] => Footer One
[slug] => footer-one
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 5
[filter] => raw
)
[31-Aug-2017 15:27:52 UTC] Array
(
)
So it sees the menu but for some reason return no items in either menu.
I have turned off all plugins and still get the same result, I have also eliminated all other queries on this page to see if there was conflict and this still this happens.
Any insight on this one would be great not sure where to look next for issues.
Chalk another one up for RTFM, I had a pre_get_posts hook altering the query and I didn't ensure that the query it was altering was just the $query->is_main_query() because strangely enough this alteration was getting into the nav_menu query.
query monitor is now my new best friend.

Resources