I need to show the total amount of orders(my mean all pages) in the Woocommerce admin orders table But I couldn't find any action or hook for that. Is there any way to do this? You can see what I want in the Image below. Thanks.
This is exactly what we needed and came up with a simple JS solution that works well:
//ADD Total for current page on Admin Orders Page in column "total" header
add_action( 'admin_footer', 'ATW_order_total_admin_footer_tracking_js' );
function ATW_order_total_admin_footer_tracking_js() {
global $pagenow;
if ( $pagenow === 'edit.php' && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ) :
?>
<script type="text/javascript">
jQuery( function($){
var total_total = 0.00;
jQuery( "td.order_total.column-order_total > span" ).each(function( index ) {
var tt = parseFloat(jQuery(this).text().replace("$","").replace(",",""));
total_total=total_total+tt;
});
total_total = total_total.toFixed(2);
jQuery('table > tfoot > tr > th.manage-column.column-order_total').append(" $"+total_total);
jQuery('table > thead > tr > th.manage-column.column-order_total').append(" $"+total_total);
});
</script>
<?php
endif;
}
Related
I've created a specific method of payment to manage approval or decline of orders for specific products.
Once order was approved I set order in status "Waiting for payment" and send an email to customer with link to pay.
The problem is that for some payment method I have to apply a discount.
I'm able to apply discount before order was placed ( I use it for order that doesn't require approval), but I don't know how to recalcultate the total in this moment.
For do that I use this code:
add_action( 'woocommerce_cart_calculate_fees','payment_method_discount', 20, 1 );
function payment_method_discount( $cart_object ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
foreach($cart_object->cart_contents as $cart_item_id => $cart_item) {
$payment_method_arr = array('ppcp-gateway');
foreach($payment_method_arr as $payment_method) {
$percent = 25;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ) {
WC()->cart->add_fee( 'PayPal fee', 0 - $discount );
}
}
}
}
To trigger the update I used:
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods', 20, 1 );
function refresh_payment_methods() {
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
I've tried also to add the function refresh_payment_methods to wp_footer hook.
When a user is on my woocommerce store checkout, I want to set the tax to 0 if they choose a certain payment gateway like "paypal_pro", "cheque", "bacs", or in my case "wdc_woo_credits". Which is a woocredits plugin that allows users to pay with credits instead of a credit card.
I know this function sets the taxes correctly because when I print_r($cart_object) I see I set all the taxes to 0, but yet the checkout still applies the tax in the total.
I think I need a function that recalculates the taxes after I set it using the function below.
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//if( $chosen_payment_method == 'cheque' ){
if( $chosen_payment_method == 'wdc_woo_credits' ){
$cart_object->set_cart_contents_taxes(0);
$cart_object->set_cart_contents_tax(0);
$cart_object->set_subtotal_tax(0);
$cart_object->set_shipping_tax(0);
$cart_object->set_total_tax(0);
$cart_object->set_fee_tax(0);
$cart_object->set_fee_taxes(0);
$cart_object->set_subtotal_tax(0);
foreach($cart_object->cart_contents as $product){
$cart_object->cart_contents[$product['key']]['line_tax'] = 0;
$cart_object->cart_contents[$product['key']]['line_subtotal_tax'] = 0;
$cart_object->cart_contents[$product['key']]['line_tax_data']['subtotal'] = 0;
$cart_object->cart_contents[$product['key']]['line_tax_data']['total'] = 0;
}
}
//echo '<pre>'; print_r($cart_object); echo '</pre>';
}
This function detects what payment selection they choose and re-runs the update checkout function. But yet the tax is still in the total.
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
After lots of googling, i found out that I don't need to mess with the cart totals at all. I need to alter the current logged in customer.
I can do it with this function which removes tax for the current logged in user.
WC()->customer->set_is_vat_exempt( true );
all this goes in functions.php. You still need the woocommerce_review_order_before_payment function from above which triggers the ajax. So here's the complete code to hide tax on a certain payment gateway for only logged in users.
// calculate fees on checkout page
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//if( $chosen_payment_method == 'cheque' ){
if( $chosen_payment_method == 'wdc_woo_credits' ){
// if user buys with credits, dont allow tax.
WC()->customer->set_is_vat_exempt( true );
}else{
// if user buys with credit card, allow tax
WC()->customer->set_is_vat_exempt( false );
}
}
// refresh the checkout page totals once user selects
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
I have created custom checkout button in single product page. It is working fine.But after selected the variation with checkout button,it redirects to the checkout page with this error 'Please choose product options…'.
This is my code
function add_content_after_addtocart() {
global $woocommerce;
// get the current post/product ID
$current_product_id = get_the_ID();
// get the product based on the ID
$product = wc_get_product( $current_product_id );
// get the "Checkout Page" URL
$checkout_url = WC()->cart->get_checkout_url();
// run only on simple products
if( $product->is_type( 'variable' ) ){
?>
<script>
jQuery(function($) {
<?php /* if our custom button is clicked, append the string "&quantity=", and also the quantitiy number to the URL */ ?>
// if our custom button is clicked
$(".custom-checkout-btn").on("click", function() {
// get the value of the "href" attribute
$(this).attr("href", function() {
// return the "href" value + the string "&quantity=" + the current selected quantity number
return this.href + '&quantity=' + $('input.qty').val();
});
});
});
</script>
<?php
echo '<div class="col-sm-6"><div class="buy_now"><a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="single_add_to_cart_button buy_now_button button alt disabled custom-checkout-btn ajax_add_to_cart" >Buy Now</a></div></div><div class="clearfix"></div>';
?>
<?php
}
else if( $product->is_type( 'simple' ) ){
echo '</div><div class="col-sm-6"><div class="p-t-35"></div><div class="buy_now">Buy Now</div></div><div class="clearfix"></div>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
Please help me..
I had the same issue and I had contacted WooThemes support team and they said that
"We limit the amount of variations we show on the front end for speed.
But sometimes you need more than 36 variations, so we offer that
filter to override that limitation."
Please add this code below in the functions.php file.
function custom_wc_ajax_variation_threshold( $qty, $product )
{
return 100;
}
add_filter( 'woocommerce_ajax_variation_threshold', 'custom_wc_ajax_variation_threshold', 100, 2 );
I want to show error message if no category selected while adding a new post?
You can check it via jquery:
/* Checks if cat is selected when publish button is clicked */
jQuery( '#submitdiv' ).on( 'click', '#publish', function( e ) {
var $checked = jQuery( '#category-all li input:checked' );
//Checks if cat is selected
if( $checked.length <= 0 ) {
alert( "Please Select atleast one category" );
return false;
} else {
return true;
}
} );
and output error message as you want.
You can do that by using the admin_notices hook. Just add the following code in your functions.php file:
function wp_823232_admin_notice__no_category_error()
{
?>
<div class="notice notice-error is-dismissible">
<p><?php _e('Warning: No category is selected for this post!', 'sample-text-domain'); ?></p>
</div>
<?php
}
function wp_823232_check_category()
{
global $pagenow;
global $post;
if ($pagenow == 'post-new.php' || $pagenow == 'post.php') {
if (count(get_categories($post->ID)) == 1 && has_category("uncategorized", $post->ID)) {
add_action('admin_notices', 'wp_823232_admin_notice__no_category_error');
}
}
}
add_action('in_admin_header', 'wp_823232_check_category');
Is there a hook that would allow setting the Inventory > Manage Stock checkbox in woocommerce globally?
All my products are single item quantity, so it seems pretty counter-D.R.Y to always have to remember to check it, especially for other employees.
Although this is quite late, since you asked about a hook: although there doesn't appear to be a hook, action or function specifically for turning on the Manage Stock option, you can still hook into the post save function and auto-enable it that way, since it is just a post_meta value:
add_action('save_post', 'myWoo_savePost', 10, 2);
function myWoo_savePost($postID, $post) {
if (isset($post->post_type) && $post->post_type == 'product') {
update_post_meta($post->ID, '_manage_stock', 'yes');
}
}
Note that stock levels will always default to 0, so you may also want to add the line:
update_post_meta($post->ID, '_stock', '1');
...which will update your stock quantity to 1. Do be aware that this will happen every time a product is saved, though.
I'm unsure as to whether this has knock-on effects elsewhere in WooCommerce for larger stock quantities, but as you're dealing with single-quantity items, I'd guess you're probably okay.
Update (with $update):
As of Wordpress 3.7, a third parameter was added to save_post so you can easily tell if it's a new post being created or an existing post being updated. As such, you can fire the function above only when creating a new post (which is arguably the desired effect):
add_action('save_post_product', 'myWoo_savePost', 10, 3);
function myWoo_savePost($postID, $post, $update) {
if (!$update) {
// $update is false if we're creating a new post
update_post_meta($post->ID, '_manage_stock', 'yes');
update_post_meta($post->ID, '_stock', '1');
}
}
(Thanks to Dylan for the reminder about post-type specific saves)
A little late, but here's how for anyone else needing to do this... I found most of this code somewhere else, but I don't have the link anymore to where I found it. Tweaked it a little and this is what I ended up with (add to functions.php):
add_action( 'admin_enqueue_scripts', 'wc_default_variation_stock_quantity' );
function wc_default_variation_stock_quantity() {
global $pagenow, $woocommerce;
$default_stock_quantity = 1;
$screen = get_current_screen();
if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' ) && $screen->post_type == 'product' ) {
?>
<!-- uncomment this if jquery if it hasn't been included
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
-->
<script type="text/javascript">
jQuery(document).ready(function(){
if ( !jQuery( '#_manage_stock' ).attr('checked') ) {
jQuery( '#_manage_stock' ).attr('checked', 'checked');
}
if ( '' === jQuery( '#_stock' ).val() ) {
jQuery( '#_stock' ).val(<?php echo $default_stock_quantity; ?>);
}
});
</script>
<?php
}
}
Woocommerce now has it's own hook for saving a product. For a simple product it's woocommerce_process_product_meta_simple.
Before updating the meta, we should first check that the _manage_stock is empty i.e. the checkbox has not been checked, so that it only triggers on products which have not already been set.
Then toggle the manage stock and set the default stock number.
function manage_stock_default( $post_id ) {
if (empty($_POST['_manage_stock'])) {
update_post_meta($post_id, '_manage_stock', 'yes');
update_post_meta($post_id, '_stock', '1');
}
}
add_action( 'woocommerce_process_product_meta_simple', 'manage_stock_default');