我在另一个线程上发现了这段php代码.它向WooCommerce购物车添加每种产品的百分比费用,并根据类别进行 checkout .我需要修改代码,使其添加一个统一费率的费用,而不是基于百分比的费用.我对php不是很了解,无法进行修改,所以我想问一问这里的专家能不能给我演示一下实现这一点的代码更改?

它工作得很好,只是加了%,而不是统一费率.以下是代码片段:

function df_add_ticket_surcharge( $cart_object ) {

    global $woocommerce;
    $specialfeecat = 33; // category id for the special fee
    $spfee = 0.00; // initialize special fee
    $spfeeperprod = 0.05; //special fee per product

    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 taxonamy of the prducts
        if ( $terms && ! is_wp_error( $terms ) ) :
            foreach ( $terms as $term ) {
                $catid = $term->term_id;
                if($specialfeecat == $catid ) {
                    $spfee = $spfee + $itmprice * $quantiy * $spfeeperprod;
                }
            }
        endif;  
    }

    if($spfee > 0 ) {

        $woocommerce->cart->add_fee( 'Ticket Concierge Charge', $spfee, true, 'standard' );
    }

}

add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

我try 了我对php代码知之甚少的东西,以及在线搜索,但都没有成功.我希望能在这里找到一个解决方案.谢谢你所能提供的任何帮助.

推荐答案

您正在使用的代码既复杂又过时.它可以针对WooCommerce 3+进行高度简化和更新.

由于你的问题不是很清楚,这里有多个 case (variations):

  1. 基于类别的唯一固定费用:
add_action( 'woocommerce_cart_calculate_fees', 'add_ticket_concierge_fee' );
function add_ticket_concierge_fee( $cart ) {
    $terms = array( 33 ); // Here, set the targeted category(ies) id(s), slug(s) or name(s)
    $fee_amount = 5; // Here, set the flat fee amount (per item)
    $term_count = 0; // Initializing variable

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        // Check if the current item belongs to the targeted category(ies)
        if( has_term($terms, 'product_cat', $item['product_id']) ) {
            $term_count++;
        } 
    }

    if($term_count > 0 ) {
        $cart->add_fee( 'Ticket Concierge Charge', $fee_amount, true );
    }
}
  1. 独特的固定费用,基于类别中的项目计数:
add_action( 'woocommerce_cart_calculate_fees', 'add_ticket_concierge_fee' );
function add_ticket_concierge_fee( $cart ) {
    $terms = array( 33 ); // Here, set the targeted category(ies) id(s), slug(s) or name(s)
    $fee_amount = 5; // Here, set the flat fee amount (per item quantity)
    $term_count = 0; // Initializing variable

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        // Check if the current item belongs to the targeted category(ies)
        if( has_term($terms, 'product_cat', $item['product_id']) ) {
            $term_count++;
        } 
    }

    if($term_count > 0 ) {
        $cart->add_fee( 'Ticket Concierge Charge', $fee_amount * $term_count, true );
    }
}
  1. 基于某一类别的商品数量计数的唯一固定费用:
add_action( 'woocommerce_cart_calculate_fees', 'add_ticket_concierge_fee' );
function add_ticket_concierge_fee( $cart ) {
    $terms = array( 33 ); // Here, set the targeted category(ies) id(s), slug(s) or name(s)
    $fee_amount = 5; // Here, set the flat fee amount
    $term_count = 0; // Initializing variable

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        // Check if the current item belongs to the targeted category(ies)
        if( has_term($terms, 'product_cat', $item['product_id']) ) {
            $term_count += $item['quantity'];
        } 
    }

    if($term_count > 0 ) {
        $cart->add_fee( 'Ticket Concierge Charge', $fee_amount * $term_count, true );
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

Laravel通过(扩展FormRequest类)方法验证表单请求

通过激活模板在数据库中创建表

一列必须指定多个变量的Laravel查询

在ShipStation for WooCommerce中使用自定义订单项目元数据

在WooCommerce中禁用客户用户角色的纳税计算

在Laravel Eloquent中实现一对多关系

为什么在我的PHP联系人脚本中开机自检后出现未定义变量错误?

PHP -多维数组到一维数组

使用php创建表格和插入数据

根据选定的字段值显示或隐藏WooCommerce注册字段

这是在可召回的背景下吗?

使用HPOS过滤WooCommerce中的订单列表

登录后重定向在WooCommerce中购买产品的用户

PHP 数组中第一级 node 到 XML 的唯一名称

启用 WooCommerce 我的帐户付款方式,允许客户添加付款方式

PHP访问子数组列表出错结果和长度

通过ajax发送数据到下拉列表未完全设置为所选

如何将文件上传添加到 WooCommerce 结帐?

从 Laravel 机制中排除目录并直接访问该目录

Woocommerce注册中的自定义复选框验证错误提示问题