I saw in this question that is possible create a shortcode from my-orders page, I am trying create something similar to display the edit account page via shortcodes.
Reference: in woocommerce, is there a shortcode/page to view all orders?
function shortcode_my_orders( $atts ) {
extract( shortcode_atts( array(
'order_count' => -1
), $atts ) );
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', get_current_user_id() ),
'order_count' => $order_count
) );
return ob_get_clean();
}
add_shortcode('my_orders', 'shortcode_my_orders');
I created this shortcode to add the HTML contents of the Edit Account page in another page. I believe that's what you are asking for.
// Paste this in the function.php file of your active child theme or theme.
function wc_customer_edit_account_html_shortcode( $atts ) {
// Attributes
extract( shortcode_atts( array(
'text' => 'Edit Account' ), $atts ) );
return wc_get_template_html( 'myaccount/form-edit-account.php', array( 'user' => get_user_by( 'id', get_current_user_id() ) ) );;
}
add_shortcode( 'wc_customer_edit_account_html', 'wc_customer_edit_account_html_shortcode' );
You can also put this in a New Snippet in the Snippets plugin instead of editing the functions.php page.
You can display the edit account form, wherever you want, with this code:
function clket_edit_account_form(){
ob_start();
wc_get_template( 'myaccount/form-edit-account.php', array( 'user' => get_user_by( 'id', get_current_user_id() ) ) );
return ob_get_clean();
}
add_shortcode('clket_edit_account', 'clket_edit_account_form');
Shortcode: [clket_edit_account]
Ref: https://woocommerce.wp-a2z.org/oik_api/wc_shortcode_my_accountedit_account/
Related
I am working on a new plugin. I am dealing with a problem that I have outlined in the title. My intention is to redirect the user to the My Pages page after the user clicks the "Delete page" button.
Here is my code:
function custom_admin_bar_delete_link( $wp_admin_bar ) {
global $post;
if( is_admin() || ! is_object( $post ) )
return;
if ( ! current_user_can( 'delete_pages' ) )
return;
if ( $post->post_type != 'page' )
return;
$args = array(
'id' => 'delete_link',
'title' => 'Delete this page',
'href' => get_delete_post_link( $post->ID ),
'meta' => array( 'class' => 'delete-link' )
);
$wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'custom_admin_bar_delete_link', 999 );
function custom_page_delete_redirect( $location, $post_id ) {
$post = get_post( $post_id );
if ( 'page' === $post->post_type && 'trash' === get_post_status( $post_id ) ) {
return admin_url( 'edit.php?post_type=page' );
}
return $location;
}
add_filter( 'wp_redirect', 'custom_page_delete_redirect', 10, 2 );
Thank you.
Without thinking too hard about it, I would add an ID to your button and just write some jQuery to do the redirect.
(The PHP: NOT tested)
EDIT:
Per the Wordpress documentation, you should be able to add an ID:
https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/
$args = array(
'id' => 'delete_link',
'title' => 'Delete this page',
'href' => get_delete_post_link( $post->ID ),
'id' => "someid",
'meta' => array( 'class' => 'delete-link' )
);
The jQuery:
EDIT:
How are you adding the jQuery? You should probably save it in a separate file in your plugin folder (maybe in a subfolder named "js") and enqueue it in your plugin. The 2nd answer to this question would be a good place to start:
https://wordpress.stackexchange.com/questions/42641/how-to-include-a-simple-jquery-file-into-a-wordpress-plugin
jQuery(document).ready(function($){
$('body').on('click','#someid',function(event){
// window.alert('jquery executing');
setTimeout(function(){window.location.href = "http://example.com/page/";}, 500);
});
});
Thoughts: I added a timeout, because Wordpress loads slowly sometimes, and I've had to do this for other applications in Wordpress, but try without and see if it performs without the timeout.
Have a look at this answer:
https://wordpress.stackexchange.com/questions/132196/get-delete-post-link-redirect
The question is similar, and this may get you where you need to go.
I'm trying to write a shortcode to display users woocommerce order history.
I've found an answer here in woocommerce, is there a shortcode/page to view all orders? , but that does'nt work anymore.
If i follow the current answer it gives me a fatal error.
Fatal error: Call to undefined function wc_get_account_orders_actions() in /wp-content/themes/wrapgate/woocommerce/myaccount/my-orders.php on line 72
Anybody knows updated code to get my shortcode to work?
Here's the shortcode function i've tried
add_shortcode( 'woocommerce_history', 'woo_order_history' );
function woo_order_history() {
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', get_current_user_id() ),
'order_count' => -1
));
return ob_get_clean();
}
Same error occurs if i try to use
woocommerce_account_orders( -1 );
Woocommerce as well as wordpress are on the latest version.
I've tried to call the shortcode function from my themes functions.php
Thanks in advance for every help.
my-orders.php is deprecated after version 2.6.0. Woocommerce my-account uses orders.php now. To create a shortcode to display order history
function woo_order_history( $atts ) {
extract( shortcode_atts( array(
'order_count' => -1
), $atts ) );
ob_start();
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
) ) );
wc_get_template(
'myaccount/orders.php',
array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total,
)
);
return ob_get_clean();
}
add_shortcode('woocommerce_history', 'woo_order_history');
Add this code to your theme->functions.php or child-theme->functions.php (if you have child theme enabled).
Now where you want to display the order just add the shortcode [woocommerce_history]
I am having some trouble customizing the woocommerce templates in my wordpress theme. I would like to add additional data as variables in my templates.
I want to show active orders on the dashboard/my-account page. I want to do this by passing in order data variables to the template to be able to call, like how it is done in the orders.php template.
I know I can override the wc-template-functions.php in my theme and then add the data in the wc_get_templates function for the dashboard or my account. However, I don't want to do this.
What I've tried is creating a hook such as:
functions.php
function wc_fr_add_orders_to_account( $fr_account_orders, $current_page ) {
global $fr_account_orders;
$current_page = empty( $current_page ) ? 1 : absint( $current_page );
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query',
array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
'status' => array( 'wc-pending' )
) ) );
$fr_account_orders = array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total
);
return $fr_account_orders;
}
add_action( 'woocommerce_account_content', 'wc_fr_add_orders_to_account' );
/theme-directory/woocommerce/templates/myaccount/dashboard.php (also tried in my-account.php)
do_action( 'woocommerce_account_dashboard', $fr_account_orders);
var_dump($fr_account_orders);
$fr_account_orders comes back null. However if I var_dump the array in the hook function, it comes back with data. Any help is appreciated.
Eaasy there. If you want to return the variable, that's just not the way to do it. You should use the apply_filters like so:
function wc_fr_add_orders_to_account() {
/* your function */
return $fr_account_orders;
}
add_filter( 'woocommerce_account_dashboard', 'wc_fr_add_orders_to_account' );
and in your template..
$my_var = apply_filters( 'woocommerce_account_dashboard', $fr_account_orders );
var_dump( $my_var );
now if you want to send some variables do it like so:
function wc_fr_add_orders_to_account( $var1, $var2 ) {
/* your function */
return $fr_account_orders;
}
add_filter( 'woocommerce_account_dashboard', 'wc_fr_add_orders_to_account', 10, 3 );
and in your template again..
$my_var = apply_filters( 'woocommerce_account_dashboard', $fr_account_orders, $var1, $var2 );
var_dump( $my_var );
read more about apply_filters here https://developer.wordpress.org/reference/functions/apply_filters/ one more thing, try not to change templates, but use add_action on the do_action hooks from template for better compatibility. thanks!
I tried several ways and couldn't figure out how to keep the pagination correct. This way lists all of the orders on my dashboard.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array ('wc-pending'),
);
$customer_waiting_orders = new WP_Query( $args );
if ( $customer_available_orders->have_posts() ) :
while ( $customer_available_orders->have_posts() ) : $customer_available_orders->the_post();
//code here
wp_reset_postdata();
endwhile;
endif;
This other Stack Overflow question is exactly what I need, but the answer on it is a link that is 5+ years outdated.
It tells me to change the following code:
public function enqueue_admin_scripts() {
wp_enqueue_script("featured-image-custom", plugins_url(basename(dirname(__FILE__)) . '/js/multi-post-thumbnails-admin.js'), array('jquery'));
}
to this:
public function enqueue_admin_scripts() {
$template_url = get_bloginfo('template_url') . '/js/multi-post-thumbnails-admin.js';
wp_enqueue_script("featured-image-custom", $template_url, array('jquery'));
}
but the updated plugin now has this instead:
public function enqueue_admin_scripts( $hook ) {
global $wp_version, $post_ID;
// only load on select pages
if ( ! in_array( $hook, array( 'post-new.php', 'post.php', 'media-upload-popup' ) ) )
return;
if (version_compare($wp_version, '3.5', '<')) {
add_thickbox();
wp_enqueue_script( "mpt-featured-image", $this->plugins_url( 'js/multi-post-thumbnails-admin.js', __FILE__ ), array( 'jquery', 'media-upload' ) );
} else { // 3.5+ media modal
wp_enqueue_media( array( 'post' => ( $post_ID ? $post_ID : null ) ) );
wp_enqueue_script( "mpt-featured-image", $this->plugins_url( 'js/multi-post-thumbnails-admin.js', __FILE__ ), array( 'jquery', 'set-post-thumbnail' ) );
wp_enqueue_script( "mpt-featured-image-modal", $this->plugins_url( 'js/media-modal.js', __FILE__ ), array( 'jquery', 'media-models' ) );
}
wp_enqueue_style( "mpt-admin-css", $this->plugins_url( 'css/multi-post-thumbnails-admin.css', __FILE__ ) );
}
I've tried leaving it as is, changing all of them like the blog link says, and adding the $template_url variable every time is says $this. In the back end of WordPress, I see the 2nd featured image, but the only thing it does is add a number sign (#) to the editor's url.
Here's also my code added to the functions.php file:
// Load external file to add support for MultiPostThumbnails. Allows you to set more than one "feature image" per post.
require_once('library/multi-post-thumbnails.php');
// Define additional "post thumbnails". Relies on MultiPostThumbnails to work
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => '2nd Feature Image',
'id' => 'feature-image-2',
'post_type' => 'page'
)
);
new MultiPostThumbnails(array(
'label' => '3rd Feature Image',
'id' => 'feature-image-3',
'post_type' => 'home'
)
);
};
Please and thank you for your time.
I am trying to create a 'Pie Chart' shortcode to use in WP. All works fine apart from the percentage number. If it is entered into the array it works fine, but if I remove that number (ie; 100 - as seen in the code below), any number that is entered on the front-end by the user returns empty?? Quite puzzling?
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data_percentage' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data_percentage .'"><span>'.$data_percentage.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );
And this is the shortcode that needs to be entered on the front-end -
[piechart_inner data-percent="45" title="WordPress"][/piechart_inner]
Which outputs nothing for the data-percent, whatever value is entered?
Many thanks
You are using the wrong variable. You are giving data-percent when you have variable data_percentage
Your shortcode should look like this:
[piechart_inner data_percentage="45" title="WordPress"][/piechart_inner]
Or change the function to following:
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data-percent' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data-percent .'"><span>'.$data-percent.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );