折價券的折扣轉換成商品

會有這個客制,主要是因為我要匯入進銷存系統,預設的coupon的值會因為平均的關係造成小數點,所以才改成此法,用這招有一個重點,折扣改成0,輸入的代碼輸入後系統會自行加入該商品

// 在主题的functions.php文件中添加以下代码
add_action('woocommerce_before_calculate_totals', 'custom_apply_coupon', 10);

function custom_apply_coupon($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // 定义折扣码与产品ID的映射
    $coupon_product_mapping = array(
		'cy50' => 44738,
		'hg100' => 40445,

		'vip100'=> 47551,
    );

    // 检查购物车中是否包含指定产品
    $product_in_cart = false;

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        foreach ($coupon_product_mapping as $coupon_code => $product_id) {
            if ($cart_item['product_id'] === $product_id) {
                $product_in_cart = true;
                break 2;
            }
        }
    }

    // 获取当前应用的折扣码
    $applied_coupons = $cart->get_applied_coupons();

    foreach ($coupon_product_mapping as $coupon_code => $product_id) {
        // 检查应用的折扣码是否匹配
        if (in_array($coupon_code, $applied_coupons, true)) {
            // 如果折扣码有效,检查购物车中是否包含产品,如果不包含,则添加
            if (!$product_in_cart) {
                $cart->add_to_cart($product_id);
                break;
            }
        } else {
            // 如果折扣码失效或不存在,检查购物车中是否包含产品,如果包含,则移除
            if ($product_in_cart) {
                foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
                    if ($cart_item['product_id'] === $product_id) {
                        $cart->remove_cart_item($cart_item_key);
                        break;
                    }
                }
            }
        }
    }
}

由於贈品我是用比較笨的方式,用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