客制化滿額贈,並且依指定的分類累積金額

// 計算分類及商品類型的總金額
function calculate_total_amounts() {
    $cart = WC()->cart;
    $total_amount_common = 0;
    $total_amount_frozen = 0;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $product_id = $product->get_id();
        $product_category = wc_get_product_category_list( $product_id );
        $product_type = $product->get_attribute('pa_temperature'); // 假設使用屬性 "pa_temperature" 來區分商品類型

        $subtotal = $cart_item['line_subtotal'];

        if ( strpos( $product_category, '常溫' ) !== false ) {
            $total_amount_common += $subtotal;
        } elseif ( strpos( $product_category, '冷凍' ) !== false ) {
            $total_amount_frozen += $subtotal;
        }
    }

    return array(
        'common' => $total_amount_common,
        'frozen' => $total_amount_frozen,
    );
}

// 在購物車頁面顯示提示及處理贈品
function display_cart_amounts_notice_and_add_gifts() {
    $totals = calculate_total_amounts();

    // 顯示常溫和冷凍商品的總金額檢查異常狀態使用
/*    echo '<p>常溫商品總金額:' . wc_price( $totals['common'] ) . '</p>';
    echo '<p>冷凍商品總金額:' . wc_price( $totals['frozen'] ) . '</p>';*/

    // 以下是您提供的閾值及贈品設定
    $thresholds = [
       '常溫' => [
            3888 => [40440],
            2888 => [51361],  
        ], 
		
        '冷凍' => [
            3888 => [52120],
            2888 => [45663],
        ],
    ];

    $category_totals = array(
        '常溫' => $totals['common'],
        '冷凍' => $totals['frozen'],
    );

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

    // 根據閾值選擇並添加贈品
    foreach ($thresholds as $category => $category_thresholds) {
        foreach ($category_thresholds as $threshold_amount => $product_ids) {
            if ($category_totals[$category] >= $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' );

常溫商品滿 3888 會送 40440編號商品
冷凍商品滿 3888 會送 52120編號商品,以下依此類推
累積金額會自動於購物車加入該商品

由於贈品我是用比較笨的方式,用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 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);
瀏覽次數:3