I am working with Woocommerce and I am about to make the order confirmation email.
It want the mail to say:
"Hi [customers name]"
How do you get Woocommerce to print the customers name?
You need the order object, so depending on what hook you are using it should be there. Try something like this:
add_action('woocommerce_order_status_completed','my_woo_email');
function my_woo_email($order_id){
$order = new WC_Order( $order_id );
$to = $order->billing_email;
$subject = 'this is my subject';
$message = 'Hi '.$order->billing_first_name.' '.$order->billing_email;$order->billing_last_name.', thanks for the order!';
woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/htmlrn", $attachments = "" )
}
This is untested but should get you started
As of WooCommerce 3.x the $order object's properties should not be accessed directly as suggested in the previous answers. The proper way is now:
echo 'Hi ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
Here's my starter example of getting a bunch of the WooCommerce customer info inside a WP_Query loop:
$args = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'posts_per_page' => -1,
'order' => 'ASC'
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$order = new WC_Order( $query->post->ID );
echo $order->billing_address_1;
echo $order->billing_address_2;
echo $order->billing_city;
echo $order->billing_company;
echo $order->billing_country;
echo $order->billing_email;
echo $order->billing_first_name;
echo $order->billing_last_name;
echo $order->billing_phone;
echo $order->billing_postcode;
echo $order->billing_state;
echo $order->cart_discount;
echo $order->cart_discount_tax;
echo $order->customer_ip_address;
echo $order->customer_user;
echo $order->customer_user_agent;
echo $order->order_currency;
echo $order->order_discount;
echo $order->order_key;
echo $order->order_shipping;
echo $order->order_shipping_tax;
echo $order->order_tax;
echo $order->order_total;
echo $order->payment_method;
echo $order->payment_method_title;
echo $order->shipping_address_1;
echo $order->shipping_address_2;
echo $order->shipping_city;
echo $order->shipping_company;
echo $order->shipping_country;
echo $order->shipping_first_name;
echo $order->shipping_last_name;
echo $order->shipping_method_title;
echo $order->shipping_postcode;
echo $order->shipping_state;
}
wp_reset_postdata();
Hopefully this will be helpful for someone else.
you can get customer data from $order object, something like this.
$customer = get_userdata( $order->get_customer_id() );
$name = $customer->display_name;
Related
I am curretly trying to create a shortcode that shows 3 products and the snippet looks like this:
function show_recent_products(){
$args = array('posts_per_page' => 3, 'post_type' => 'product');
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
echo '<div id="recent-posts" class="flex space-between">';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
echo '<div class="woocommerce_recent_products" style="background: url('.get_the_post_thumbnail_url().')">';
echo '<p>';
//echo get_regular_price();
echo'</p>';
echo '</div>';
}
echo '</div>';
}
wp_reset_postdata();
}
How can I get hold of the product price?
Reason why I am not using the default recent products shortcode it because the layout looks completely different and I would not know how to change the default code.
You can fetch product detail from ID. Use wc_get_product(). This will return Product Object.
$current_product = wc_get_product( get_the_ID() );
After that you can use this object $current_product and fetch relevant product information using its methods like get_price(). Example.
echo $current_product->get_price();
I used the global product inside the while loop and with I managed to get hold of all the product information.
function show_recent_products(){
global $post;
$args = array('posts_per_page' => 3, 'post_type' => 'product');
$post_id = $post->ID;
$product = wc_get_product( $post_id );
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
echo '<div id="recent-posts" class="flex space-between">';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
global $product;
echo '<a href="'. get_the_permalink() .'" class="woocommerce_recent_products flex align-center" style="background: url('.get_the_post_thumbnail_url().')">';
echo '<div class="recent-content-inner">';
echo '<div>';
echo '<p class="recent-add-title">';
echo get_the_title();
echo'</p>';
echo '<p class="recent-add-price">';
echo "R". $product->price;
echo'</p>';
echo '<p class="recent-add-cart">';
echo "ADD CART";
echo'</p>';
echo'</div>';
echo'</div>';
echo "</a>";
}
echo '</div>';
}
wp_reset_postdata();
}
I am trying to build a sitemap plugin ,but am stuck at a simple WordPress loop. Every time I try to get the URL, the site crash. Its the get_permalink that is making the loop and crashing the site. I have tested these and non of them works for me:
get_permalink
the_permalink
get_post_permalink
get_the_permalink
The loop:
function fa_sitemap_build() {
// Create/open the file
$file = fopen( get_template_directory() . '/sitemap.xml','wb');
$the_query = get_posts('post_type = page');
foreach ( $the_query as $post ) {
$title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$date = $post->post_modified;
$url .= '
<url>
<title>'.$title.'</title>
<url>'.$link.'</url>
<lastmod>'.$date.'</lastmod>
</url>
';
}
wp_reset_postdata();
$sitemap = '
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
'.$url.'
</urlset>';
// write content to the file
fwrite( $file, $sitemap );
fclose( $file ); // Close the file
}
The arguments passed to the get_posts() must be an array. Try the following code.
<?php
$args = array(
'post_type' => 'page',
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
echo $post->title;
echo '<br />';
echo $post->post_content;
echo '<br />';
echo get_permalink( $post->ID );
}
or you can use the following code as well, it works same.
$args = array(
'post_type' => 'page',
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post );
the_title();
echo '<br />';
the_content();
echo '<br />';
the_permalink();
}
wp_reset_postdata();
Reference:
get_posts() function on Codex
I am trying to use a WordPress User Query to create a list of users that is ordered by a custom meta value. Its a simple numeric value, going from 1 to 100, so 1 needs to be displayed first, 100 last, etc.
This is my attempt, which has failed miserably:
<?php
$args = array(
'role' => 'Author',
'meta_key' => 'order-number',
'orderby' => 'order-number',
'order' => 'asc',
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<div style="float:left;">';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<div class="post team-member"><div class="image">';
echo get_avatar( $author_info->ID, 91 );
echo '</div>';
echo '<div class="content"><div class="title">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</div>';
echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...';
echo '</div></div>';
}
echo '</div>';
} else {
echo 'No authors found';
}
?>
I think the problem is that wp_user_query does not support custom fields in orderby - so I need a solution that works around this.
Any ideas?
Correct. The orderby parameter can only accept possible values of 'login' (default), 'nicename', 'email', 'url', and 'registered'.
A dirty fix would be something like this:
<?php
global $wpdb; //Ignore this line if you're not within a function
$order = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key='order-number' ORDER BY meta_value ASC", "ARRAY_N");
$authors = array();
foreach($order as $aid)
$authors[] = new WP_User($aid[0]);
if (!empty($authors))
{
echo '<div style="float:left;">';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<div class="post team-member"><div class="image">';
echo get_avatar( $author_info->ID, 91 );
echo '</div>';
echo '<div class="content"><div class="title">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</div>';
echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...';
echo '</div></div>';
}
echo '</div>';
} else {
echo 'No authors found';
}
This is largely untested, so I can't guarantee this will work straight out of the gate, but it should get you started.
Hope this helps!
<?php
// prepare arguments this is your query.
$args = array(
'meta_key' => 'last_name',
'query_id' => 'wps_last_name',
);
// Create the WP_User_Query object
$author_query = new WP_User_Query( $args );
?>
Add following lines to functions.php file
<?php
add_action( 'pre_user_query', 'wps_pre_user_query' );
/*
* Modify the WP_User_Query appropriately
*
* Checks for the proper query to modify and changes the default user_login for $wpdb->usermeta.meta_value
*
* #param WP_User_Query Object $query User Query object before query is executed
*/
function wps_pre_user_query( &$query ) {
global $wpdb;
if ( isset( $query->query_vars['query_id'] ) && 'wps_last_name' == $query->query_vars['query_id'] )
$query->query_orderby = str_replace( 'user_login', "$wpdb->usermeta.meta_value", $query->query_orderby );
}
?>
I'm using a custom post type plugin that returns an uploaded file's attachment ID instead of its url.
I've been able to get the image to display using wp_get_attachment_image_src as outlined in the codex here http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src but my problem is getting it to play nicely with the code on the template page used to call the information from the custom post type.
Stripping it down to the basics, this is what calls the custom post type info from the template page:
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
echo '<img src="' . $slide['slide'] . '" />';
}
?>
I'm having difficulty reconciling this with what the codex provides:
<?php
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?>
<img src="<?php echo $image_attributes[0]; ?>">
It seems like something like the following should work, but I'm obviously missing something with the php syntax
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
echo '<img src="<?php echo $image_attributes[0]; ?>" />';
}
?>
Any thoughts would be appreciated, thanks.
I think this is what you want
if ( $post->post_type == 'slideshowplatform' && $post->post_status == 'publish' )
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
));
if ($attachments) {
foreach ($attachments as $attachment) {
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
//$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
//echo '<img src="' . $image_attributes[0] . '" />';
}
}
}
So I have a snippet of code that grabs the categories and their coinciding posts and lists them outside of the loop (Below). I've been trying to get the post to link to #post-[ID] instead of the permalink - but I keep failing. Can anyone help?
<ul id="sidebar">
<?php
foreach( get_categories('orderby=ID&order=desc') as $cat ) :
if( !$cat->parent ) {
echo '<li class="title"><h2>' . $cat->name . '</h2>';
echo '<ul>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
$id = $post->ID;
global $post;
if( $cat_posts ) :
foreach( $cat_posts as $menuPost ) :
echo '<li';
if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; }
echo '>';
echo '' . $menuPost->post_title . '';
echo '</li>';
endforeach;
endif;
echo '</ul></li>';
}
?>
The above code is outputting UL/LI tags like this:
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
Admittedly, I don't exactly understand what you mean by "linking to #post-[ID]", but going with the question title:
If you use get_permalink() when echoing the link, you will get the permalink - that's just what that function does.
Use get_the_ID() instead, if you want the post-ID returned, or the_ID() if you want it displayed (the_ID() is the same as echo get_the_ID()).
Edited from here:
If you're otherwise happy with the above code, changing
echo '' . $menuPost->post_title . '';
to
echo '' . $menuPost->post_title . '';
ought to do it.
However, I'd go about it like so:
echo '<ul>';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<li class="title"><h2>' . $category->name . '</h2><ul>';
// query posts of that category:
$query_args = array(
'cat' => $category->cat_ID,
'posts_per_page' => -1
);
$your_query = new WP_Query( $query_args );
// loop through them:
while ( $your_query->have_posts() ) : $your_query->the_post();
echo '<li><a href="#post-';
the_ID();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo '</ul></li>';
// Reset Post Data
wp_reset_postdata();
}
echo '</ul>';