Something appears to have changed in the new version of WooCommerce, this snippet to change the order of the items in a formatted address used to work fine....
add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address' );
/**
* woo_custom_order_formatted_billing_address
*
* #access public
* #since 1.0
* #return void
*/
function woo_custom_order_formatted_billing_address() {
$address = array(
'first_name' => $this->billing_first_name,
'last_name' => $this->billing_last_name,
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'state' => $this->billing_state,
'postcode' => $this->billing_postcode,
'country' => $this->billing_country
);
return $address;
}
But now it returns the following error...
Fatal error: Using $this when not in object context in
Can anyone point me in the right direction of either what is going wrong or an alternative way of achieving it?
I'm seeing this example on the internet, but I don't know if it ever worked. First of because in 'woocommerce_order_formatted_billing_address' filter, that is applied in class-wc-order.php file, two arguments are provided, $address ARRAY, and a reference to current WC_Order OBJECT. But your definition of the filter function does not provide any arguments. Second, error that you receive describes the problem very accurately, pseudo-variable $this is available from within an object context, but your global function is not part of any object.
Enough with technicalities, the definition should look like this:
add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address', 10, 2 );
function woo_custom_order_formatted_billing_address( $address, $wc_order ) {
// make the changes to $address array here
// use for example, $wc_order->billing_first_name, instead of $this->billing_first_name
return $address;
}
#Eolis' answer doens't work for me, because Woocommerce applies WC()->countries->get_formatted_address( $address ) and this functions erases my new fields added to array on add_filter, so my solution is add field to previous field (first_name):
add_filter('woocommerce_order_formatted_billing_address', 'my_order_formatted_billing_address', 10, 2);
function my_order_formatted_billing_address($address, $wc_order) {
$billing_last_name_2 = get_post_meta( $wc_order->id, '_billing_last_name_2', true );
$address = array(
'postcode' => $wc_order->billing_postcode,
'last_name' => $wc_order->billing_last_name,
//\n force break line
'first_name' => $wc_order->billing_first_name . "\n" . $billing_last_name_2,
'address_1' => $wc_order->billing_address_1,
'address_2' => $wc_order->billing_address_2,
'city' => $wc_order->billing_city,
'state' => $wc_order->billing_state,
'country' => $wc_order->billing_country
);
return $address;
}
Related
I'm using the following code to add attributes with terms:
$taxonomy = 'pa_' . $attr['name']; // The attribute taxonomy
if (!taxonomy_exists($taxonomy)) {
global $wpdb;
$insert = $wpdb->insert(
$wpdb->prefix . 'woocommerce_attribute_taxonomies',
array(
'attribute_name' => $attr['name'],
'attribute_label' => $attr['name'],
'attribute_public' => 0
),
array('%s', '%s', '%d')
);
if (is_wp_error($insert)) {
throw new WC_API_Exception('woocommerce_api_cannot_create_product_attribute', $insert->get_error_message(), 400);
}
// Clear transients
delete_transient('wc_attribute_taxonomies');
}
if (!term_exists($attr['value'], $attr['name'])) {
wp_insert_term($attr['value'], $attr['name']);
}
$term_slug = get_term_by('name', $attr['value'], $attr['name'])->slug; // Get the term slug
wp_set_post_terms($product_id, $attr['value'], $attr['name'], true);
// Set/save the attribute data in the product variation
update_post_meta($variation_id, 'attribute_' . $taxonomy, $term_slug);
// Assign to the product
wp_set_object_terms($product_id, $attr['value'], $taxonomy, true);
$att = array($taxonomy => array(
'name' => $taxonomy,
'value' => $attr['value'],
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1',
));
update_post_meta($product_id, '_product_attributes', $att);
However, the first time the code runs it adds the attributes without terms. If I run it a second time, only then does it add the terms to the previously added attributes.
Why is that?
Edit: The problem seems to start at the following line:
$term_slug = get_term_by('name', $attr['value'], $attr['name'])->slug
It simply doesn't yet recognize the newly created taxonomy. Only at the next run.
But why? Is there a function that can be used to "refresh" the attributes, or the $wp_attributes variable, which seems to be closely related?
Thanks!
It is hard to debug without looking at the live site. However, I think it could be something to do with this code:
if (!term_exists($attr['value'], $attr['name'])) {
wp_insert_term($attr['value'], $attr['name']);
}
On first request the term is inserted yet it will not be part of WordPress main query until further requests.
Again, this is mostly guessing as I need to see the actual implementation.
I use such a method to create a taxonomy.
public function setAttribute($attributeArgs)
{
$attributeId = wc_attribute_taxonomy_id_by_name($attributeArgs['slug']);
if ($attributeId === 0) {
$attributeId = wc_create_attribute($attributeArgs);
register_taxonomy('pa_' . $attributeArgs['slug'], ['product'], []);
} else {
$attributeId = wc_update_attribute($attributeId, $attributeArgs);
}
return [
'id' => $attributeId,
'args' => $attributeArgs,
];
}
An important line in this code is register_taxonomy('pa_' . $attributeArgs['slug'], ['product'], []);
Without this line, I had the same problem.
I'm using WP as a headless CMS with ACF en ACF-2-REST plugins. I've added categories to a post-type and when I make a GET call, it shows me all the information of a particular post including the categories, but only the ID's. If I want to match it, I have to do another call to Categories to get the information of that categories (name, parent etc).
How can I show that information instead of just the ID in a post call?
How the JSON looks now at the /activities call:
{
"id":111,
"date":"2020-01-18T15:39:27",
"date_gmt":"2020-01-18T15:39:27",
"guid":{"rendered":"https:\/\/url.be\/?post_type=activities&p=111"},
"modified":"2020-01-18T15:39:27",
"modified_gmt":"2020-01-18T15:39:27",
"slug":"walking-on-wood",
"status":"publish",
"type":"activities",
"link":"https:\/\/url.be\/activities\/walking-on-wood\/",
"title":{"rendered":"Walking on wood"},
"template":"",
"categories":[14,25,13,2,18,21,6,24],
"acf":{...}
}
What I want to show in the "categories" instead of just the numbers (from the categories call)
{
"id":3,
"count":1,
"description":"",
"link":"https:\/\/url.be\/category\/duration\/lower-than-30-min\/",
"name":"< 30 min.",
"slug":"lower-than-30-min",
"taxonomy":"category",
"parent":2,"meta":[],
"acf":[],
"_links":{"self":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/3"}],
"collection":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories"}],
"about":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/taxonomies\/category"}],
"up":[{"embeddable":true,"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/2"}],
"wp:post_type":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/posts?categories=3"},{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/activities?categories=3"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}
}
Can't find any solution on the internet how I could manipulate the structure of that JSON with a custom function, would appreciate it a lot if someone point me to the right direction. Thanks!
As discussed in the comments section, a solution to this question is to use a custom endpoint method for the WP REST API and perform extra queries in there to get the data you need. This way, you can do all the data manipulation to get the perfect response, resulting in one REST call.
As taken from the official developer docs
Define an endpoint method and add some extra data
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func', //note that this is the method it will fire
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
) );
/**
* Grab latest post by an author along with its category!
*
* #param array $data Options for the function.
* #return array Post,
*/
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if(!empty($posts)) {
//Example of appending extra data
foreach($posts as $post) {
$category = wp_get_post_terms($post->ID, 'category');
$post['category'] = $category;
}
return $posts;
} else {
return new WP_Error( 'no_author', 'Invalid author', array( 'status' => 404 ) );
}
}
I am trying to show the purchase note beneath the product in the customer_processing_order email that Woocommerce generates.
I have added the following to my functions.php file:
function sww_add_images_woocommerce_emails( $output, $order ) {
// set a flag so we don't recursively call this filter
static $run = 0;
// if we've already run this filter, bail out
if ( $run ) {
return $output;
}
$args = array(
'show_purchase_note' => true,
);
// increment our flag so we don't run again
$run++;
// if first run, give WooComm our updated table
return $order->email_order_items_table( $args );
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_images_woocommerce_emails', 10, 2 );
This works, however it is printing an error message in the email stating the following:
"Notice: WC_Order::email_order_items_table is deprecated since version
3.0! Use wc_get_email_order_items instead. in /nas/content/staging/ishgamultisite/wp-includes/functions.php on line
3853"
if I change woocommerce_email_order_items_table to wc_get_email_order_items the function doesn't work.
I'm hoping someone can tell me how I should modify the code as I'm not sure?
Bit late but if anybody is still needing to show purchase notes, then there's an easier hook:
/**
* PURCHASE NOTE
* Edits the email order items args to show purchase notes
*/
function ag_add_wc_order_email_purchase_notes( $args ) {
$args['show_purchase_note'] = true;
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'ag_add_wc_order_email_purchase_notes', 10, 1 );
The filter woocommerce_email_order_items_args contains the arguments for what to display in the order emails.
$array — Optional. (callback) => array( 'order' => $order, 'items' => $order->get_items(), 'show_download_links' => $order->is_download_permitted() && ! $args['sent_to_admin'], 'show_sku' => $args['show_sku'], 'show_purchase_note' => $order->is_paid() && ! $args['sent_to_admin'], 'show_image' => $args['show_image'], 'image_size' => $args['image_size'], 'plain_text' => $args['plain_text'], 'sent_to_admin' => $args['sent_to_admin'], )
Source: http://hookr.io/filters/woocommerce_email_order_items_args/
Tested with WooCommerce 3.6.2 and works fine.
replace return $order->email_order_items_table( $args );
with
return wc_get_email_order_items( $order, $args );
I keep trying to setup a notification when a form is submitted but I'm not getting the emails. When I go back in to edit the notification, it is not there. It's like it is not saving that. Then I noticed this on the notifications page: function WP_List_Table::get_columns() must be over-ridden in a sub-class. Any idea what that means?
Note: It is saving the submissions in the entries area of the plugin.
There is a simple fix for this without upgrading Gravity Forms, but you'd have to edit a plugin file for Gravity Forms.
in notification.php, in the class
GFNotificationTable extends WP_List_Table {
...
Add this method:
function get_columns() {
$columns = array(
'name' => 'Name',
'subject' => 'Subject'
);
return $columns;
}
The same solution can be applied to any plugin where you're seeing this problem. The columns array just needs to match the names set as $this->_column_headers.
Adding to the previous answer, to fully fix the problem you'll need to also place that same function:
function get_columns() {
$columns = array(
'name' => 'Name',
'subject' => 'Subject'
);
return $columns;
}
In the GF form_settings.php file under the class GFConfirmationTable extends WP_List_Table.
The first fixes the Notifications error and this fixes the Confirmations error.
I figured it out. Once I put the license key into the settings, I was able to download the update. Installed and the error went away.
You also have to add it to:
class GFAddOnFeedsTable extends WP_List_Table of file "class-gf-feed-addon.php" in includes/addons folder in order for add ons to work.
function get_columns() {
$columns = array(
'name' => 'Name',
'subject' => 'Subject'
);
return $columns;
}
You have to try this code inside wp-admin/includes/class-wp-list-table.php
Copy and paste this code inside the function public function get_columns() at line 872.
$columns = array(
'name' => 'Name',
'subject' => 'Subject'
);
return $columns;
I have tried this code snippet and it worked!
function get_columns() {
$columns = array(
'name' => 'Name',
'subject' => 'Subject'
);
return $columns;
}
Basically I want to create a custom handler to unserialize a db field called birthday.
I've managed to correctly output the field serialized using the default views_handler_field. Unfortunately When I try to create a custom handler, I get this message:
Error: handler for drappsprofiles > birthday doesn't exist!
Here's the file structure:
all/modules/drapps/drappsprofile/
|->drappsprofiles.views.inc
|->drappsprofiles.module
|->drappsprofiles.install
|->drappsprofiles.info
|->drappsprofiles.inc
|->drappsprofiles_handler_field_birthday.inc
here's drappsprofiles.module
/**
* VIEWS2 MODULE
* Implementation hook_views_api
**/
function drappsprofiles_views_api() {
$info['api'] = 2;
return $info;
}
/*****************************************************************************
* INCLUDES
**/
// Loads Google Apps Profile Integration
module_load_include('inc', 'drappsprofiles');
(...)
here's drappsprofiles.views.inc
/**
*
* Implementation of hook_views_handlers().
*
**/
function drappsprofiles_views_handlers() {
return array(
'handlers' => array(
'drappsprofiles_handler_field_birthday' => array(
'parent' => 'views_handler_field',
)
)
);
}
/**
* Implementation of hook_views_data().
*
* #return array
**/
function drappsprofiles_views_data() {
(...)
$data['drappsprofiles']['birthday'] = array(
'title' => t('Birthday'),
'help' => t('Users birthday'),
'field' => array(
'handler' => 'drappsprofiles_handler_field_birthday',
'click sortable' => FALSE,
),
);
return $data;
}
drappsprofiles_handler_field_birthday.inc
<?php
/**
*
* Custom views handler for Birthday
*
*/
class drappsprofiles_handler_field_birthday extends views_handler_field {
function render($values) {
$val = unserialize($values->{$this->field_alias});
return ($val);
}
}
It seems that drappsprofiles_handler_field_birthday.inc is not being read, although I can't figure out why.
Any help would be appreciated. (I've been around this for 2 weeks!)
Assuming your (...) in .views.inc conceals code like:
$data['drappsprofiles']['table']['group'] = t('drappsprofiles');
$data['drappsprofiles']['table']['base'] = array(
'field' => 'birthday',
);
$data['drappsprofiles']['table']['join'] = array(
'#global' => array(),
);
Which I am assuming it does since your field has a group from which to select it so that it can look for the missing handler..
Then the next thing to look at is still in .views.inc in module_views_handlers() { :
return array(
++ 'info' => array(
++ 'path' => drupal_get_path('module','drappsprofilse'),
++ ),
'handlers' => array(
And beyond that, I hate to say it, but uninstalling and reinstalling the module apparently refreshes recent code tweaks to the .views.inc .. I know I had to a bunch of times, you probably noticed this too.