WooCommerce code - Dynamically add a surcharge to cart and checkout - wordpress

I've added 2 radio buttons to my WooCommerce cart. However I want to add a surcharge when one is slected but not the other, and I want it to update the check out data that is displayed.
I know I can do this to add the surcharge all time:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
$surcharge = 5.32;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
however how to I control it to add this or not based on which radio button I have selected?

The best way is with ajax (untested code):
<input type="radio" name"surcharge" value="5.32" class="surcharge" />
<script>
$(document).ready(function () {
$(".surcharge").click(function(){
$.ajax({
url: "where_is_your.php", // Name of PHP script
type: "POST",
data: {
action: $(this).val(), //here you get the value 5.32
}
}).done(function(data) {
alert( data.message ); //your success message if you want it
});
})
})
</script>
and your php:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
$surcharge = $_POST['action'];
function woocommerce_custom_surcharge() {
global $woocommerce;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}

<?php
/**
* WooCommerce Extra Feature
* --------------------------
*
* Add custom fee to cart automatically
*
*/
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' ); ?>
You can use "woocommerce_cart_calculate_fees" to add surcharge money with an amount to your theme, and plus AJAX as above will help you out.

Well I solved this by creating an AJAX to handle the buttons and then in the PHP callback issuing a WC->session->set to set a session variable. Upon return, I then issue a location.reload() which refreshes the page but does not clear the cache. That way when the functions.php are loaded again, inside the surcharge function I added as a action_add, I can query the session variable to see which of the radio buttons were clicked and add the appropriate cart->add_fee.

Related

WooCommerce - get shipping country for non registered user

Good morning everybody.
I need to implement a method to subtract shipping costs from the total in case the shipping destination is included in a specific array of values.
This is not a case of free shipping, because this costs will be added later for other reasons.
I cannot base my decision on the user country, for two reasons:
user can be not registered
user country and shipping country can be different.
I see that WooCommerce reload the order totals when I change billing/shipping country. I believe I need to intercept this kind of change an trigger an action to insert a new cart fee (a negative one, of course).
Well, how can I do that?
This is a part of my code
function delayShippingCosts(){
global $woocommerce;
$EUcountries = ['IT','AT','BE','BG','CY','HR','DK','EE','FI','FR','DE','GR','IE','LV','LT','LU','MT','NE','PL','PT','CZ','RO','SK','SI','ES','SE','HU'];
return in_array( $woocommerce->customer->get_country() , $EUcountries);
}
add_action( 'woocommerce_cart_calculate_fees', 'scc_detract_shipping_costs' );
function scc_detract_shipping_costs(){
global $woocommerce;
if(delayShippingCosts()){
$shippingCosts = WC()->cart->get_shipping_total() * -1;
if(current_user_can('administrator')) {
$woocommerce->cart->add_fee( 'Delayed shipping costs', $shippingCosts, true, 'standard' );
}
}
}
The problem is that now I'm looking to my customer data, and these are not dynamic (and void for unregisterd / unlogged users).
Any suggestions?
Thanks!!
EDIT
Almost OK
I managed to retrieve the shipping country from "woocommerce_checkout_update_order_review" hook, like that:
function action_woocommerce_checkout_update_order_review($posted_data) {
global $shipTo;
$data = array();
$vars = explode('&', $posted_data);
foreach ($vars as $k => $value){
$v = explode('=', urldecode($value));
$data[$v[0]] = $v[1];
}
WC()->cart->calculate_shipping();
$shipTo = $data['shipping_country'] ? $data['shipping_country'] : $data['billing_country'];
// REMOVE ALL NOTICES, IF PRESENTS...
wc_clear_notices();
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1);
add_action( 'woocommerce_cart_calculate_fees', 'scc_detract_shipping_costs' );
function scc_detract_shipping_costs(){
global $woocommerce;
... something ...
if(condition) {
wc_add_notice("info message", "error");
}
}
My problem is that the notice is not removed when "condition" in false.
I tried to call wc_remove_notices() both in woocommerce_cart_calculate_fees and woocommerce_checkout_update_order_review. No big difference! :(
Any hint?
The delayShippingCosts() function is not needed. You can get the list of European countries via the get_european_union_countries method of the WC_Countries class (unless you want to customize the list).
Also you are getting the country with the get_country() method of the WC_Customer class.
The WC_Customer::get_country function is deprecated since version 3.0.
You can get the country like this:
WC()->customer->get_shipping_country() the country of the shipping address
WC()->customer->get_billing_country() the country of the billing address
Finally, note that to apply the standard tax class for the fee you have to set the 4th parameter as an empty string instead of 'standard'. See here for more information.
TO GET THE COUNTRY FIELD FROM A USER NOT LOGGED IN WITH THE woocommerce_cart_calculate_fees HOOK
You can send an AJAX call to send the billing and shipping country value when the respective fields change in the checkout.
To make sure that the AJAX function is executed before the woocommerce_cart_calculate_fees hook it is necessary to remove the update_totals_on_change class from the billing and shipping country fields (to avoid the AJAX call being made to update the checkout) and update the checkout only after the call AJAX has been completed.
This method may take a few extra milliseconds/second to update the checkout because you have to wait for the AJAX call to create the option to complete.
See this answer for more details on how to submit an AJAX call in Wordpress.
Add the following code inside your active theme's functions.php:
// enqueue the script for the AJAX call
add_action('wp_enqueue_scripts', 'add_js_scripts');
function add_js_scripts(){
wp_enqueue_script( 'ajax-script', get_stylesheet_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
// update options with checkout country values
add_action( 'wp_ajax_nopriv_set_option_country', 'set_option_country' );
add_action( 'wp_ajax_set_option_country', 'set_option_country' );
function set_option_country() {
if ( isset( $_POST ) ) {
// get the countries valued in the checkout by the user (guest or logged in)
$countries = $_POST['countries'];
$billing_country = $countries['billing_country'];
$shipping_country = $countries['shipping_country'];
// update options
update_option( 'guest_billing_country', $billing_country );
update_option( 'guest_shipping_country', $shipping_country );
// returns the output as a response to the AJAX call
echo 'success';
}
// always die in functions echoing AJAX content
die();
}
Create a script.js file and add it inside your child theme (because I used get_stylesheet_directory_uri() instead of get_template_directory_uri()) in the directory: /child-theme/js/script.js:
jQuery(function($){
// disable AJAX update
$('#billing_country_field').removeClass('update_totals_on_change');
$('#shipping_country_field').removeClass('update_totals_on_change');
// when the country fields change
$('#billing_country, #shipping_country').change(function(){
var countries = {
'billing_country': $('#billing_country').val(),
'shipping_country': $('#shipping_country').val(),
};
$.ajax({
url: ajax_object.ajaxurl,
type : 'post',
data: {
'action': 'set_option_country',
'countries': countries
},
complete: function(){
// update checkout via AJAX
$(document.body).trigger('update_checkout');
},
success:function(data) {
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
});
The code has been tested and works.
So, the correct scc_detract_shipping_costs function will be:
add_action( 'woocommerce_cart_calculate_fees', 'scc_detract_shipping_costs' );
function scc_detract_shipping_costs(){
$countries = new WC_Countries();
// get the list of countries of the european union
$eu_countries = $countries->get_european_union_countries();
// get countries from checkout
$billing_country = get_option( 'guest_billing_country' );
$shipping_country = get_option( 'guest_shipping_country' );
// if the shipping country is part of the European Union
if ( in_array( $shipping_country, $eu_countries ) ) {
$shippingCosts = WC()->cart->get_shipping_total() * -1;
if ( current_user_can('administrator') ) {
WC()->cart->add_fee( 'Delayed shipping costs', $shippingCosts, true, '' );
}
}
}
The code has been tested and works. Add it to your active theme's functions.php.
TO GET THE COUNTRY FIELD FROM A USER NOT LOGGED IN WITH THE woocommerce_calculate_totals HOOK
add_action( 'woocommerce_calculate_totals', 'get_post_checkout_data' );
function get_post_checkout_data( $cart ) {
// get post data
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
if ( ! empty( $post_data ) ) {
$billing_country = $post_data['billing_country'];
$shipping_country = $post_data['shipping_country'];
// ...
}
}
Add the code in your active theme's functions.php.

Wordpress custom button/action custom post type

I have a custom post type called request created with Toolset plugin.
I need to add a button in these admin pages (only for this custom post type):
new request (post-new.php)
edit request (post.php)
When admin submit the button I need to send an email (no problem for this) with info about the post.
How can I add the button and a callback to submit?
One way to add new elements to the Post Edit screen is via metaboxes.
Please check the following code:
/**
* Adds a call-to-action metabox to the right side of the screen under the "Publish" box.
*/
function wp645397_add_cta_metabox() {
add_meta_box(
'wp645397_request_cta',
'Call-to-action title here', /* This is the title of the metabox */
'wp645397_request_cta_html',
'request', /* the request post type */
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'wp645397_add_cta_metabox' );
/**
* Output the HTML for the call-to-action metabox.
*/
function wp645397_request_cta_html() {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( 'request_send_post_details', 'request_cta_nonce' );
// Output the call-to-action button
?>
Call-to-action button text here
<script>
/**
* We'll handle the button via Ajax to avoid having to reload the screen.
*/
jQuery(function($){
// Handle button click
$( "#wp645397_request_cta #btn-call-to-action" ).on("click", function(e){
e.preventDefault();
// Get the security nonce
var nonce = $(this).parent().find("#request_cta_nonce").val();
// Send the data via AJAX
$.post(
ajaxurl,
{
action: 'send_request_email',
nonce: nonce,
postid: <?php echo $post->ID; ?>
},
function( response ){
// Do something after the data has been sent
if ( 'OK' == response ) {
alert( 'Info sent' );
}
else {
alert( 'Something went wrong, try again' );
}
}
);
});
});
</script>
<?php
}
/**
* Handles CTA AJAX.
*/
function wp645397_send_request_email(){
// Invalid nonce
if ( !isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'request_send_post_details' ) ) {
wp_die( 'error' );
}
$post_ID = $_POST['postid'];
// Get post data
$post = get_post( $post_ID );
// Get post title
$post_title = $post->post_title;
// Get custom field, etc
// #TODO
// Send e-mail with data
// #TODO
wp_die('OK');
}
add_action( 'wp_ajax_send_request_email', 'wp645397_send_request_email' );
What it does:
Adds a custom metabox right above the Publish box on the right.
When the user clicks on the "Call-to-action button text here" button (remember to rename it!), it'll send the ID of the current post (request) via Ajax to a function called wp645397_send_request_email, where you need to process the data and send the e-mail.
I added some comments in the code that should explain what's going on. If you have any questions don't hesitate to ask, ok?
I am not sure what options toolset plugin provides but if I were to do this programatically, I would do something like
$post_types = array('post', 'page');
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$fetch_post_types = get_post_types($args, $output, $operator);
foreach ($fetch_post_types as $key => $value) {
if ($value != 'vocabulary' && $value != 'augmentation') {
array_push($post_types, $value);
}
}
add_meta_box('email_sender_post_type_metabox', __('Email Options', 'seoaipdl'), 'email_sender_post_type_metabox', $post_types, 'side', 'high', null);
function email_sender_post_type_metabox() {
//Button HTML code here
}
add_action('save_post', 'save_meta_data_function', 10, 2);
function save_meta_data_function($id, $data) {
if (!current_user_can("edit_post", $id)) {
return $data;
}
if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
return $data;
}
\\$_POST variable will have your submit button here.
}
Alternatively You can add a button using jquery and trigger your email function using ajax on this button click.
To do so you will need to look into admin_enqueue_script and wp_localize_script

Woocommerce Product Default Description for All Products

Is this possible to add Woocommerce Product Default Description for All Products page. That should be image or Note text. And I want It will be put in above or bottom of the description text.
Thanks in advance
you can use hooks like
// define the woocommerce_single_product_summary callback function
function my_custom_action() {
echo '<p>This is my custom action function</p>';
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );
or
woocommerce_after_single_product_summary
// define the woocommerce_after_single_product_summary callback
function action_woocommerce_after_single_product_summary( $evolve_woocommerce_after_single_product_summary, $int ) {
// make action magic happen here...
};
// add the action
add_action( 'woocommerce_after_single_product_summary', 'action_woocommerce_after_single_product_summary', 10, 2 );
or woocommerce_before_single_product_summary
// define the woocommerce_before_single_product_summary callback
function action_woocommerce_before_single_product_summary( $woocommerce_show_product_sale_flash, $int ) {
// make action magic happen here...
};
// add the action
add_action( 'woocommerce_before_single_product_summary', 'action_woocommerce_before_single_product_summary', 10, 2 );
or you may also change direct on woo-commerce files.

Remove selected checkout fields with woocommerce depending on shipping method

Hello so I'm trying to figure out how to remove some billing fields using woocommerce checkout depending on the shipping method selected. So with this code I'm trying to unset the billing address, billing city, billing state and billing postcode when the customer selects local shipping but this code isn't working. Any help would be appreciated.
add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='local_pickup:1'; // Set the desired shipping method to hide the checkout field(s).
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['billing']['billing_address_1']); // Add/change filed name to be hide
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_postcode']);
}
return $fields;
}
Here's how I would go about solving this problem.
This will involve php, css, and javascript (jQuery).
PHP
add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
// change below for the method
$shipping_method ='local_pickup:1';
// change below for the list of fields
$hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
// uncomment below line and reload checkout page to check current $chosen_methods
// print_r($chosen_methods);
$chosen_shipping = $chosen_methods[0];
foreach($hide_fields as $field ) {
if ($chosen_shipping == $shipping_method) {
$fields['billing'][$field]['required'] = false;
$fields['billing'][$field]['class'][] = 'hide';
}
$fields['billing'][$field]['class'][] = 'billing-dynamic';
}
return $fields;
}
Instead of unsetting the fields, we will just alter it's requiredness.
That means, if the chosen method is the one we want to check, we will not make it required. Then we will add a hide class. With this, we can hide these fields using css. And woocommerce will not throw an error that it is required. Using jQuery, we can show/hide these fields. So if we unset it on the first run, there's nothing to show because the fields are not there in the first place and with that the page needs to reload.
So here's the javascript and the css part.
add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
if (is_checkout()) :
?>
<style>
.hide {display: none!important;}
</style>
<script>
jQuery( function( $ ) {
// woocommerce_params is required to continue, ensure the object exists
if ( typeof woocommerce_params === 'undefined' ) {
return false;
}
$(document).on( 'change', '#shipping_method input[type="radio"]', function() {
// change local_pickup:1 accordingly
$('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
});
});
</script>
<?php
endif;
}

Adding a hook to "Place order" button in woocommerce

When the user gets to the checkout, there is a button , the "Place order" button at the bottom of the form. I have been trying to add a hook to this button in woocommerce, but I don't seem to find the correct one, I have tried woocommerce_checkout_place_order... but it doesnt do anything.
function my_function() {
//write function
}
add_action( "woocommerce_order_status_pending", "my_function");
Thanks in advance!
You need this hook woocommerce_review_order_after_submit. It will execute any function you hook to it just after the submit area. with this hook you can add some html on the checkout page after the submit button.
But if you need to call a function after the user have pressed the "Place order" button - use woocommerce_checkout_order_processed. This one will hook you just after the order was created so you can use the freshly generated order details:
add_action( 'woocommerce_checkout_order_processed', 'is_express_delivery', 1, 1 );
function is_express_delivery( $order_id ){
$order = new WC_Order( $order_id );
//something else
}
You may check this site for some more hooks you might use on the checkout page.
## I USED THIS CODE FOR ADDING DELIVERY CHARGES DEPENDING UPON THE CART SUBTOTAL AND SOME POST FIELDS ##
function action_woocommerce_checkout_process($wccs_custom_checkout_field_pro_process )
{
global $woocommerce;
//Add Fuel Surcharge & CAF
function woo_add_cart_fee() {
global $woocommerce;
if ( WC()->cart->cart_contents_total < 1500 &&
$_POST['delivery_type']=='Pick Up') {
$fuel_surchargeandCAF = get_option( 'fuel_surchargeandCAF',
70 );
WC()->cart->add_fee( __('Delivery Charges', 'woocommerce'),
$fuel_surchargeandCAF, TRUE, '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
};
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10,
1 );

Resources