- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我找到了类似的解决方案,例如:“如何将订单限制在一个类别中”。我尝试修改代码,但它不够具体。
在我的例子中,每个供应商都是由一个产品属性值定义的,总共有 8 个术语。如果购物车包含来自 3 个以上不同条款的产品,我需要设置一条消息,内容为“抱歉,您一次只能从 3 个不同的供应商处订购”。
这是我用作起点的内容:
add_action( 'woocommerce_add_to_cart', 'three_vendors' );
function three_vendors() {
if ATTRIBUTE = SELECT A VENDOR; NUMBER OF TERMS > 3 {
echo "Sorry! You can only order from 3 Vendors at a time.”;
}
}
中间一行是我用非php语言填空
我正在寻找一种方法来定义购物车中的变化量。如果属性无法做到这一点,我愿意改用类别。
有人知道怎么做吗?
最佳答案
Update: Is not possible to manage variations with
woocommerce_add_to_cart_valisation
hook
相反,我们可以使用 Hook 在 woocommerce_add_to_cart
过滤器 Hook 中的自定义函数,当购物车中有超过 3 个供应商(商品)时,删除最后添加的购物车商品:
// Remove the cart item and display a notice when more than 3 values for "pa_vendor" attibute.
add_action( 'woocommerce_add_to_cart', 'no_more_than_three_vendors', 10, 6 );
function no_more_than_three_vendors( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// Check only when there is more than 3 cart items
if ( WC()->cart->get_cart_contents_count() < 4 ) return;
// SET BELOW your attribute slug… always begins by "pa_"
$attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")
// The current product object
$product = wc_get_product( $variation_id );
// the current attribute value of the product
$curr_attr_value = $product->get_attribute( $attribute_slug );
// We store that value in an indexed array (as key /value)
$attribute_values[ $curr_attr_value ] = $curr_attr_value;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// The attribute value for the current cart item
$attr_value = $cart_item[ 'data' ]->get_attribute( $attribute_slug );
// We store the values in an array: Each different value will be stored only one time
$attribute_values[ $attr_value ] = $attr_value;
}
// We count the "different" values stored
$count = count($attribute_values);
// if there is more than 3 different values
if( $count > 3 ){
// We remove last cart item
WC()->cart->remove_cart_item( $cart_item_key );
// We display an error message
wc_clear_notices();
wc_add_notice( __( "Sorry, you may only order from 3 different Vendors at a time. This item has been removed", "woocommerce" ), 'error' );
}
}
代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码已经过测试,应该适合您。它将检查与您的特定属性相关的属性值,并将删除最后添加的超过 3 个属性值的购物车项目。
The only annoying thing is that I can't remove the classic added to cart notice for the moment. I will try to find another way…
关于php - 防止客户从 Woocommerce 中超过 3 个供应商处订购,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45594842/
Java 文档说 CompletableFuture:supplyAsync(Supplier supplier)在 ForkJoinPool#commonPool() 中运行任务而 Completa
我正在尝试设置 IVR,或者更具体地说是使用 Asterisk 的自动接线员。除了简单的自动菜单系统之外,我不想要任何花哨的东西,而不是调用分机(现在),如果按下 1,只需调用同一条电话线 (POTS
当我尝试从 Symfony2 项目根运行以下命令时 php bin/vendors install 我收到以下错误: Could not open input file: bin/vendors 我对
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我正在开发一个涉及用户位置检测的 Android 应用程序。我想知道的是,这个 -> LocationManager.GPS_PROVIDER 是如何工作的? 它是使用手机中的 GPS 系统还是使用移
我不知道如何编写代码以在可能的情况下选择网络提供商,或者如果网络不可用则选择 GPS 提供商。我怎样才能改变代码来得到这个。这是我的第一个 Android 应用程序,我尝试这样做但没有成功。 pack
我不是 MySQL 专家,我必须为我的水平设计一个相当复杂的数据库。 我现在面临的问题在于同一个表(公司的宏观类别)中存在供应商-客户关系: 宏表 id name mega_i
我希望至少有人能在这里为我指明正确的方向。 我的业务需要开放式身份验证。 但是,不要使用其他服务,如facebook 或 google 等。 我们有一个成员(member)数据库 - 一个标准的 as
如果我需要一个变量的 ThreadLocal,是否还需要使用 Supplier(也是线程安全的)? 例如,Supplier 是否不需要在这里实现线程安全? private ThreadLocal> m
我在 brunch@1.7.6 没有编译 bower_component css 文件时遇到问题。类似于 Separating app and vendor css in Brunch .只有 css
我正在使用 select2在 angular 项目中(使用自耕农)。 Select2 css 位于以下目录中: bower_components/select2/select2.css bower_c
在我的 Rails 应用程序目录中,vendor/plugins 和 vendor/assets/stylesheets 存在(两者都是空的)。我想创建 javascripts 文件夹。我可以手动创建
我的代码 fragment 是: mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if
我继承了一个 Hadoop 安装,我很想知道以前的管理员是如何安装它的,它是从哪里来的。我是 Hadoop 的新手,但似乎以前的管理员简单地从源代码安装了 Apache Hadoop(而不是使用 Cl
我是 Ionic 2 的新手,正在尝试学习所有介绍如何添加提供程序的在线教程。 Ionic 似乎更改了生成的应用程序结构。有人可以给我一个例子,说明如何使用当前的 Ionic 2 应用程序结构执行此操
为什么供应商只支持无参数构造函数? 如果存在默认构造函数,我可以这样做: create(Foo::new) 但是如果唯一的构造函数需要一个字符串,我必须这样做: create(() -> new Fo
我已经通过 docker-compose 构建了一个容器,这里是 .yml: gateway: build: . image: sng container_name: sn
虽然不是直接的编程问题,但我想我可以在这里找到最佳答案。 为什么 USB-IF 监管供应商 ID 的使用并出售它们? 想要编写开源驱动程序的人或想要 2,000 美元会产生巨大影响的小公司会发生什
我正在使用 laravel-analytics ( https://github.com/spatie/laravel-analytics/ ) 并已在本地安装了所有内容,工作正常。 但是,每当我尝试
有没有一种方法/测试工作流程 - 如果我想从 gui 读取字符串内容并将其放入 ArrayList 中,然后将其写入 .xlsx 文件并使用该文件作为数据提供程序。如果是的话,我可以获得它的@test
我是一名优秀的程序员,十分优秀!