Vignesh Pichamani Vignesh Pichamani - 4 months ago 27
PHP Question

Display updated data in checkout page of woocommerce

Question may be odd :(

I am trying to display some information about discount via shortcode in checkout page of woocommerce with hook of

woocommerce_before_checkout_form
If i applied the coupon in cart page and visit to checkout noticed that hook messages shows up correct value of discount, but if i remove/add from checkout nothing shows up in that
woocommerce_before_checkout_form
hook i tried by removing and add that hook but still shortcode value are not updating, i tested many times with following function can anyone have some work around ideas/suggestion would be great

<?php

/*
* Plugin Name: Hook Priority
*
*/

function add_my_shortcode() {
ob_start();
global $woocommerce;
echo $woocommerce->cart->discount_cart;
return ob_get_clean();
}

add_shortcode('my_shortcode', 'add_my_shortcode');

function add_message_in_checkout() {
var_dump(do_shortcode('[my_shortcode]'));
}

add_action('woocommerce_before_checkout_form', 'add_message_in_checkout');

function coupon_removed_function( $coupon_code ) {

remove_all_actions('woocommerce_before_checkout_form');
remove_shortcode('my_shortcode');
do_action('woocommerce_before_checkout_form');
}

add_action("woocommerce_removed_coupon", 'coupon_removed_function');


I tried with javascript to reload the page(refresh instance) and it is working, i don't want to use that one unless there is no native wordpress/php solutions to overcome.

echo "<script type='text/javascript'>location.reload();</script>";


What currently happen in my side is


$10 discount i added from cart, then in checkout i can able to see $10 value but
if i remove the coupon from checkout it still showing $10 but it should be $0.


Thanks in Advance.

Answer

You can use jQuery to achieve your result.

Approach :

WooCommerce fires a jQuery event updated_checkout nearly every time when anything happens on to the checkout page. So you can use that event to achieve your desired result.

Modification in your current code :

1) First of all add JS so that we can do jQuery coding in that JS File.

So after Modification your code will look something like this :

customPlugin.php

<?php

/*
 * Plugin Name: Hook Priority
 *
 */

function add_my_shortcode() {
    ob_start();
    global $woocommerce;
    echo $woocommerce->cart->discount_cart;
    return ob_get_clean();
}

add_shortcode('my_shortcode', 'add_my_shortcode');

function add_message_in_checkout() {
    //var_dump(do_shortcode('[my_shortcode]'));
    /*Modification*/
    echo '<div class="coupon_value">'.do_shortcode('[my_shortcode]').'</div>'; // Here I have modified it to give class and update value
}

add_action('woocommerce_before_checkout_form', 'add_message_in_checkout');

function coupon_removed_function( $coupon_code ) {

    remove_all_actions('woocommerce_before_checkout_form');
    remove_shortcode('my_shortcode');
    do_action('woocommerce_before_checkout_form');
}

add_action("woocommerce_removed_coupon", 'coupon_removed_function');

/*Modifications starts from here*/

/*Action to enqueue Jjavascript in Footer*/
add_action("wp_footer", 'enqueue_plugin_script');

function enqueue_plugin_script(){
    /*Enqueue Custom Javascript to use*/
    wp_enqueue_script( 'custom-script', plugin_dir_url( __FILE__ ).'custom.js', array( 'jquery' ), '1.0.0', true );
    /*Localize parameter to use in JS file*/
    wp_localize_script( 'custom-script', 'custom_values', array(
        'ajaxurl'   => admin_url( 'admin-ajax.php' ),
        'token'     => wp_create_nonce( 'token' )
    ));
}

/*AJAX Event to check for discount*/
add_action('wp_ajax_check_for_coupon', 'check_for_coupon');
add_action('wp_ajax_nopriv_check_for_coupon', 'check_for_coupon');

function check_for_coupon(){
    global $woocommerce;
    $send_json = array();
    $send_json = array('success'=>false);
    if($woocommerce->cart->discount_cart){
        $send_json = array('success'=>true, 'discount'=>$woocommerce->cart->discount_cart);
    }
    wp_send_json($send_json);
    die();
}

custom.js

/*
 * custom.js
 * @author : Rohil Mistry
 */

(function($){
    $(document).ready(function(){
        /*updated_checkout event*/
        $(document.body).on('updated_checkout', function(){
            /*Make an AJAX call on updated_checkout event*/
            $.ajax({
                type:       'POST',
                url:        custom_values.ajaxurl,
                data:       {action:'check_for_coupon'},
                success:    function( result ) {
                    console.info(result);
                    if(result.success){
                        $(".coupon_value").html(result.discount);
                    }
                    else{
                        $(".coupon_value").html('');
                    }
                }
            });
        });
    });
})(jQuery);

Find my Inline comment in code to understand the code.

Let me know if you have any doubt.

Comments