客制化全館滿額贈

// 計算購物車中商品的總金額
function calculate_total_amount() {
    $cart = WC()->cart;
    $total_amount = 0;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $subtotal = $cart_item['line_subtotal'];
        $total_amount += $subtotal;
    }

    return $total_amount;
}

// 在購物車頁面顯示提示及處理贈品
function display_cart_amounts_notice_and_add_gifts_all() {
    $total_amount = calculate_total_amount();

    // 以下是您提供的閾值及贈品設定
    $thresholds = [
        6888 => [45232],
	3688 => [43999],
    ];

    // 移除已存在的贈品
    $cart_items = WC()->cart->get_cart();
    foreach ($cart_items as $cart_item_key => $cart_item) {
        foreach ($thresholds as $threshold_amount => $product_ids) {
            if (in_array($cart_item['product_id'], $product_ids)) {
                WC()->cart->remove_cart_item($cart_item_key);
            }
        }
    }

    // 根據閾值選擇並添加贈品
    foreach ($thresholds as $threshold_amount => $product_ids) {
        if ($total_amount >= $threshold_amount) {
            foreach ($product_ids as $product_id) {
                WC()->cart->add_to_cart($product_id);
            }
            break; // 只贈送該閾值的產品
        }
    }

    // 顯示提示

}
add_action( 'woocommerce_before_cart', 'display_cart_amounts_notice_and_add_gifts_all' );

由於贈品我是用比較笨的方式,用0元商品,因此還要搭配下面的functoin使用

禁止贈品被搜尋

function exclude_gift_products_from_search1111($query) {
    if (!is_admin() && $query->is_search) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat', 
                'field'    => 'slug', 
                'terms'    => 'gift', 
                'operator' => 'NOT IN', 
            )
        );
        $query->set('tax_query', $tax_query);
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_gift_products_from_search1111');

/*
// 添加一个过滤器,用于检查产品类别并禁止加入购物车
function prevent_add_to_cart_for_gift_category($passed, $product_id, $quantity, $variation_id = '', $variations = array(), $cart_item_data = array()) {
    // 获取产品的类别(分类)
    $product = wc_get_product($product_id);
    if ($product) {
        $categories = $product->get_category_ids();
        foreach ($categories as $category_id) {
            // 获取类别(分类)的 slug
            $category = get_term_by('id', $category_id, 'product_cat');
            if ($category && $category->slug === 'gift') {
                // 如果产品属于 "gift" 类别,则阻止加入购物车
                wc_add_notice(__('抱歉!此商品為贈品不可允許自行加入購物車.', 'your-text-domain'), 'error');
                return false;
            }
        }
    }
    return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'prevent_add_to_cart_for_gift_category', 10, 6);

*/
function exclude_gift_products_from_search1111($query) {
    if (!is_admin() && $query->is_search) {
        $tax_query = array(
            array(
                'taxonomy' => 'product_cat', 
                'field'    => 'slug', 
                'terms'    => 'gift', 
                'operator' => 'NOT IN', 
            )
        );
        $query->set('tax_query', $tax_query);
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_gift_products_from_search1111');

/*
// 添加一个过滤器,用于检查产品类别并禁止加入购物车
function prevent_add_to_cart_for_gift_category($passed, $product_id, $quantity, $variation_id = '', $variations = array(), $cart_item_data = array()) {
    // 获取产品的类别(分类)
    $product = wc_get_product($product_id);
    if ($product) {
        $categories = $product->get_category_ids();
        foreach ($categories as $category_id) {
            // 获取类别(分类)的 slug
            $category = get_term_by('id', $category_id, 'product_cat');
            if ($category && $category->slug === 'gift') {
                // 如果产品属于 "gift" 类别,则阻止加入购物车
                wc_add_notice(__('抱歉!此商品為贈品不可允許自行加入購物車.', 'your-text-domain'), 'error');
                return false;
            }
        }
    }
    return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'prevent_add_to_cart_for_gift_category', 10, 6);

*/

若產品名稱有'贈品'均返回首頁

function check_product_name_and_redirect() {
    if (is_product()) {
        global $post;
        $product_name = $post->post_title;

        if (strpos($product_name, '贈品') !== false) {
            wp_redirect(home_url());
            exit();
        }
    }
}
add_action('template_redirect', 'check_product_name_and_redirect');

產品有贈品兩個字無法被購買

function restrict_specific_products_from_cart2($passed, $product_id, $quantity) {
    $product = wc_get_product($product_id);

    if (strpos($product->get_name(), '贈品') !== false) {
        $passed = false;
        $message = "本商品無法加入購物車。";
        wc_add_notice($message, 'error');
    }
    
    return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'restrict_specific_products_from_cart2', 10, 3);
瀏覽次數:2