Php 第一和第二季度的折扣百分比;第四级,适用于自定义表格

Php 第一和第二季度的折扣百分比;第四级,适用于自定义表格,php,woocommerce,Php,Woocommerce,我正在使用“”应答代码对其进行一些更改: add_action('woocommerce_cart_calculate_fees' , 'discount_based_on_customer_orders', 10, 1); function discount_based_on_customer_orders( $cart_object ){ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

我正在使用“”应答代码对其进行一些更改:

add_action('woocommerce_cart_calculate_fees' , 'discount_based_on_customer_orders', 10, 1);
function discount_based_on_customer_orders( $cart_object ){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Getting "completed" customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed' // Only orders with status "completed"
    ) );

    // Orders count
    $customer_orders_count = count($customer_orders);

    // The cart total
    $cart_total = WC()->cart->cart_contents_total;

    // First customer order
    if( empty($customer_orders) || $customer_orders_count == 0 ){
        $discount_text = __('First Order Discount', 'woocommerce');
        $discount = 0 - ($cart_total * 0.5);
    }
    // 4th order discount
    elseif( $customer_orders_count == 4 ){
        $discount_text = __('4th Order Discount', 'woocommerce');
        $discount = 0 - ($cart_total * 0.5);
    }

    // Apply discount
    if( ! empty( $discount ) ){
        // Note: Last argument is related to applying the tax (false by default)
        $cart_object->add_fee( $discount_text, $discount, false);
    }
}
我已经为客户设定了第一个和第四个订单,也设定了一个百分比,而不是固定价格

我希望使用以下内容将其应用于自定义签出页面:

<?php
if($discount == true){
    ?>
    <tr class="table__row discount_row discountrow">
        <th class="table__cell table__cell--head  table__cell--item table__cell--regular" scope="row" data-title="Discount">Discount</th>
        <td class="table__cell table__cell--quantity" data-title="QTY"></td>
        <td class="table__cell table__cell--total table__cell--regular" data-title="Discount">&pound;<span class="referral_discount">-<?php echo number_format($discount,2);?></span></td>
    </tr>
    <?php
}
?>

优惠
&磅-
如何做到这一点?因此,如果客户符合条件,则会在结帐时添加正确的行。

尝试使用:

<?php
if( WC()->cart->get_fee_total() < 0 ){
    ?>
    <tr class="table__row discount_row discountrow">
        <th class="table__cell table__cell--head  table__cell--item table__cell--regular" scope="row" data-title="Discount">Discount</th>
        <td class="table__cell table__cell--quantity" data-title="QTY"></td>
        <td class="table__cell table__cell--total table__cell--regular" data-title="Discount">&pound;<span class="referral_discount">-<?php echo number_format($discount,2);?></span></td>
    </tr>
    <?php
}
?>

优惠
&磅-
另外,
$discount=0-($cart\u total*0.5)可替换为
$折扣=-$cart\u总计*0.5

尝试使用:

<?php
if( WC()->cart->get_fee_total() < 0 ){
    ?>
    <tr class="table__row discount_row discountrow">
        <th class="table__cell table__cell--head  table__cell--item table__cell--regular" scope="row" data-title="Discount">Discount</th>
        <td class="table__cell table__cell--quantity" data-title="QTY"></td>
        <td class="table__cell table__cell--total table__cell--regular" data-title="Discount">&pound;<span class="referral_discount">-<?php echo number_format($discount,2);?></span></td>
    </tr>
    <?php
}
?>

优惠
&磅-

另外,
$discount=0-($cart\u total*0.5)可替换为
$折扣=-$cart\u总计*0.5

$折扣=0-($cart\u总计*0.5)
@BrettGregson,当我替换当前折扣金额时,它似乎不会折扣任何东西。
var\u dump($cart\u total);var_dump(折扣);模具()输出什么?@BrettGregson
string(118)£0.00“int(-1)
。它似乎没有把购物车的总数和使用它。@BrettGregson谢谢,让这个工作。。。我用第一部分更新了这个问题。现在只需要做第二部分。
$discount=0-($cart\u total*0.5)
@BrettGregson,当我替换当前折扣金额时,它似乎不会折扣任何东西。
var\u dump($cart\u total);var_dump(折扣);模具()输出什么?@BrettGregson
string(118)£0.00“int(-1)
。它似乎没有把购物车的总数和使用它。@BrettGregson谢谢,让这个工作。。。我用第一部分更新了这个问题。现在只需要做第二部分。