WooCommerce 中特定产品标签 ID 的购物车批量折扣
Cart bulk quantity discount for specific product tag IDs in WooCommerce
我想创建一个函数来为 WooCommerce 创建有条件的批量动态定价,不同的单价基于添加到购物车的数量。
我有两个产品标签 ID (326, 327),每个标签中都有一堆产品。我想为相同数量的两个标签提供不同的折扣。
- 标签 ID 326
- 10 件以上可享受 15% 的折扣
- 25 件以上可享受 20% 的折扣
- 标签 ID 327
- 10 件以上 20% 折扣
- 25 件以上可享受 35% 的折扣。
这是我正在使用的代码:
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_quantity_based_pricing', 9999 );
function woocommerce_quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
if( has_term( 326, 'product_tag' ) ) {
$threshold1 = 10;
$discount1 = 0.15;
$threshold2 = 25;
$discount2 = 0.3;
} elseif( has_term( 327, 'product_tag' ) ) {
$threshold1 = 10;
$discount1 = 0.2;
$threshold2 = 25;
$discount2 = 0.35;
}
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
$cart_item['data']->set_price( $price );
}
}
}
遗憾的是没有得到想要的结果。有什么建议吗?
要根据产品标签 ID 应用折扣,您可以使用 WC_Product::get_tag_ids(),然后进行比较。
我通过 woocommerce_cart_item_price
挂钩提供了一段额外的代码,这将允许您删除原始价格并在其旁边显示新价格
所以你得到:
// Used to calculate totals
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Set Tag IDs
$tag_id_1 = 326;
$tag_id_2 = 327;
// Tag ID 1
// 15% discount above 10 units
$tag_id_1_threshold_1 = 10;
$tag_id_1_discount_1 = 0.15;
// 20% discount above 25 units
$tag_id_1_threshold_2 = 25;
$tag_id_1_discount_2 = 0.20;
// Tag ID 2
// 20% discount above 10 units
$tag_id_2_threshold_1 = 10;
$tag_id_2_discount_1 = 0.20;
// 35% discount above 25 units
$tag_id_2_threshold_2 = 25;
$tag_id_2_discount_2 = 0.35;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get price
$price = $cart_item['data']->get_price();
// Get quantity
$quantity = $cart_item['quantity'];
// Get product tag ID's
$tag_ids = $cart_item['data']->get_tag_ids();
// Check for tag ID
if ( in_array ( $tag_id_1, $tag_ids ) ) {
// Compare
if ( $quantity >= $tag_id_1_threshold_1 && $quantity < $tag_id_1_threshold_2 ) {
$discount = $tag_id_1_discount_1;
} elseif ( $quantity >= $tag_id_1_threshold_2 ) {
$discount = $tag_id_1_discount_2;
}
} elseif ( in_array ( $tag_id_2, $tag_ids ) ) {
// Compare
if ( $quantity >= $tag_id_2_threshold_1 && $quantity < $tag_id_2_threshold_2 ) {
$discount = $tag_id_2_discount_1;
} elseif ( $quantity >= $tag_id_2_threshold_2 ) {
$discount = $tag_id_2_discount_2;
}
}
// Isset & NOT empty
if ( isset( $discount ) && ! empty( $discount ) ) {
// Set new price
$cart_item['data']->set_price( $price * ( 1 - $discount ) );
// Reset
$discount = '';
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
// Strikethrough the original price
function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Get the product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get reqular price
$regular_price = $product->get_regular_price();
// New price
$new_price = $cart_item['data']->get_price();
// NOT empty and NOT equal
if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
// Output
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';
}
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
我想创建一个函数来为 WooCommerce 创建有条件的批量动态定价,不同的单价基于添加到购物车的数量。
我有两个产品标签 ID (326, 327),每个标签中都有一堆产品。我想为相同数量的两个标签提供不同的折扣。
- 标签 ID 326
- 10 件以上可享受 15% 的折扣
- 25 件以上可享受 20% 的折扣
- 标签 ID 327
- 10 件以上 20% 折扣
- 25 件以上可享受 35% 的折扣。
这是我正在使用的代码:
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_quantity_based_pricing', 9999 );
function woocommerce_quantity_based_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
if( has_term( 326, 'product_tag' ) ) {
$threshold1 = 10;
$discount1 = 0.15;
$threshold2 = 25;
$discount2 = 0.3;
} elseif( has_term( 327, 'product_tag' ) ) {
$threshold1 = 10;
$discount1 = 0.2;
$threshold2 = 25;
$discount2 = 0.35;
}
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
$cart_item['data']->set_price( $price );
}
}
}
遗憾的是没有得到想要的结果。有什么建议吗?
要根据产品标签 ID 应用折扣,您可以使用 WC_Product::get_tag_ids(),然后进行比较。
我通过 woocommerce_cart_item_price
挂钩提供了一段额外的代码,这将允许您删除原始价格并在其旁边显示新价格
所以你得到:
// Used to calculate totals
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Set Tag IDs
$tag_id_1 = 326;
$tag_id_2 = 327;
// Tag ID 1
// 15% discount above 10 units
$tag_id_1_threshold_1 = 10;
$tag_id_1_discount_1 = 0.15;
// 20% discount above 25 units
$tag_id_1_threshold_2 = 25;
$tag_id_1_discount_2 = 0.20;
// Tag ID 2
// 20% discount above 10 units
$tag_id_2_threshold_1 = 10;
$tag_id_2_discount_1 = 0.20;
// 35% discount above 25 units
$tag_id_2_threshold_2 = 25;
$tag_id_2_discount_2 = 0.35;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get price
$price = $cart_item['data']->get_price();
// Get quantity
$quantity = $cart_item['quantity'];
// Get product tag ID's
$tag_ids = $cart_item['data']->get_tag_ids();
// Check for tag ID
if ( in_array ( $tag_id_1, $tag_ids ) ) {
// Compare
if ( $quantity >= $tag_id_1_threshold_1 && $quantity < $tag_id_1_threshold_2 ) {
$discount = $tag_id_1_discount_1;
} elseif ( $quantity >= $tag_id_1_threshold_2 ) {
$discount = $tag_id_1_discount_2;
}
} elseif ( in_array ( $tag_id_2, $tag_ids ) ) {
// Compare
if ( $quantity >= $tag_id_2_threshold_1 && $quantity < $tag_id_2_threshold_2 ) {
$discount = $tag_id_2_discount_1;
} elseif ( $quantity >= $tag_id_2_threshold_2 ) {
$discount = $tag_id_2_discount_2;
}
}
// Isset & NOT empty
if ( isset( $discount ) && ! empty( $discount ) ) {
// Set new price
$cart_item['data']->set_price( $price * ( 1 - $discount ) );
// Reset
$discount = '';
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
// Strikethrough the original price
function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Get the product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get reqular price
$regular_price = $product->get_regular_price();
// New price
$new_price = $cart_item['data']->get_price();
// NOT empty and NOT equal
if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
// Output
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';
}
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );