草庐IT

php - 修复 WooCommerce 中购物车百分比的最大优惠券折扣

coder 2024-04-14 原文

我在 woocommerce 中有优惠券代码 (XYZ25),其中包含 25% 的折扣,最高折扣为 Rs.250。

如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制用户获得不超过 250 卢比的折扣。

最佳答案

Since Woocommerce 3.2 or 3.3, this code doesn't work anymore

  1. 您可以根据 **RS.250固定购物车折扣设置额外的优惠券 FIX250 代码 (不含税)并且最低消费 (4 x 250) = RS.1000

  2. 然后在下面脚本的帮助下,如果客户应用您的 XYZ25 优惠券代码并且如果购物车总计达到 Rs.1000,它将用 FIX250 替换 XYZ25 优惠券,同时显示一个解释性通知......

这是代码:

add_action( 'woocommerce_calculate_totals', 'coupon_discount_max_switch', 10, 1);
function coupon_discount_max_switch( $cart_obj ) {

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

    // Set HERE your 2 coupons slugs  <===  <===  <===  <===  <===  <===  <===  <===  <===
    $coupon_25_percent = 'xyz25';
    $coupon_25_fixed = 'fix250';

    // Set HERE the limit amount  <===  <===  <===  <===  <===  <===  <===  <===  <===  <===
    $limit = 250; // Without VAT

    $total_discount = $cart_obj->get_cart_discount_total(); // Total cart discount

    // When 'xyz25' is set and the total discount is reached
    if( $cart_obj->has_discount( $coupon_25_percent ) && $limit_icl_vat <= $total_discount ){
        // Remove the 'xyz25' coupon
        $cart_obj->remove_coupon( $coupon_25_percent );
        // Checking that the fixed dicount is not already set.
        if( ! $cart_obj->has_discount( $coupon_25_fixed ) ){
            // Add the 'fix250' coupon
            $cart_obj->add_discount( $coupon_25_fixed );

            // Displaying a custom message
            $message = __( "The cart discount limit of Rs.$limit is reached", "woocommerce" );
            wc_add_notice( $message, 'notice' );
        }
    } 
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此工作代码已在 WooCommerce 版本 2.6.x 和 3.0+ 上进行测试。

关于php - 修复 WooCommerce 中购物车百分比的最大优惠券折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43887663/

有关php - 修复 WooCommerce 中购物车百分比的最大优惠券折扣的更多相关文章

  1. ruby - 如何修复 RVM gem "[ missing bin/ruby ]" - 2

    执行rvmlist后,我得到以下输出:rvmrubiesgems[missingbin/ruby]=*ruby-2.0.0-p645[x86_64]ruby-2.1.6[x86_64]ruby-2.2.1[x86_64]gems[missingbin/ruby]是什么意思?gems是某种系统gemset吗?它不是我创建的,我不知道我是否可以或应该删除它。 最佳答案 在我跑完之后:rvmfix-permissions然后我能够卸载具有[缺少bin/ruby]的版本。 关于ruby-如何修复

  2. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  3. ruby - 处理 gem 依赖项中错误修复的最佳方法是什么? - 2

    我有一个依赖于另一个gem的gem(在RubyGems上可用)。那个依赖的gem有一个我最近修复的错误。不幸的是,那个依赖的gem几乎已经死了;它已经很多年没有更新了,而且所有者不再在GitHub上活跃,根本,更不用说提交这个gem了,所以我不希望我的补丁会被接受——肯定不会很快。鉴于此,处理此依赖gem的补丁版本的最佳方法是什么?我是否将它fork并上传一个新的gem(使用新名称)到RubyGems,并依赖它?我是否以某种方式将我的固定版本与我自己的gem打包在一起? 最佳答案 首先,检查有问题的gem的许可证(以及您的代码的许可

  4. ruby-on-rails - 什么是自动修复英语语法的 Rails 插件或 Ruby gem? - 2

    Facebook刚刚重新推出了具有自动语法修复功能的评论。Whatdoesthegrammarfilterdo?Addspunctuation(e.g.periodsattheendofsentences)TrimsextrawhitespaceAutocaseswords(e.g.capitalizethefirstwordofasentence)Expandsslangwords(e.g.plzbecomesplease)Addsaspaceafterpunctuation(e.g.Hi,CatwouldbecomeHi,Cat)Fixcommongrammarmistakes(e

  5. ruby-on-rails - 在 Windows 7 x64 上安装 Ruby and Rails 和 DevKit 时出现问题 - 需要修复 - 2

    我在尝试安装ruby​​和rails时遇到了很多问题。在清除以前安装的版本之后,我已经尝试过没有和现在。尝试运行“geminstallrdiscount--platform=ruby”时出现以下错误:C:\Windows\system32>geminstallrdiscount--platform=rubyTemporarilyenhancingPATHtoincludeDevKit...Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrdiscount:ERROR:Failedtobuildgem

  6. ruby - 如何修复此错误 : kernel_require. rb :45:in `require' : cannot load such file? - 2

    我有以下文件结构:执行.rb图书馆我的类(class).rb在execute.rb我有下面的代码:#!/usr/bin/rubyrequire'lib/my_class'my_object=MyClass.newmy_object.some_method这是my_class.rb的代码:classMyClassdefsome_methodputs'OK'endend所以,我尝试运行execute.rb:rubyexecute.rb但是我收到这个错误:/home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/ru

  7. ruby - 如何修复 activesupport 3.0.0 与 2.x 相比的行为差异? - 2

    我在我的Sinatra应用程序中使用Hash#to_xml。在我转向actviesupport3.0.0之前,它一直有效activesupport在3.0.0的使用有区别吗?例如这很好用gem'activesupport','2.3.5'require'active_support'{}.to_xml和gem'activesupport','3.0.0'require'active_support'{}.to_xml生成:NoMethodError:{}的未定义方法“to_xml”:哈希 最佳答案 当您需要时,ActiveSuppo

  8. ruby-on-rails - 如何修复 `new' :String 的未定义方法 "Rack::Cors" - 2

    我是RubyonRails的新手,正在学习如何使用Angular,但是在我运行“geminstallrack-cors”之后,当我尝试启动Rails应用程序时,我保持收到此错误:C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:35:in`build':undefinedmethod`new'for"Rack::Cors":String(NoMethodError)Didyoumean?nextfromC:/Ruby23-x64/lib/ruby

  9. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  10. ruby - 通过 API 和 ruby​​ 创建优惠券返回错误 : woocommerce_api_missing_coupon_data - 2

    我正在尝试使用本页文档中的示例代码通过我的Rails4应用程序的其余API创建优惠券:https://woocommerce.github.io/woocommerce-rest-api-docs/?ruby#create-a-coupon这是我使用的代码:data={code:"10off",discount_type:"percent",amount:"10",individual_use:true,exclude_sale_items:true,minimum_amount:"100.00"}woocommerce.post("coupons",data).parsed_resp

随机推荐