gpt4 book ai didi

javascript - Sitepoint "Redeem code"Woocommerce 问题教程

转载 作者:行者123 更新时间:2023-11-30 20:57:08 25 4
gpt4 key购买 nike

我正在尝试在 Woocommerce 商店中实现“兑换优惠券”功能,我已经找到了有用的教程,但我无法使其正常工作。

This is the tutorial.

我已经做了什么:

  1. 使用以下代码创建了新的页面模板:

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

  2. 将此添加到我的主题的 functions.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' );
  3. 将此添加到我的主题的 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();
    }
    }
  4. 创建“kody.js”:

    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();
    });
    });
  5. 使用以下代码从 functions.php 调用脚本:

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

奇怪的是:它在起作用。我已经设置了一个页面,我可以在其中输入优惠券代码,粘贴代码,单击“兑换”,它会将与优惠券相关的产品添加到购物车。它不会,但是应用预定义的折扣。

“兑换优惠券”页面也只运行了一半。当有人将字段留空或输入不正确的代码时,它应该显示错误消息 - 它只会显示前者。输入不正确的代码会导致重定向到购物车。

我对 Ajax 和 JS 的了解非常有限,我试图找到一些其他教程,但没有运气。

代码有问题吗?它是从 2014 年开始的,所以 Wordpress 引擎可能发生了一些变化,导致了麻烦。

提前感谢您的任何回复!

问候

最佳答案

问题已解决,如果有人在提供的教程中遇到类似问题,请执行以下操作:

  1. 要申请折扣,请添加以下代码:

    global $woocommerce;
    WC()->cart->add_discount( $code );

    就在这些行的下面:

    // 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 );
  2. 要显示无效代码消息,请更改:

    // Check coupon to make determine if its valid or not
    if( ! $coupon->id && ! isset( $coupon->id ) ) {

    对此:

    // Check coupon to make determine if its valid or not
    if( ! $coupon->id && ! isset( $coupon_id ) ) {

现在一切正常。

(也改了标题,方便以后其他人找到这篇文章。)

关于javascript - Sitepoint "Redeem code"Woocommerce 问题教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47533346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com