Allow checkout only when a product of a mandatory category is in cart(仅当购物车中包含强制性类别的产品时才允许结帐)
问题描述
如果客户的购物车中没有特定的产品类别,我想阻止他们进行结账.我还想通过错误消息告诉他们他们需要添加某个产品.我找到了一些代码,但无法正常工作.我已将它作为代码片段添加到我的 Wordpress 安装中,但可惜它不起作用,即使我打开了调试,也没有错误消息.这是我在 Github 中找到的可能需要修改才能使其工作的代码:
因此,如果兄弟姐妹"类别是购物车中的唯一商品,我希望停止结帐(带有错误消息).我有一个标准"类别,必须在客户结账之前放在购物篮中.希望这是有道理的.
这里有一个可以解决问题的解决方案.特别是有两个主要功能(最后一个):
- 第一个功能(N°3)在购物车页面上显示您的消息,当购物车中有东西但不是强制性产品类别时.还会在必需的产品存档页面上显示消息(当客户从结帐中被重定向时很有用,见下文).
- 第二个功能(N°4)在客户尝试结账并且他的购物车没有缺少的强制性产品类别时将客户重定向到产品强制性类别存档页面.
<块引用>
在 your_mandatory_category_slug()
函数中的强制类别 slug 之前定义.
这是代码:
这会出现在活动子主题(或主题)的 function.php 文件中,也可能出现在任何插件文件中.
此代码经过测试且功能齐全.
I'd like to stop any customer advancing to the checkout if they do not have a particular product category in their basket. I would also like to tell them with an error message that they need to add a certain product. I've found some code but cannot it to work. I've added it as a code snippet into my Wordpress install but alas it does not function and there are no error messages even though I have debugging switched on. Here is the code that I have found in Github that may need modification in order for this to work:
So I'm looking to stop checkout (with error message) if the 'sibling' category is the only item in the cart. I have a 'standard' category which must be in the basket before the customer makes it to the checkout. Hope this makes sense.
Here you have a solution that will make the trick. There is especially 2 main functions (the last ones):
- The first function (N°3) display your message on cart page, when there is something in cart but not the mandatory product category. Displays also the message on the mandatory product archive pages (useful when customer get redirected from checkout, see below).
- The second function (N°4) redirect customer to the product mandatory category archive pages when it tries to checkout and his cart has not that missing mandatory product category.
Define before your mandatory category slug in
your_mandatory_category_slug()
function.
This is the code:
This goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.
这篇关于仅当购物车中包含强制性类别的产品时才允许结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!