Woocommerceストアで "クーデッドクーポン"機能を実装しようとしていますが、すでに役に立つチュートリアルが見つかりましたが、正しく動作させることはできません。Woocommerceの問題のSitepoint "Redeem code"チュートリアル
私はすでにやった:このコードで
作成した新しいページテンプレート:
<div class="redeem-coupon"> <form id="ajax-coupon-redeem"> <p> <input type="text" name="coupon" id="coupon"/> <input type="submit" name="redeem-coupon" value="Redeem Offer" /> </p> <p class="result"></p> </form><!-- #ajax-coupon-redeem -->
は私のテーマの機能にこれを追加しました。 PHPファイル:
add_action('wp_ajax_spyr_coupon_redeem_handler', 'spyr_coupon_redeem_handler'); add_action('wp_ajax_nopriv_spyr_coupon_redeem_handler', 'spyr_coupon_redeem_handler');
を追加しました。この私のテーマののfunctions.phpファイルへ:
function spyr_coupon_redeem_handler() { // Get the value of the coupon code $code = $_REQUEST['coupon_code']; // Check coupon code to make sure is not empty if(empty($code) || !isset($code)) { // Build our response $response = array( 'result' => 'error', 'message' => 'Code text field can not be empty.' ); header('Content-Type: application/json'); echo json_encode($response); // Always exit when doing ajax exit(); } // Create an instance of WC_Coupon with our code $coupon = new WC_Coupon($code); // Check coupon to make determine if its valid or not if(! $coupon->id && ! isset($coupon->id)) { // Build our response $response = array( 'result' => 'error', 'message' => 'Invalid code entered. Please try again.' ); header('Content-Type: application/json'); echo json_encode($response); // Always exit when doing ajax exit(); } else { // Coupon must be valid so we must // populate the cart with the attached products foreach($coupon->product_ids as $prod_id) { WC()->cart->add_to_cart($prod_id); } // Build our response $response = array( 'result' => 'success', 'href' => WC()->cart->get_cart_url() ); header('Content-Type: application/json'); echo json_encode($response); // Always exit when doing ajax exit(); } }
作成した "kody.js":このコードでのfunctions.phpからスクリプト呼び出され
jQuery(document).ready(function() { jQuery('#ajax-coupon-redeem input[type="submit"]').click(function(ev) { // Get the coupon code var code = jQuery('input#coupon').val(); // We are going to send this for processing data = { action: 'spyr_coupon_redeem_handler', coupon_code: code } // Send it over to WordPress. jQuery.post(woocommerce_params.ajax_url, data, function(returned_data) { if(returned_data.result == 'error') { jQuery('p.result').html(returned_data.message); } else { // Hijack the browser and redirect user to cart page window.location.href = returned_data.href; } }) // Prevent the form from submitting ev.preventDefault(); }); });
:
function my_scripts_method() { wp_register_script('kody', get_template_directory_uri() . '/js/kody.js', array('jquery'), '1.0'); enqueue the script wp_enqueue_script('kody'); } add_action('wp_enqueue_scripts', 'my_scripts_method');
これは奇妙なことです:それは働いています。私はクーポンコードを入力してコードを貼り付け、 "Redeem"をクリックしてクーポンに関連する商品をカートに追加するページをセットアップしました。ただし、あらかじめ定義された割引を適用しません。
「クーデッドクーポン」ページも半分しか動作しません。誰かがフィールドを空のままにしたり、間違ったコードを入力したりすると、エラーメッセージが表示されるはずです。間違ったコードを入力すると、カートにリダイレクトされます。
私のAjaxとJSに関する知識は非常に限られています。私はいくつかのチュートリアルを見つけようとしましたが、幸運なことはありません。
コードに問題がありますか?それは2014年からです。そのため、Wordpressエンジンで何か変わってトラブルが発生する可能性があります。
返信いただきありがとうございます!
よろしく解決