【问题标题】:Apply percentage discount on Woocommerce cart items for new customers为新客户应用 Woocommerce 购物车商品的百分比折扣
【发布时间】:2018-12-19 05:56:40
【问题描述】:

我们正在使用 Klarna Checkout(第三方插件)为我们的 WooCommerce 平台处理付款。

将产品添加到购物车后,会出现 Klarna 结帐表单,其中包含所需的详细信息,例如 电子邮件联系电话

当用户输入他们的电子邮件时,我会确定它是否是一封新电子邮件,并给予 50% 的折扣

our-custom.js

  var j = jQuery.noConflict();
  // check every second if email is filled
  var check_is_email_done = setInterval(function() {
    var is_email_done = j('.klarna-widget-form-user .email').text();

    if(is_email_done.length > 0) {
      console.log('email is filled: ' + is_email_done);
      var notFound = j('.fortnox-users td').filter(function(){ 
        return j(this).text() == is_email_done;
      }).get();

      var token = notFound.length;
      if(token > 0) {
        console.log('Old customer..');
      } else { 

        console.log('New customer..');

        // call new_customer_discount() method in functions.php
        j.ajax({
          type: 'GET',
          url: ajaxurl,
          cache: false,
          data: { action: 'newcustomerdiscount'},
          success: function(data) {

            console.log('newcustomerdiscount' + data);

          },
          error: function(xhr,status,error) {
            console.log('newcustomerdiscount error:'+error);

          }
        });

      }

      clearInterval(check_is_email_done);
    }

  },1000);

functions.php

function new_customer_discount() {
  //echo "new_customer_discount123";
  $my_total = wc_format_decimal(WC()->cart->total, 2);

  echo 'Total: '.$my_total;


  do_action('woocommerce_calculate_totals', function($cart) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    print_r($cart);  
    $computed_price = 0;

    // Loop Through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get the product id (or the variation id)
        $product_id = $cart_item['data']->get_id();

        // GET THE NEW PRICE (code to be replace by yours)
        if($computed_price > 0)
          $prod_price = $computed_price * .50; // 50% discount

        // Updated cart item price
        $cart_item['data']->set_price( $prod_price );
    }


  });

}

我上面的代码流程是当我确定一个客户是否是新客户时,我调用 functions.php 中的new_customer_discount() 方法然后执行带有回调的do_action

你知道我怎样才能在functions.php中正确执行上面的钩子吗?任何帮助是极大的赞赏。谢谢

【问题讨论】:

    标签: php jquery ajax wordpress woocommerce


    【解决方案1】:

    由于我无法测试您的 jQuery 代码,假设 jQuery Ajax 请求有效。现在要更改购物车商品的价格,您需要使用 woocommerce_before_calculate_totals 而在您的 php Ajax 中您将使用 WC_Session...

    在您的 jQuery 代码中,您可能需要在 success 部分添加以下行:

    j('body').trigger('update_checkout'); // Refresh checkout
    

    所以你的 PHP 代码将是:

    add_action('wp_ajax_nopriv_newcustomerdiscount', 'ajax_customer_discount');
    add_action('wp_ajax_newcustomerdiscount', 'ajax_customer_discount');
    function ajax_customer_discount() {
        WC()->session->set('new_customer', true);
    }
    
    
    
    add_action('woocommerce_before_calculate_totals', 'set_new_customer_discount', 100, 1 );
    function set_new_customer_discount( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // If it's not a new customer we exit
        if( ! WC()->session->get('new_customer') )
            return; // Exit
    
        // Loop Through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // 50% items discount
            $cart_item['data']->set_price( $cart_item['data']->get_price() / 2 ); 
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。当WC()->session->get('new_customer')true 时经过测试和工作......

    【讨论】:

    • 感谢您的回答。只是一个后续问题,即使 add_to_cart 已经完成,woocommerce_before_calculate_totals 仍然会运行吗?
    • woocommerce_before_calculate_totals 是一个 ajax 驱动的钩子,所以它就像woocommerce_calculate_totals 一样一直运行...
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2019-01-31
    • 1970-01-01
    • 2020-08-18
    • 2019-02-06
    • 1970-01-01
    相关资源
    最近更新 更多