2015-11-06 96 views
8

問題可能是奇數:(顯示更新的數據

我想在結帳頁面woocommercewoocommerce_before_checkout_form鉤來顯示通過短碼打折的一些信息。如果我申請優惠券在購物車頁面和訪問結帳注意到掛鉤消息顯示正確的折扣價值,但如果我從結帳中刪除/添加沒有出現在該woocommerce_before_checkout_form鉤我試圖通過刪除並添加該鉤,但仍然shortcode值不更新,我多次測試以下功能任何人都可以有一些工作想法/建議將是偉大的

<?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'); 

我嘗試用JavaScript來重新加載頁面(刷新實例),它正在工作,我不想使用那一個,除非沒有原生的WordPress/PHP解決方案來克服。

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

目前在我的身邊發生的是

$ 10的折扣,我從購物車中添加,然後在結帳我可以看到$值10,但 如果我從結賬它仍然呈現出$ 10拆卸優惠券但它應該是$ 0。

在此先感謝。

+0

爲什麼你需要簡碼才能添加優惠券?你有截圖/樣機應該是什麼樣子?我很難跟隨你的所作所爲。 – helgatheviking

回答

4

您可以使用jQuery來實現您的結果。

方法:

WooCommerce大火幾乎每一個遇到事到結帳頁面時一個jQuery事件updated_checkout。所以你可以使用這個事件來達到你想要的結果。

修改在當前的代碼:所有的

1)首先添加JS這樣我們就可以做jQuery在JS文件編碼。

所以修改後的代碼會是這個樣子:

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); 

查找代碼我在線的留言理解代碼。

讓我知道你是否有任何疑問。

相關問題