2015-11-06 12 views
8

Soru olabilir i kupon uygulanan ben woocommerce_before_checkout_form kancası ile birlikte ödeme sayfasını WooCommerceiçinde SHORTCODE yoluyla indirim hakkında bazı bilgileri görüntülemek için çalışıyorum :(Ekran güncellenen veriler

garip alışveriş sepeti sayfada ve indirim için ziyaret edin, kanca mesajları indirim doğru değeri gösterir, ancak ben kaldırmak/kaldırmak/bu kanca ama yine de kısa kod değeri güncelleme değil, ekleyerek çalıştığım bu woocommerce_before_checkout_form kanca içinde hiçbir şey ortaya çıkarsa aşağıdaki fonksiyonu ile birçok kez test edilmiş olabilir herkes fikir etrafında biraz iş var/öneri harika olurdu

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

Sayfayı yeniden yüklemek için javascript ile denedim (yenileme örneği) ve çalışıyor, üstesinden gelmek için yerel bir wordpress/php çözümü olmadıkça bunu kullanmak istemiyorum.

echo "<script type='text/javascript'>location.reload();</script>"; 
anda yanımda gerçekleşmesi ne

olan

$ 10 indirim i hala 10 $ gösteren kasada gelen kupon kaldırırsanız o zaman ödeme i 10 $ değer ama edebilmek görmek edebilir, sepetinden eklendi ama 0 $ olmalı.

Teşekkürler.

+0

Neden kupon eklemek için bir kısa kod gerekiyor? Neye benzemesi gerektiğine dair bir ekran görüntüsünüz var mı? Neyin peşinde olduğunuzu takip etmekte zorlanıyorum. – helgatheviking

cevap

4

Sonucunuzu elde etmek için jQuery kullanabilirsiniz.

Yaklaşım:

WooCommerce yangınları bir jQuery olay updated_checkout şey ödeme sayfasına yönlendirmeye olur neredeyse her zaman. Böylece istediğiniz sonucu elde etmek için bu etkinliği kullanabilirsiniz. Mevcut kodunda

Modifikasyon: Herşeyden

1) Öncelikle bu JS Dosyasında kodlama yüzden yapabileceği jQueryJS ekleyin.

Yani sonra Modifikasyon bu gibi bir şey olacaktır kod: kod benim Inline yorumunu bulun

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

kodu anla. Herhangi bir şüpheniz varsa lütfen bize bildirin.