accessing shipping rates in order complete hook - wordpress

The client has very specific shipping requirements so I built a shipping plugin that works perfectly. But I need to pull the chosen shipping info in a specific format to send a purchase order to a 3rd party provider.
In the shipping plugin I store these values as follows:
$rate1cfree = array(
'id' => 'shipping_method_courier_free',
'label' => 'Courier (free)',
'cost' => '0',
'calc_tax' => 'per_item',
'meta_data' => array(
'delivery_channel' => 'NAT',
'delivery_service' => 'EXP'
)
);
$this->add_rate( $rate1cfree );
The 2 meta values that I need in the order complete hook is 'delivery_channel' and 'delivery_service'.
Now in my woocommerce_payment_complete function I can pull most of the order data using:
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
The problem is that the meta values comes through as follows:
[shipping_lines] => Array
(
[358] => WC_Order_Item_Shipping Object
(
[meta_data:protected] => Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 2515
[key] => delivery_channel
[value] => NAT
)
[data:protected] => Array
(
[id] => 2515
[key] => delivery_channel
[value] => NAT
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 2516
[key] => delivery_service
[value] => EXP
)
[data:protected] => Array
(
[id] => 2516
[key] => delivery_service
[value] => EXP
)
)
)
)
)
And no matter which ever methods I try and use I can't seem to access those values. I've tries using get_post_meta(), get_meta_data(), get_meta() and pretty much anything that looks like it could help on this page: https://docs.woocommerce.com/wc-apidocs/class-WC_Data.html
Any help would be greatly appreciated as I'm completely stuck. :)

OK, my co-working hero solved it as follows:
$order = wc_get_order( $order_id );
foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
$shipping_item_data = $shipping_item_obj->get_data();
$ship_info = $shipping_item_data['meta_data'];
foreach($ship_info as $ship_key => $ship_value){
$ship[$ship_key] = $ship_value->get_data();
}
}
print_r($ship);

Related

How can I filter records in custom post type list in admin based on user_id that passed as a query string parameter

Is there any option that I can further filter post list for a custom post type in admin based on user_id if the same has been passed as a query string like http://album.multibaselocal.com/wp-admin/edit.php?post_type=albums&user_id=31 ?
Here is what I want to accomplish:
I have a custom post type called album which is used by site users to upload Albums to the system. By default when I go to the list page all albums come up fine. In user list page, I have added a custom column which shows a count of Published albums by each user which links back to album list page with a query string user_id like above.
I want to filter the albums by the user_id I have passed. Is this possible?
I am trying this:
add_filter( 'parse_query', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$requery = array(
'post_type' => 'albums',
'post_author' => $user_id,
'status' => 'publish',
'posts_per_page' => -1,
);
// What to do here?
}
}
I have lost here completely. Not an experienced WP developer!
If I have not understood it all wrong, because the hook I am using is called parse_query, I have to somehow parse the query again and filter the existing result using user_id. But don't know where and how.
EDIT (Query output):
WP_Query Object
(
[query] => Array
(
[post_type] => albums
[posts_per_page] => 20
)
[query_vars] => Array
(
[post_type] => albums
[posts_per_page] => 20
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] => user_id
[meta_value] => 31
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] => 1
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] => 1
[query_vars_hash:WP_Query:private] => 1c0b485390fc9607d8008a14177bd63d
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
EDIT (Function body presently using):
add_action( 'pre_get_posts', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if(!is_admin())
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
$query->set('meta_key', 'user_id');
$query->set('meta_value', $user_id);
}
echo '<pre>'; print_r($query); echo '</pre>'; die();
}
This reminded me of a similar problem I had a couple weeks ago. I was wanting to adjust the 'orderby' part of the query, but I believe you can accomplish your task with the same action. Try using the 'pre_get_posts' action instead of that filter. I haven't tested this, so let me know if it works!
CORRECT ANSWER (EDITED after user input):
function user_id_filter( $query ) {
if ( ! is_admin() && !$query->is_main_query() )
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$query->set( 'author', $user_id );
}
//Debug if necessary
//echo '<pre>'; print_r($query); echo '</pre>'; die();
}
add_action( 'pre_get_posts', 'user_id_filter', 500000000 );
I used this as a reference. https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Schedule email using wpMandrill to Wordpress

Is that possible to schedule a message using wpMandrill plugin?
I saw in this post that we can use mandrill_payload filter to change anything in the structure (following Mandrill's API , /messages/send).
How can I change the send_at parameter so that I can schedule e-mails to be sent.
Would it be something like this:
function customFilterSendAt($send_at)
{
$send_at = "2016-01-23 14:00:00";
return $send_at;
}
add_filter('mandrill_payload', 'customFilterSendAt');
Then
wp_mail($email_adress, $subj, $body );
?
I found that it is possible to schedule emails using wpMandrill. This link (check aaroneight comments) helped me.
To schedule your email using wpMandrill:
$message = array(
'subject' => $subj,
'from_name' => 'From Name',
'from_email' => 'from_email#example.com',
'to' => 'to_email#example.com',
'html' => $body,
'async' => false,
'ip_pool' => null,
'send_at' => '2016-02-24 19:45:00'
);
$sendmessage = wpMandrill::sendEmail($message);
Debugging:
echo '<pre>'.print_r($sendmessage,true).'</pre>';
Output example:
Array
(
[0] => Array
(
[email] => to_email#example.com
[status] => scheduled
[_id] => db835dfe43cd5d67b3743a30e184f84d
[reject_reason] =>
)
)
At this time, scheduled emails may only be managed via Mandrill's API, so you won't find them within Mandrill's Dashboard.
to list your scheduled emails you can use:
$url = 'https://mandrillapp.com/api/1.0/messages/list-scheduled.json';
$key = 'Your API key';
//$to = ''; // optional
$args = array(
'body' => array(
'key' => $key
)
);
$results = wp_remote_post( $url, $args );
$results = json_decode($results['body']);
Output example:
Array
(
[0] => stdClass Object
(
[_id] => 1d0xe54f3b759a1153b7a53g3321f4b6
[created_at] => 2016-02-24 19:30:13
[send_at] => 2016-02-24 19:42:00
[from_email] => from_email#example.com
[to] => to_email#example.com
[subject] => Email Subject
)
[1] => stdClass Object
(
[_id] => 1272e526f6924ba096d23146e2dxad4c
[created_at] => 2016-02-24 19:31:12
[send_at] => 2016-02-24 19:45:00
[from_email] => from_email#example.com
[to] => to_email#example.com
[subject] => Email Subject
)
)

As I read (foreach php) this array stdClass

I have the following code which gives me a query generated , what I need it is to walk the array ( php foreach ) to query the data.
I tried to do with the classic foreach (php) but I foreach throws an error.
Thanks in advance...
Array (
[0] => stdClass Object
(
[_new] =>
[_attributes] => Array
(
[id] => 36
[nombre] => Diana
[estado] => 1
)
[_related] => Array
(
)
[_c] =>
[_pk] => 36
[_alias] => t
[_errors] => Array
(
)
[_validators] =>
[_scenario] => update
[_e] =>
[_m] =>
)
[1] => stdClass Object
(
[_new] =>
[_attributes] => Array
(
[id] => 1
[geoCode] => MAX
)
[_related] => Array
(
)
[_c] =>
[_pk] => 1
[_alias] => t
[_errors] => Array
(
)
[_validators] =>
[_scenario] => update
[_e] =>
[_m] =>
))
As I walk or convert to be read as ?
<?php foreach ($response as $value) {echo $value->nombre;} ?>

Woocommerce - Try to merge product data with data from custom tables

i have several custom tables and i'm using advanced custom fields (acf) with twig. These fields stored in the custom tables.
for example:
custom_table1, custom_table2, custom_table1_table2_relation and more of this.
They can be edited in the admin product panel and displays as objects in in relation with the product data.
Now i will merge those to an associative array.
i have this:
/*output*/
[post] => TimberPost Object (
[ImageClass] => TimberImage
[PostClass] => TimberPost
[object_type] => post
[class] => post-8 product type-product
[id] => 1
[post_author] => 1
[post_content] => description
[custom] => Array (
[table1-field1] => data
[table1-field2] => data
[table2_field1] => data
[table2_field] => data
)
)
and i like to have this:
$products['products'] = array(
'id' => 1,
'post_content' => 'description',
'post_title' => 'title',
'and_so_on' => 'stuff',
[ 'custom' ] => array(
'table1' => array(
'data1' => 'content',
'data2' => 'content',
),
'table2' => array(
'data1' => 'content',
'data2' => 'content',
) /*and so on*/
)
);
I've tried to extended a WC_Class and creating a action to call these in the fronted
this is just an snippet example
class WCIntegrationProductIntegration extends WC_Query {
function __construct() {
add_action( 'woocommerce_custom_product_data', array( $this->_get_custom_product_data ) );
}
public function _get_custom_product_data() {
global $woocommerce;
$custom = array_merge(
array(
/*get custom tables and data*/
'table' = $table1,
'data' array($fields),
)
);
$product = array_merge(
array(
/*get productss and data*/
'table' = $table1,
'data' array($fields),
)
);
$the_query = new WP_Query();
}
}
new WCIntegrationProductIntegration();
But i don't get it. I'm a bit in a mess
I dont know if extending WC classes is necessary, adding custom data to a WP_Post object is simple with the_posts filter.
Raw example:
add_filter( 'the_posts', function( $posts ) {
$posts[0]->custom = [ 'table1' => [] ];
return $posts;
});
add_action( 'the_post', function( $post ) {
global $product;
print_r( $post );
print_r( $product );
});
Output:
/*
WP_Post Object
(
[ID] => 99
[post_author] => 1
...
[custom] => Array
(
[table1] => Array
...
)
WC_Product_Simple Object
(
[id] => 99
[post] => WP_Post Object
(
[ID] => 99
[post_author] => 1
...
[custom] => Array
(
[table1] => Array
...
*/
I bet that WooCommerce also has its own filter for that.

Single Meta key with multiple values not working in searching

I have search query where i am using single meta_key with multiple values but its not working . here is the meta query .
[meta_query] => Array
(
[0] => Array
(
[key] => _vital_lighting_color
[value] => Array
(
[0] => red
[1] => green
[2] => blue
)
[type] => char
[compare] => IN
)
)
but its not fetching record i don't what's happening can you please check it .
Thanks
Using proper php syntax would help :
$query_args = array(
'meta_query' => array(
'key' => '_vital_lighting_color' ,
'value' => array
('red' , 'green' ,'blue' ) ,
'compare' => 'IN'
)
) ;
I removed type, because "char' is the default value
If it does not work, then run a query on the database directly to check you have posts

Resources