Asked  1 Year ago    Answers:  5   Viewed   12 times

From my previous question I am using Adding prefix to WooCommerce order number if order has items from a specific product category anwser code.

Now I want to extend this function for more categories, but I can't seem to find it out.

Now I want to add more if statements for the other categories. I am thinking of something like this:

if ('category of first product in appearing in cart' === 'category id / category name') {

    $prefix = 'AB-';
    $new_order_id = $prefix . $order_id;
    return $new_order_id;

}
elseif ('category of first product in appearing in cart' === 'category id / category name') {

    $prefix = 'CD-';
    $new_order_id = $prefix . $order_id;
    return $new_order_id;

}
elseif ('category of first product in appearing in cart' === 'category id / category name') {

    $prefix = 'EF-';
    $new_order_id = $prefix . $order_id;
    return $new_order_id;

}

Anyone able to help?

 Answers

5

To extend the code from Adding prefix to WooCommerce order number if order has items from a specific product category answer code for multiple categories you can use:

function filter_woocommerce_order_number( $order_number, $order ) {
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Product ID
        $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

        // Has term (product category)
        if ( has_term( array( 'categorie-1', 'categorie-2', 15, 16 ), 'product_cat', $product_id ) ) {
            return 'AB-' . $order_number;
        } elseif ( has_term( array( 'categorie-3', 'categorie-4' ), 'product_cat', $product_id ) ) {
            return 'CD-' . $order_number;
        } elseif ( has_term( array( 'categorie-5', 'categorie-6' ), 'product_cat', $product_id ) ) {
            return 'EF-' . $order_number;
        }
    }
    
    return $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );

Using if/elseif/else you can add as many conditions as you prefer

Saturday, May 29, 2021
 
Akarun
 
5

I was having the same issue and came across a code snippet that set me on the correct path. For the life of me, I cannot find the original site that housed the snippet, but here is my reworked version.

function df_add_ticket_surcharge( $cart_object ) {

    global $woocommerce;
    $specialfeecat = 86; // 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' );

This will add a 5% surcharge to each item in the cart with the category of ticket (id: 86). I hope this helps you out, if you have not found a solution already.

Thursday, April 1, 2021
 
TMichel
 
4

Your code is not working because by default WooCommerce does't store used coupon code in wp_postmeta table. It stores in wp_woocommerce_order_items table, under order_item_type => coupon and order_item_name => YOUR_CODE.

You have to first get all the Order ID(s), then you have to loop it to get the desired total, or tax or discount.

Here is the code:

function wh_getOrderbyCouponCode($coupon_code, $start_date, $end_date) {
    global $wpdb;
    $return_array = [];
    $total_discount = 0;

    $query = "SELECT
        p.ID AS order_id
        FROM
        {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        WHERE
        p.post_type = 'shop_order' AND
        p.post_status IN ('" . implode("','", array_keys(wc_get_order_statuses())) . "') AND
        woi.order_item_type = 'coupon' AND
        woi.order_item_name = '" . $coupon_code . "' AND
        DATE(p.post_date) BETWEEN '" . $start_date . "' AND '" . $end_date . "';";

    $orders = $wpdb->get_results($query);

    if (!empty($orders)) {
        $dp = ( isset($filter['dp']) ? intval($filter['dp']) : 2 );
        //looping throught all the order_id
        foreach ($orders as $key => $order) {
            $order_id = $order->order_id;
            //getting order object
            $objOrder = wc_get_order($order_id);

            $return_array[$key]['order_id'] = $order_id;
            $return_array[$key]['total'] = wc_format_decimal($objOrder->get_total(), $dp);
            $return_array[$key]['total_discount'] = wc_format_decimal($objOrder->get_total_discount(), $dp);
            $total_discount += $return_array[$key]['total_discount'];
        }
//        echo '<pre>';
//        print_r($return_array);
    }
    $return_array['full_discount'] = $total_discount;
    return $return_array;
}

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

USAGE

$orders = wh_getOrderbyCouponCode('my_code', '2016-09-17', '2016-10-07');
echo 'Total Discount : ' . $orders['full_discount'];
//print_r($orders);

Please Note:

All dates are in YYYY-MM-DD format.
print_r(array_keys(wc_get_order_statuses())); will output something like this:

Array
(
    [0] => wc-pending
    [1] => wc-processing
    [4] => wc-on-hold
    [5] => wc-completed
    [6] => wc-cancelled
    [7] => wc-refunded
    [8] => wc-failed
)

Code is tested and works.

Hope this helps!

Thursday, April 1, 2021
 
simPod
 
1

Updated

To sort fees by name on customer orders and email notifications, you will use the following:

// Custom function that sort displayed fees by name on order items
function wc_get_sorted_order_item_totals_fee_rows( &$total_rows, $tax_display, $order ) {
    $fees = $order->get_fees();

    if ( $fees ) {
        $fee_names = []; // initializing

        // First Loop
        foreach ( $fees as $fee_id => $fee ) {
            $fee_names[$fee_id] = $fee->get_name();
        }
        asort($fee_names); // Sorting by name

        // 2nd Loop
        foreach ( $fee_names as $fee_id => $fee_name ) {
            $fee = $fees[$fee_id];

            if ( apply_filters( 'woocommerce_get_order_item_totals_excl_free_fees', empty( $fee['line_total'] ) && empty( $fee['line_tax'] ), $fee_id ) ) {
                continue;
            }

            $total_rows[ 'fee_' . $fee->get_id() ] = array(
                'label' => $fee->get_name() . ':',
                'value' => wc_price( 'excl' === $tax_display ? $fee->get_total() : $fee->get_total() + $fee->get_total_tax(), array( 'currency' => $order->get_currency() ) ),
            );
        }
    }
}

// Display sorted fees by name everywhere on orders and emails
add_filter( 'woocommerce_get_order_item_totals', 'display_date_custom_field_value_on_order_item_totals', 10, 3 );
function display_date_custom_field_value_on_order_item_totals( $total_rows, $order, $tax_display ){
    // Initializing variables
    $new_total_rows = $item_fees = [];

    // 1st Loop - Check for fees
    foreach ( $total_rows as $key => $values ) {
        if( strpos($key, 'fee_') !== false ) {
            $item_fees[] = $key;
        }
    }

    if( count($item_fees) > 1 ) {
        // 2nd Loop - Remove item fees total lines
        foreach ( $item_fees as $key ) {
            unset($total_rows[$key]);
        }

        $key_start = isset($total_rows['shipping']) ? 'shipping' : ( isset($total_rows['discount']) ? 'discount' : 'cart_subtotal' );

        // 3rd Loop - loop through order total rows
        foreach( $total_rows as $key => $values ) {
            $new_total_rows[$key] = $values;

            // Re-inserting sorted fees
            if( $key === $key_start ) {
                wc_get_sorted_order_item_totals_fee_rows( $new_total_rows, $tax_display, $order );
            }
        }
        return $new_total_rows;
    }
    return $total_rows;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Saturday, May 29, 2021
 
Oshrib
 
1

There is no need to use a filter hook in the if condition. You can immediately add all logic in the correct filter hook.

So to add a prefix to the order number when the order contains an item from a specific category you only have to use:

function filter_woocommerce_order_number( $order_id, $order ) {
    // Prefix
    $prefix = 'AB-';
    
    // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
    $categories = array( 'categorie-1', 'categorie-2', 15, 16 );
    
    // Flag
    $found = false;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Product ID
        $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

        // Has term (product category)
        if ( has_term( $categories, 'product_cat', $product_id ) ) {
            $found = true;
            break;
        }
    }
    
    // true
    if ( $found ) {
        $order_number = $prefix . $order_id;
    } else {
        $order_number = $order_id;
    }
    
    return $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );

Related: Adding prefix to WooCommerce order number based on multiple categories

Saturday, May 29, 2021
 
inieto
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share