WooCommerce 快速购物车费用
WooCommerce Quick cart fee
如果所有定义类别的产品总数为 1,或者如果数量为 2-9,我想对某些类别增加费用。只订购 1 件,如果他们订购 2-9 件,则每件产品 + 5 美元)。
我是从这个基础开始的:Custom Fee based on product dimensions and categories
我做了一些修改,但我无法让它工作,我卡住了。
这是我的代码:
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
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 );
}
我做错了什么?我怎样才能让它工作?
Updated 2
你的需求不是很明确,所以我已经尽力了。我了解到您想添加:
- 当定义的产品类别中有 1 件购物车商品时,将收取固定的初始费用。
- 当有来自定义产品类别的(2 到 9)个购物车商品时,根据购物车商品总数计算的费用。
这是相应的代码:
add_action( 'woocommerce_cart_calculate_fees','conditional_custom_multiple_fees', 10, 1 );
function conditional_custom_multiple_fees( $cart_object ) {
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)
$product_categories = array('clothing', 'disc');
$cat_qty = 0;
$cart_total_qty = $cart_object->get_cart_contents_count();
// Iterating through each cart item
foreach( $cart_object->get_cart() as $cart_item ){
$product = $cart_item['data']; // The product object
$item_qty = $cart_item['quantity']; // Item quantity
// Counting quanties for the product categories
if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
$cat_qty += $cart_item['quantity'];
}
## -- -- -- -- CALCULATIONS AND CONDITIONAL FEES -- -- -- -- ##
// 1. A fee for the product defined categories for 1 cart item only
if($cat_qty == 1) // updated HERE
{
$fee_amount = 10;
$cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
}
// 2. Or a calculated fee for the defined product categories (qty from 2 to 9)
if( $cat_qty >= 2 && $cat_qty <= 9 )
{
$amount_by_item = 5; // amount by item (updated)
$calculated_fee = $amount_by_item * $cat_qty; // calculation (updated)
$cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且可以工作。
谢谢LoicTheAztec我想只要稍微修改一下你的代码就可以了...
add_action( 'woocommerce_cart_calculate_fees','conditional_custom_multiple_fees', 10, 1 );
function conditional_custom_multiple_fees( $cart_object ) {
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)
$product_categories = array('clothing', 'disc');
$cat_qty = 0;
$cart_total_qty = $cart_object->get_cart_contents_count();
// Iterating through each cart item
foreach( $cart_object->get_cart() as $cart_item ){
$product = $cart_item['data']; // The product object
$item_qty = $cart_item['quantity']; // Item quantity
// Counting quanties for the product categories
if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
$cat_qty += $cart_item['quantity'];
}
## -- -- -- -- CALCULATIONS AND CONDITIONAL FEES -- -- -- -- ##
// 1. The 1st fee for the product defined categories (qty = 1)
if($cat_qty = 1)
{
$fee_amount = 10;
$cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
}
// 2. The Separate additional fee for the defined product categories (qty from 2 to 9)
if( $cat_qty >= 2 && $cat_qty <= 9 )
{
$amount_by_item = 5; // amount by item
$calculated_fee = $cat_qty * $amount_by_item; // calculation
$cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
}
}
如果所有定义类别的产品总数为 1,或者如果数量为 2-9,我想对某些类别增加费用。只订购 1 件,如果他们订购 2-9 件,则每件产品 + 5 美元)。
我是从这个基础开始的:Custom Fee based on product dimensions and categories
我做了一些修改,但我无法让它工作,我卡住了。
这是我的代码:
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
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 );
}
我做错了什么?我怎样才能让它工作?
Updated 2
你的需求不是很明确,所以我已经尽力了。我了解到您想添加:
- 当定义的产品类别中有 1 件购物车商品时,将收取固定的初始费用。
- 当有来自定义产品类别的(2 到 9)个购物车商品时,根据购物车商品总数计算的费用。
这是相应的代码:
add_action( 'woocommerce_cart_calculate_fees','conditional_custom_multiple_fees', 10, 1 );
function conditional_custom_multiple_fees( $cart_object ) {
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)
$product_categories = array('clothing', 'disc');
$cat_qty = 0;
$cart_total_qty = $cart_object->get_cart_contents_count();
// Iterating through each cart item
foreach( $cart_object->get_cart() as $cart_item ){
$product = $cart_item['data']; // The product object
$item_qty = $cart_item['quantity']; // Item quantity
// Counting quanties for the product categories
if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
$cat_qty += $cart_item['quantity'];
}
## -- -- -- -- CALCULATIONS AND CONDITIONAL FEES -- -- -- -- ##
// 1. A fee for the product defined categories for 1 cart item only
if($cat_qty == 1) // updated HERE
{
$fee_amount = 10;
$cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
}
// 2. Or a calculated fee for the defined product categories (qty from 2 to 9)
if( $cat_qty >= 2 && $cat_qty <= 9 )
{
$amount_by_item = 5; // amount by item (updated)
$calculated_fee = $amount_by_item * $cat_qty; // calculation (updated)
$cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且可以工作。
谢谢LoicTheAztec我想只要稍微修改一下你的代码就可以了...
add_action( 'woocommerce_cart_calculate_fees','conditional_custom_multiple_fees', 10, 1 );
function conditional_custom_multiple_fees( $cart_object ) {
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)
$product_categories = array('clothing', 'disc');
$cat_qty = 0;
$cart_total_qty = $cart_object->get_cart_contents_count();
// Iterating through each cart item
foreach( $cart_object->get_cart() as $cart_item ){
$product = $cart_item['data']; // The product object
$item_qty = $cart_item['quantity']; // Item quantity
// Counting quanties for the product categories
if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
$cat_qty += $cart_item['quantity'];
}
## -- -- -- -- CALCULATIONS AND CONDITIONAL FEES -- -- -- -- ##
// 1. The 1st fee for the product defined categories (qty = 1)
if($cat_qty = 1)
{
$fee_amount = 10;
$cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
}
// 2. The Separate additional fee for the defined product categories (qty from 2 to 9)
if( $cat_qty >= 2 && $cat_qty <= 9 )
{
$amount_by_item = 5; // amount by item
$calculated_fee = $cat_qty * $amount_by_item; // calculation
$cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
}
}