我想為我的基于客戶的用戶角色提供折扣,例如:
- 對于用戶角色(訂閱者),如果他們從特定類別購買 25 件產品,則可享受 50% 的折扣。
如果使用片段但它不包括我問題的第二部分(來自特定類別的 25 種產品)。
// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_on_user_role', 20, 1 );
function discount_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('company') )
return; // Exit
// HERE define the percentage discount
$percentage = 50;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
有什么建議嗎?
uj5u.com熱心網友回復:
對于類別categorie-1(根據需要進行調整),您可以根據購物車中的產品數量使用計數器。
當這等于 25 或更多時,您可以應用折扣。
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 'company' user role
if ( ! current_user_can( 'company' ) )
return;
// Category
$category = 'categorie-1';
// Percentage discount
$percentage = 50; // 50%
// Min quantity
$minimun_quantity = 25;
// Initialize
$current_quantity = 0;
// Loop though each cart item
foreach ( $cart->get_cart() as $cart_item ) {
// Has certain category
if ( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
// Quantity
$product_quantity = $cart_item['quantity'];
// Add to total
$current_quantity = $product_quantity;
}
}
// Greater than or equal to
if ( $current_quantity >= $minimun_quantity ) {
// Calculation
$discount = $cart->get_subtotal() * $percentage / 100;
// Applying discount
$cart->add_fee( sprintf( __( 'Discount (%s)', 'woocommerce' ), $percentage . '%'), -$discount, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/345951.html
標籤:WordPress的 求购 产品 大车
上一篇:PHP:獲取影像以鏈接到產品ID(woocommerce)
下一篇:python:在字典串列中查找
