问题可能是奇怪的:(
我试图通过woocommerce的结帐页面显示一些关于折扣的信息与woocommerce_before_checkout_form的钩子如果我在购物车页面中应用优惠券并访问结帐注意到钩子消息显示正确的折扣价值,但如果我从中移除/添加checkout什么都没有显示在woocommerce_before_checkout_form钩子我尝试通过删除并添加该钩子但仍然短码值没有更新,我测试了多次以下功能可以任何人有一些工作的想法/建议将是伟大的
<?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 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.
提前致谢.
您可以使用jQuery来实现结果.
原文链接:https://www.f2er.com/php/136425.html方法:
几乎每次结账页面发生任何事情时,WooCommerce都会触发jQuery事件updated_checkout.因此,您可以使用该事件来实现所需的结果.
1)首先添加JS,以便我们可以在JS文件中进行jQuery编码.
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','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);
如果您有任何疑问,请告诉我.