【发布时间】:2017-09-07 04:59:22
【问题描述】:
我是新来的,虽然我已经使用该网站数月来从其他问题中收集一些帮助。
我正在尝试通过以下方式在 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' );
谢谢!
【问题讨论】:
-
您的意思是要计算适用于每个产品或总购物车的费用百分比(所有产品总和 = 总购物车)?不是很清楚……
-
我希望购物车对某些产品的定制收取费用。因此,每个可定制的产品都在类别 id 61 中。当您订购 3 件产品(50 英镑、40 英镑和 30 英镑)时,其中两个(50 英镑和 40 英镑)在类别 id 61(可定制)中。所以我想要的是购物车对这些收取 10% 的额外费用。所以它会计算每个的 10%,然后将它们相加,并作为费用收取。因此,这三种产品的最终购物篮将是: 产品 1 - 50 英镑 产品 2 - 40 英镑 产品 3 - 30 英镑 ---------- 小计 - 120 英镑的费用 - 9 英镑(50 的 10% + 40 的 10%)总计 - 129 英镑谢谢
标签: php wordpress woocommerce