implode causing invalid argument to show on admin sales emails

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

Anytime we have a sale without a coupon being used, I get an error on the email stating implode() invalid argument functions.php

It was not causing this issue, until recently, then I moved the website from one host to another and updated all the plugins.

The email function hooked that display the text

add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for admins and when there at least 1 coupon in the order
    if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;

    foreach( $order->get_items('coupon') as $coupon ){
        $coupon_codes[] = $coupon->get_code();
    }
    // For one coupon
    if( count($coupon_codes) == 1 ){
        $coupon_code = reset($coupon_codes);
        echo '

'.__( 'Coupon Used: ').$coupon_code.'

'; } // For multiple coupons else { $coupon_codes = implode( ', ', $coupon_codes); echo '

'.__( 'Coupons Used: ').$coupon_codes.'

'; } }

If the customer does not use a coupon it either should be blank or state "No Coupon Used" - I am not sure why the multiple coupons area is causing the section when they do not use any coupons.

Instead of it working I keep just getting this invalid argument to implode error

profilepic.png
manpreet 2 years ago

Try to use array_push instead of $coupon_codes[]

$coupon_codes = array();
foreach( $order->get_items('coupon') as $coupon ){
      //$coupon_codes[] = $coupon->get_code();
      array_push($coupon_codes,$coupon->get_code());//changes
}

some hosts are not supporting [] in array creatation.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.