我正在為客戶創建一個登錄頁面,并使用類別為landing-page.
我希望當前在購物車頁面上的其他產品在購物車上landing-page存在類別時被洗掉。
這是片段。現在,它會洗掉其中的所有產品,因為$woocommerce->cart->empty_cart().
add_action('woocommerce_checkout_before_customer_details', 'check_if_landing_page_category_is_on_cart');
function check_if_landing_page_category_is_on_cart() {
global $woocommerce;
$categories = array('landing-page');
$has_category = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$woocommerce->cart->empty_cart();
$has_category = true;
break;
}
}
if ( $has_category ) {
?>
<style>
.coupon-form {
display: none;
}
</style>
<?php
}
}
有什么建議嗎?
uj5u.com熱心網友回復:
您可以在WC_Cart::empty_cart()對面使用WC_Cart::remove_cart_item ()
所以你得到:
function action_woocommerce_checkout_before_customer_details() {
// Add categories. Multiple can be added, separated by a comma
$categories = array( 'landing-page' );
// Initialize
$cart_item_keys = array();
$has_category = false;
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// NOT certain category
if ( ! has_term( $categories, 'product_cat', $product_id ) ) {
// Push to array
$cart_item_keys[] = $cart_item_key;
} else {
$has_category = true;
}
}
// NOT empty & has category is true
if ( ! empty ( $cart_item_keys ) && $has_category ) {
// Loop through all cart item keys that do not contain the category
foreach ( $cart_item_keys as $cart_item_key ) {
// Remove product from cart
WC()->cart->remove_cart_item( $cart_item_key );
}
}
}
}
add_action( 'woocommerce_checkout_before_customer_details', 'action_woocommerce_checkout_before_customer_details', 10, 0 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/375138.html
標籤:WordPress的 求购 产品 钩子商务 查看
上一篇:如何在Wordpress中僅使用HTML更改Google可視化表上的顏色?
下一篇:無法讀取null的屬性
