My WP site crashes when trying to access the appearance > widgets menu in the admin section. I am not sure how old this problem is as I just discovered it today.
Error details:
An error of type E_ERROR was caused in line 3 of the file /home/echipame/public_html/wp-content/themes/wilmer/framework/modules/woocommerce/widgets/woocommerce-dropdown-cart/templates/content.php. Error message: Uncaught Error: Call to a member function is_empty() on null in /home/echipame/public_html/wp-content/themes/wilmer/framework/modules/woocommerce/widgets/woocommerce-dropdown-cart/templates/content.php:3
Stack trace:
#0 /home/echipame/public_html/wp-content/themes/wilmer/framework/lib/mkdf.functions.php(849): include()
#1 /home/echipame/public_html/wp-content/themes/wilmer/framework/lib/mkdf.functions.php(870): wilmer_mikado_get_template_part('framework/modul...', '', Array, false)
#2 /home/echipame/public_html/wp-content/themes/wilmer/framework/modules/woocommerce/widgets/woocommerce-dropdown-cart/woocommerce-dropdown-cart.php(51): wilmer_mikado_get_module_template_part('widgets/woocomm...', 'woocommerce')
#3 /home/echipame/public_html/wp-includes/class-wp-widget.php(393): WilmerMikadoClassWoocommerceDropdownCart->widget(Array, Array)
#4 /home/echipame/public_html/wp-includes/widgets.php(1977): WP_Widget->display_callback(Array, Array)
#5 /home/echipame/public_html/wp-includes/rest-api/endpoints/class-wp-res
The PHP code (content.php) that seems to cause is is this:
<?php wilmer_mikado_get_module_template_part( 'widgets/woocommerce-dropdown-cart/templates/parts/opener', 'woocommerce' ); ?>
<div class="mkdf-sc-dropdown">
<div class="mkdf-sc-dropdown-inner <?php if(WC()->cart->is_empty()){ echo esc_attr('mkdf-empty-sc'); }?>">
<?php if ( ! WC()->cart->is_empty() ) {
wilmer_mikado_get_module_template_part( 'widgets/woocommerce-dropdown-cart/templates/parts/loop', 'woocommerce' );
wilmer_mikado_get_module_template_part( 'widgets/woocommerce-dropdown-cart/templates/parts/order-details', 'woocommerce' );
wilmer_mikado_get_module_template_part( 'widgets/woocommerce-dropdown-cart/templates/parts/button', 'woocommerce' );
} else {
wilmer_mikado_get_module_template_part( 'widgets/woocommerce-dropdown-cart/templates/posts-not-found', 'woocommerce' );
} ?>
</div>
</div>
I am very unknowledgeable when it comes to PHP so can you help me find the culprit? I think the issue is related to the following line, but I am not sure how to fix it:
<?php if ( ! WC()->cart->is_empty() ) {
CodePudding user response:
In both cases (you check if the cart is empty on line 3 and if the cart is not empty on line 4), you need to check if the "cart object" exists:
if ( WC()->cart && WC()->cart->is_empty() )
and
if ( WC()->cart && ! WC()->cart->is_empty() )