【问题标题】:Add a percent fee to WooCommerce per product, based on category根据类别向每个产品的 WooCommerce 添加百分比费用
【发布时间】: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


【解决方案1】:

精美呈现!

这个插件可以轻松解决问题,它允许您根据您在 woocommerce 商店中配置的多个条件规则的组合在购物车中收取额外费用。

希望对你有帮助!

WooCommerce Conditional Product Fees For Checkout

【讨论】:

    【解决方案2】:

    希望这会有所帮助:

    产品 A - Category1 /// 产品 B - Category2 /// 产品 C - Category3 //// 产品 D - Category1

    我的购物车 = 产品 A 的 2 个,产品 B 的 3 个,产品 C 的 1 个,产品 D 的 4 个

    我想添加一笔费用,计算出我有 7 件类别 1 和 2 的商品,并添加每个产品 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 );
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多