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]
Related
To begin with I have created a simple plugin that contains this piece of code:
function woo_create_credit_attribute_taxonomy() {
$attributes = wc_get_attribute_taxonomies();
$slugs = wp_list_pluck( $attributes, 'remaining_credits' );
if ( ! in_array( 'remaining_creds', $slugs ) ) {
$args = array(
'slug' => 'remaining_creds',
'name' => __( 'Remaining Credits', 'bidrop-credits' ),
'type' => 'select',
'orderby' => 'menu_order',
'has_archives' => false,
);
$result = wc_create_attribute( $args );
}
}
// On Activation create the credit attribute taxonomy
add_action( 'admin_init', 'woo_create_credit_attribute_taxonomy' );
This piece of code, simply creates a new Woocommerce Product Attribute.
And in the uninstall.php file of the same plugin, I use the WPDB query below to remove it uppon uninstall.
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE 1");
Notes: The Attribute is succesfully being deleted from the Database, but remains visible on front-end and when I try deleting it from the front-end it seems to be stuck there, Until I deactive and Reactivate Woocommerce.
Any ways I could adjust this code to flush_rewrite the woocommerces rules?
I have already tried the cassual flush_rewrite_rules() built-in function from wordpress. But it has no better results.
Thanks in advance for your time and effort
I'm trying to add a product archive widget in Elementor but in this widget specifically must hide "Out of stock" products.
I try to use the following code`s but I have not succeeded, because they doesn't work if Elementor widget is AJAX.
If the widget is not AJAX, then it`s fine (they are working).
How to modify this code`s to work with AJAX?
1). With a meta query:
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
if( ! is_admin() ) {
$query_args['meta_query'][] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
}
return $query_args;
}
2). Or with a tax query:
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
if( ! is_admin() ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
'operator' => 'NOT IN'
);
}
return $query_args;
}
I found this codes from another stack question:
Answered by #LoicTheAztec
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/
I'm currently create a plugin that takes all comments from all products and places them under the review tab for all comments.
Inside Wordpress I have the following setting checked:
Settings > Discussion > Break comments into pages with...
This enables pagination on the default comment section. I've added this code with my code but it doesn't enable pagination, even when I remove the If statement.
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
) ) );
echo '</nav>';
endif; ?>
I'm currently using this function to call all reviews into one:
function products_all_reviews(){
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments );
}
What needs to be done to call/enable pagination properly? This is my first time creating plugin so the help is much appreciated
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;