[英]WooCommerce minimum order fee for products in specific category
我想在我的 woocommerce 商店中為特定金額(價值,而不是數量)下的訂單添加費用。
我正在使用“ 基於 WooCommerce 動態最低訂單金額的費用”答案代碼,效果很好。
現在我正在嘗試更改此功能並將其僅應用於特定產品類別中的項目。 基本上,我必須將購物車中與特定類別匹配的所有產品的價值相加,並使用此值計算費用。
我怎樣才能達到這個目標?
以下重新訪問的代碼將處理特定定義的產品類別。 您必須定義:
編碼:
// Get the cart items subtotal amount from specific product categories
function get_specific_cart_items_total( $cart ) {
$categories = array('t-shirts'); // <=== Define your product categories
$items_amount = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ) {
if( has_term( $categories, 'product_cat', $item['product_id'] ) ) {
$items_amount += $item['line_subtotal'];
}
}
return $items_amount;
}
// Add a custom fee (displaying a notice in cart page)
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1 );
function add_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
return;
$min_price = 200; // The minimal cart amount
$current_price = get_specific_cart_items_total( $cart );
$custom_fee = $min_price - $current_price;
if ( $custom_fee > 0 ) {
$cart->add_fee( __('Minimum Order Adjustment'), $custom_fee, true );
// NOTICE ONLY IN CART PAGE
if( is_cart() ){
wc_print_notice( get_custom_fee_message( $min_price, $current_price ), 'error' );
}
}
}
// Displaying the notice on checkout page
add_action( 'woocommerce_before_checkout_form', 'custom_surcharge_message', 10 );
function custom_surcharge_message( ) {
$min_price = 200; // The minimal cart amount
$cart = WC()->cart;
$current_price = get_specific_cart_items_total( $cart );
$custom_fee = $min_price - $current_price;
// NOTICE ONLY IN CHECKOUT PAGE
if ( $custom_fee > 0 ) {
wc_print_notice( get_custom_fee_message( $min_price, $current_price ), 'error' );
}
}
// The fee notice message
function get_custom_fee_message( $min_price, $current_price ) {
return sprintf(
__('We have a minimum %s per order from specific categories. As your current order is only %s, an additional fee will be applied.', 'woocommerce'),
wc_price( $min_price ),
wc_price( $current_price )
);
}
代碼位於您的活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。
選項:要也處理父產品類別,請添加以下附加功能:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
並在第一個函數中替換這一行:
if( has_term( $categories, 'product_cat', $item['product_id'] ) ) {
這樣:
if( has_product_categories( $categories, $item['product_id'] ) ) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.