[英]Add a percent fee to WooCommerce per product, based on category
我是新來的,盡管我幾個月來一直在使用該網站,以尋求其他問題的幫助。
我正在嘗試通過以下方式在woocommerce中添加費用:
該站點必須檢查購物車,找到符合類別ID的產品,計算每種產品的費用百分比,對它們進行匯總並收取費用。
到目前為止,我已經設法檢查了購物車,找到了符合類別的產品(獲取數量)並乘以固定費用乘以金額。
如您所見,如果我有3個產品符合標准(類別ID),分別為£50,£40和£30,則費用將為3 * 10(固定費用為10)。
我真正想要的是該網站計算這些產品的費用百分比(50 * 10、40 * 10、30 * 10)並將其加起來(500 + 400 + 300)。 顯然,並非所有產品的價格都相同。
這是我的代碼:
function df_add_handling_fee( $cart_object ) {
global $woocommerce;
$specialfeecat = 61; // category id for the special fee
$spfee = 0.00; // initialize special fee
$spfeeperprod = 10; //special fee per product
//Getting Cart Contents.
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity
foreach($cart as $cart_val => $cid){
$qty += $cid['quantity'];
}
foreach ( $cart_object->cart_contents as $key => $value ) {
$proid = $value['product_id']; //get the product id from cart
$quantiy = $value['quantity']; //get quantity from cart
$itmprice = $value['data']->price; //get product price
$terms = get_the_terms( $proid, 'product_cat' ); //get taxonomy of the products
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
$catid = $term->term_id;
if($specialfeecat == $catid ) {
$spfee = $spfee + $quantiy * $spfeeperprod;
}
}
endif;
}
if($spfee > 0 ) {
$woocommerce->cart->add_fee( 'Handling Fee', $spfee, true, 'standard' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' );
謝謝!
希望這會有所幫助:
產品A-類別1 ///產品B-類別2 ///產品C-類別3 ////產品D-類別1
我的購物車=產品A的2個,產品B的3個,產品C的1個和產品D的4個
我想添加一筆費用,計算出我有1類和2類中的7件商品,並且每件產品的固定價格為5美元。
我發現在線下面的代碼可能更容易...不幸的是,我已經購買了幾個插件,但是它們不能滿足我的需求。
add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee');
function custom_applied_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your categories (can be an ID, a slug or the name… or an array of this)
$category1 = 'plain';
$category2 = 'plywood';
// variables initialisation
$fee = 0;
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item){
// Get the product object
$product = new WC_Product( $cart_item['product_id'] );
$quantiy = $value['quantity']; //get quantity from cart
// Initialising variables (in the loop)
$cat1 = false; $cat2 = false;
// ## CALCULATIONS ## (Make here your conditional calculations)
$quantity = GET TOTAL QUANTITY OF ALL ITEMS IN PREVIOUSLY SPECIFIED CATEBORIES
if($quantity <= 1){
$fee += 10 * $quanity;
} elseif($quantity > 1 && $dimention <= 9){
$fee += 5 * $quanity;
} elseif($dimention > 10){
$fee += 1 * $quanity;
}
}
// Adding the fee
if ( $fee != 0 )
WC()->cart->add_fee( $fee_text, $fee, true );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.