php - 如果还添加了特定产品,我如何从 WooCommerce 中的购物车中删除其他产品?
问题描述
我的 WooCommerce 网站正在销售 2 款限量版产品,为癌症研究筹集资金。一种是男鞋,另一种是女鞋。
因为这些产品将作为预售提供(它们要到 9 月才能发货)我想确保任何包含其中 1 只鞋子的订单不能在购物车中包含其他产品(因为这会导致部分执行的命令,这会引起大家的困惑)。
这是我想要的行为:
- 如果客户将 2 款限量版鞋中的任何一款添加到他们的购物车,然后将任何其他类型的产品添加到购物车,则其他产品将被移除,并显示一条消息,例如“您无法将其他产品添加到购物车当产品在购物车中时。”
- 如果客户的购物车中有普通产品,然后将 1 件限量版鞋加入购物车,普通产品将被移除并显示相同的消息:“当产品在购物车中时,您无法将其他产品添加到购物车。 "
仅供参考,这两个产品的 WooCommerce 产品 ID 是:348455 和 348443。它们都属于名为“EMI”的产品类别,产品类别 ID 为 348。
这是我迄今为止编写的代码,基于我发现的 2017 年类似的 Stackoverflow 问题。我下面的代码的问题是,如果我先将普通产品添加到我的购物车,然后是其中一只限量版鞋,它将从我的购物车中删除限量版鞋,而不是普通产品。
如果我先添加限量版鞋,然后添加普通产品,它就可以正常工作。有人可以帮我找出问题所在吗?
add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
// Set HERE your targeted product IDs
$target_product_id_one = 348455;
$target_product_id_two = 348443;
// Initialising some variables
$has_item = false;
$is_product_id = false;
foreach( WC()->cart->get_cart() as $key => $item ){
// Check if there are other products in the cart with the 2 limited edition shoes
if( $item['product_id'] !== $target_product_id_one && $item['product_id'] !== $target_product_id_two ){
$has_item = true;
$key_to_remove = $key;
}
// Check if we add to cart the targeted product ID
if( $product_id == $target_product_id_one || $product_id == $target_product_id_two ){
$is_product_id = true;
}
}
if( $has_item && $is_product_id ){
WC()->cart->remove_cart_item($key_to_remove);
// Optionaly displaying a notice for the removed item:
wc_add_notice( __( 'Due to the presale, EMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' );
}
}
解决方案
我找到了一些适合你的代码。当第二件(或后续)物品添加到您的购物车时,它会检查产品是否在购物车中,如果特殊鞋是添加到购物车的第一件物品,则删除其他所有物品,如果是普通产品,则移除特殊鞋第一件事添加到购物车。
// Remove conditionally cart items based on a specific product (item)
add_action( 'woocommerce_before_calculate_totals', 'remove_cart_items_conditionally_a', 10, 1 );
function remove_cart_items_conditionally_a( $cart ) {
// HERE define your specific product ID
$specific_product_id = 348455;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$cart_items = $cart->get_cart(); // Cart items array
$items_count = count($cart_items); // Different cart items count
// Continue if cart has at least 2 different cart items
if ( $items_count < 2 )
return;
$last_item = end($cart_items); // Last cart item data array
$is_last_item = false; // Initializing
// Check if the specific product is the last added item
if ( in_array($specific_product_id, array( $last_item['product_id'], $last_item['variation_id'] ) ) ) {
$is_last_item = true;
}
// Loop through cart items
foreach ( $cart_items as $cart_item_key => $cart_item ) {
// Remove all others cart items when specific product ID is --the last-- added to cart
if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
$cart->remove_cart_item( $cart_item_key );
// Optionaly displaying a notice for the removed item:
wc_add_notice( __( 'Due to the presale, AMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' );
}
// Remove the specific item when its is not the last added to cart
elseif ( in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && ! $is_last_item ) {
$cart->remove_cart_item( $cart_item_key );
// Optionaly displaying a notice for the removed item:
wc_add_notice( __( 'Due to the presale, AMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' );
}
}
}
推荐阅读
- java - 在 @OneToMany 关系中的 Spring Boot 中软删除子实体的问题
- python - Python从列表中提取内容
- c++ - 如何将迭代号传递给 Google Test 中的夹具?
- python - OperationFailure 不支持的投影选项:endingDate
- python - textract 因退出代码 127 失败:Windows 10 上的 pdftotext
- amazon-web-services - AWS DMS 服务和 S3
- amazon-web-services - 为什么 AWS elastic beanstalk 不复制我的 nginx 自定义配置?
- javascript - forloop内的内部html不起作用
- java - 我想使用 selenium 4 拦截网络请求并将它们保存到 json 文件中
- .net - 我的实体不是通过异步方法添加的