我想生成优惠券代码,例如AYB4ZZ2
.但是,我还希望能够标记使用过的优惠券并限制其全球数量N
.天真的方法就是"生成N
独特的字母数字代码,将它们放入数据库并在每个优惠券操作上执行数据库搜索".
但是,据我所知,我们还可以尝试找到一个函数 MakeCoupon(n)
,它将给定的数字转换为具有预定义长度的类似优惠券的字符串.
据我了解,MakeCoupon
应满足以下要求:
是双射的.反向MakeNumber(coupon)
应该是有效可计算的.
输出MakeCoupon(n)
应该是字母数字,并且应该具有小且恒定的长度 - 因此它可以被称为人类可读的.例如,SHA1
摘要不会通过此要求.
实用的独特性.MakeCoupon(n)
对于每种天然物的结果n <= N
应该在相同的术语中是完全独特的或独特的,例如,MD5
是独特的(具有相同的极小碰撞概率).
(这个定义很棘手)如何从单个优惠券代码中枚举所有剩余的优惠券并不明显 - 让我们说MakeCoupon(n)
并且MakeCoupon(n + 1)
应该在视觉上有所不同.
例如
MakeCoupon(n),
,n
用零填充的简单输出将不符合此要求,因为000001
并且000002
实际上并不"视觉上"不同.
是否存在满足以下要求的任何功能或函数发生器?我的搜索尝试只引导我使用[CPAN]
CouponCode,但它没有满足相应函数的双射要求.
在Woocommerce中,我试图找到一种方法,如果购物车中的重量超过100磅,则可以对整个客户的订单应用10%的折扣.我是实现这一目标的一部分.对于下一步,我正在寻找一种通过actions.php以动作/钩子编程方式应用优惠券代码的方法.
似乎我可以使用函数woocommerce_ajax_apply_coupon来执行此操作(http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html)但我不确定如何使用它.
到目前为止,我已经修改了cart.php以获得购物车中所有产品的总重量,我创建了一个应用折扣的优惠券(如果手动输入),我已经在functions.php中添加了一些代码来检查重量并向用户显示消息.
编辑:删除了部分代码,完成的代码包含在下面的解决方案中.
感谢Freney的指导.这是工作最终结果,它在条件满足时成功应用折扣优惠券,并在不再符合条件时将其删除:
/* Mod: 10% Discount for weight greater than 100 lbs
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart:
global $total_weight;
$total_weight = $woocommerce->cart->cart_contents_weight;
*/
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100');
function discount_when_weight_greater_than_100( ) {
global $woocommerce;
global $total_weight;
if( $total_weight > 100 ) {
$coupon_code = '999';
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has …
Run Code Online (Sandbox Code Playgroud) 我试图对购物车的总价格应用折扣,但我只能按商品基价而不是全价购买.我用Google搜索并在wordpress stackoverflow中遇到了这篇文章:
$ amount = floatval(preg_replace('#[^\d.]#','',$ woocommerce-> cart-> get_cart_total())); preg_replace消除除十进制字符和冒号之外的所有内容.
如果你想用它做数学,floatval会将字符串中的值转换为数字值.
我尝试添加:
$amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
Run Code Online (Sandbox Code Playgroud)
和改变
$discount = round( (($discounting_amount / 100 ) * $this->amount)*-1, WC()->cart->dp);
Run Code Online (Sandbox Code Playgroud)
至
$discount = round( (($discounting_amount / 100 ) * $amount2)*-1, WC()->cart->dp);
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Fatal error: Call to a member function get_cart_total() on a non-object in...
Run Code Online (Sandbox Code Playgroud) 我遇到了Magento单一优惠券代码的问题,该代码被标记为在客户点击"下订单"按钮时使用.如果Paypal付款失败或客户在订单完成之前离开页面,他将无法返回并重新订购此优惠券,该优惠券设置为仅使用一次,并且已标记已被使用.
我找到了一段代码,减少了用户使用优惠券的次数,并允许他重复使用优惠券.不幸的是,他在单击下订单按钮时尝试连接Paypal页面时出错.为了能够再次使用优惠券并访问Paypal页面,我必须使用此客户的ID 删除表salesrule_coupon_usage和salesrule_customer中SQL数据库中的行.
以下是我需要更改以自动删除客户ID的优惠券使用信息的代码:
public function cancel($observer)
{
$order = $observer->getEvent()->getPayment()->getOrder();
if ($order->canCancel()) {
if ($code = $order->getCouponCode()) {
$coupon = Mage::getModel('salesrule/coupon')->load($code, 'code');
if ($coupon->getTimesUsed() > 0) {
$coupon->setTimesUsed($coupon->getTimesUsed() - 1);
$coupon->save();
}
$rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
error_log("\nrule times used=" . $rule->getTimesUsed(), 3, "var/log/debug.log");
if ($rule->getTimesUsed() > 0) {
$rule->setTimesUsed($rule->getTimesUsed()-1);
$rule->save();
}
if ($customerId = $order->getCustomerId()) {
if ($customerCoupon = Mage::getModel('salesrule/rule_customer')->loadByCustomerRule($customerId, $rule->getId())) {
$couponUsage = new Varien_Object();
Mage::getResourceModel('salesrule/coupon_usage')->loadByCustomerCoupon($couponUsage, $customerId, $coupon->getId());
if ($couponUsage->getTimesUsed() > 0) { …
Run Code Online (Sandbox Code Playgroud) 首先,我什至不知道从哪里开始。我试过通读 Apple 和 Google 的文档,但仍然没有正确的答案。
我的客户已经在他们的网站上展示了优惠券,他们的客户可以打印或在手机上截图,带进商店扫描要应用的折扣。他们现在希望将“添加到钱包”按钮添加到他们的优惠券页面,这样用户就可以将优惠券添加到他们的 Apple 钱包或 Google Pay 帐户中。
执行此操作涉及哪些步骤?我知道我们必须连接到 Apple/Google API 才能与我们的网站集成。我对这些优惠券或“通行证”是如何生成的感到更加困惑?看起来有很多第三方服务可以帮助您创建这些通行证,但我们可以自己做吗?这里有什么关系?
如果有人在这方面有任何经验并且可以帮助我,或者为我指出一些好的文档的正确方向,那将不胜感激!
喜欢在标题...由于某种原因,magento网站不应用优惠券代码...总是返回无效的"优惠券代码无效"消息.但奇怪的是,当购物车的价格大于120我的货币时会发生这种情况.
示例:如果我在购物车中有一个价格为65的产品,则优惠券代码可以正常工作...如果我在购物车中有相同的产品但是数量为2(这意味着总价格变为130)我得到上述无效错误信息
我已经将代码追溯到"Mage_Sales_Model_Quote"类,它有一个名为:_validateCouponCode()的函数,里面有以下内容:$ address-> hasCouponCode()总是返回false ...我该怎么办?...我重新索引,刷新缓存......等等...没有改变...我似乎无法在地址模型中找到hasCouponCode函数,看看那里发生了什么...非常感谢提前
我每次有新注册时都需要运行一个脚本来检查我的优惠券,但我无法找到优惠券生成器(WooCommerce)存储的优惠券.
我在我的OpenCart环境中有另一个页面,让我们说下面有这些表格的关于我们页面,假设用户在他们的购物车中有项目,这些表格应该有效,但他们没有:
在此输入您的优惠券代码:
<form action="index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="basket">
<input type="text" value="" id="coupon" name="coupon"/>
<input type="hidden" value="coupon" name="next"/>
<input type="submit" class="button" value="Apply Coupon"/>
</form>
Run Code Online (Sandbox Code Playgroud)
在此输入您的礼券代码:
<form action="index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="basket">
<input type="text" value="" name="voucher"/>
<input type="hidden" value="voucher" name="next"/>
<input type="submit" class="button" value="Apply Voucher"/>
</form>
Run Code Online (Sandbox Code Playgroud)
这是代金券代码系统,但它不起作用(此代码默认不编辑):
/catalog/controller/checkout/cart.php
// VOUCHER
// IF THE USER HAS ENTERED A VOUCHER
if (isset($this->request->post['voucher']) && $this->request->post['voucher']) {
foreach ($this->request->post['voucher'] as $key) {
if (isset($this->session->data['vouchers'][$key])) {
unset($this->session->data['vouchers'][$key]);
}
}
}
Run Code Online (Sandbox Code Playgroud) 基于其他stackoverflow帖子,我已经能够使用以下代码以编程方式在Magento中生成个人购物车价格规则优惠券.
如何以编程方式调用"自动生成优惠券"功能,为我制定的每个定价规则创建100个独特的优惠券?谢谢!
$coupon = Mage::getModel('salesrule/rule');
$coupon->setName($_coupon['name'])
->setDescription('this is a description')
->setFromDate(date('Y-m-d'))
->setCouponType(2)
->setCouponCode($_coupon['code'])
->setUsesPerCoupon(1000)
->setUsesPerCustomer(100)
->setCustomerGroupIds(array(1)) //an array of customer groupids
->setIsActive(1)
//serialized conditions. the following examples are empty
->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setStopRulesProcessing(0)
->setIsAdvanced(1)
->setProductIds('')
->setSortOrder(0)
->setSimpleAction('by_percent')
->setDiscountAmount(100)
->setDiscountQty(null)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setIsRss(0)
->setWebsiteIds(array(1));
$coupon->save();
Run Code Online (Sandbox Code Playgroud)
例如,使用在管理面板中手动创建价格规则时可用的功能,此一个定价规则可能具有自动生成的优惠券代码(htgf-7774,htgf-2345等)的完整列表.
编辑:
我已经靠近了,使用下面的代码.仍然不知道如何专门指定自动生成模式
->setName('Name')
->setDescription('this is a description')
->setFromDate('2013-03-06')
->setToDate(NULL)
->setUsesPerCustomer('100')
->setIsActive('1')
->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
->setStopRulesProcessing('0')
->setIsAdvanced('1')
->setProductIds(NULL)
->setSortOrder('0')
->setSimpleAction('by_percent')
->setDiscountAmount('100.0000')
->setDiscountQty(NULL)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setTimesUsed('1')
->setIsRss('0')
->setCouponType('2')
->setUseAutoGeneration('1')
->setUsesPerCoupon('1000')
->setCustomerGroupIds(array('1',))
->setWebsiteIds(array('1',))
->setCouponCode(NULL)
Run Code Online (Sandbox Code Playgroud) 我有一个插件,可以向他们输入的电子邮件发送支持者推荐优惠券代码.当观众收到这封电子邮件时,我想创建一个流程,他们可以点击电子邮件中的"立即购物",优惠券将自动添加.
截至目前,对于"立即购买"按钮下的链接,我输入了以下内容:
websitename.biz/cart__trashed?code=DISCOUNTCODE
Run Code Online (Sandbox Code Playgroud)
要处理$code
我把它放在我的functions.php文件中:
add_action('woocommerce_before_cart', 'discount');
function discount( ) {
global $woocommerce;
$code= $_GET["code"];
if(!empty($code)){
if($woocommerce->cart->add_discount($code)){
echo '<div class="woocommerce_message"><strong>Applied coupon!</strong></div>';
}
}
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是:
我相信它是因为购物车是空的,代码不起作用.
只需要在受众点击链接时应用代码即可.
我怎样才能使这个工作?