I'm using Dokan-woocommerce-wordpress. trying to access the vendor ID of items added in cart, using foreach inside a plugin file.

// Get Vendor UPI ID and Murchant Code
        global $woocommerce;
        $items = $woocommerce->cart->get_cart();
         
        foreach( $items as $my_cart_item ){
            $my_product_id = $my_cart_item['product_id'];
        }
        
        
        $vendor_id = get_post_field( 'post_author', $my_product_id );
        $store_details = get_user_meta($vendor_id, 'dokan_profile_settings');
        $upi_id = $store_details[0]['vendor_upi_id'];
        $murchant_code = $store_details[0]['vendor_upi_murchant_code'];

The above code is giving the desired output but something from here is causing critical error and "edit" pages not opening in wordpress admin.

enter image description here

This is the error log msg

PHP Warning:  Invalid argument supplied for foreach() in /home3/cardocte/public_html/shreeroyals.com/wp-content/plugins/upi-qr-code-payment-for-woocommerce/includes/payment.php on line 57

Please suggest any solution for it.

1

There are 1 best solutions below

0
On

I found a solution. It seems that get_cart() causes problems if it is executed in the admin area so wrapping the above snippet in conditional: if( ! is_admin() ) { } fixed the issue for me.