Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

implode causing invalid argument to show on admin sales emails

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 6 months 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 '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
    } 
    // For multiple coupons
    else {
        $coupon_codes = implode( ', ', $coupon_codes);
        echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'<p>';
    }
}

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

usr_profile.png

User

( 6 months 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.

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif