WooCommerce Cart Error Fix

User
[Sat Nov 16 09:30:39.551418 2024] [proxy_fcgi:error] [pid 3286076] [client 2a00:23c8:a1a:7501:15e5:6dd9:9e3:24b6:0] AH01071: Got error 'PHP message: get_cart was called incorrectly. Get basket should not be called before the wp_loaded action. Backtrace: require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\BlockTypesController->register_blocks, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->__construct, Automattic\WooCommerce\Blocks\BlockTypes\Cart->initialize, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->initialize, Automattic\WooCommerce\Blocks\BlockTypes\Cart->register_block_type_assets, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->register_block_type_assets, Automattic\WooCommerce\Blocks\Assets\Api->register_script, apply_filters('woocommerce_blocks_register_script_dependencies'), WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\Payments\Api->add_payment_method_script_dependencies, Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->get_all_active_paym…; PHP message: get_cart was called incorrectly. Get basket should not be called before the wp_loaded action. Backtrace: require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\BlockTypesController->register_blocks, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->__construct, Automattic\WooCommerce\Blocks\BlockTypes\Cart->initialize, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->initialize, Automattic\WooCommerce\Blocks\BlockTypes\Cart->register_block_type_assets, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->register_block_type_assets, Automattic\WooCommerce\Blocks\Assets\Api->register_script, apply_filters('woocommerce_blocks_register_script_dependencies'), WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\Payments\Api->add_payment_method_script_dependencies, Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->get_all_active_paym…; PHP message: get_cart was called incorrectly. Get basket should not be called before the wp_loaded action. Backtrace: require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\BlockTypesController->register_blocks, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->__construct, Automattic\WooCommerce\Blocks\BlockTypes\Cart->initialize, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->initialize, Automattic\WooCommerce\Blocks\BlockTypes\Cart->register_block_type_assets, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->register_block_type_assets, Automattic\WooCommerce\Blocks\Assets\Api->register_script, apply_filters('woocommerce_blocks_register_script_dependencies'), WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\Payments\Api->add_payment_method_script_dependencies, Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->get_all_active_paym…; PHP message: get_cart was called incorrectly. Get basket should not be called before the wp_loaded action. Backtrace: require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\BlockTypesController->register_blocks, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->__construct, Automattic\WooCommerce\Blocks\BlockTypes\Checkout->initialize, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->initialize, Automattic\WooCommerce\Blocks\BlockTypes\Checkout->register_block_type_assets, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->register_block_type_assets, Automattic\WooCommerce\Blocks\Assets\Api->register_script, apply_filters('woocommerce_blocks_register_script_dependencies'), WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\Payments\Api->add_payment_method_script_dependencies, Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->get_all_act…; PHP message: get_cart was called incorrectly. Get basket should not be called before the wp_loaded action. Backtrace: require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), WP_Hook->do_action, WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\BlockTypesController->register_blocks, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->__construct, Automattic\WooCommerce\Blocks\BlockTypes\Checkout->initialize, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->initialize, Automattic\WooCommerce\Blocks\BlockTypes\Checkout->register_block_type_assets, Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->register_block_type_assets, Automattic\WooCommerce\Blocks\Assets\Api->register_script, apply_filters('woocommerce_blocks_register_script_dependencies'), WP_Hook->apply_filters, Automattic\WooCommerce\Blocks\Payments\Api->add_payment_method_script_dependencies, Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->get_all_act…; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action, WP_Hook->apply_filters, load_serchdata_callback, WC_Data->get_meta, WC_Data->is_internal_meta_key, wc_doing_it_wrong. This message was added in version 3.2.0.; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action, WP_Hook->apply_filters, load_serchdata_callback, WC_Data->get_meta, WC_Data->is_internal_meta_key, wc_doing_it_wrong. This message was added in version 3.2.0.; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action, WP_Hook->apply_filters, load_serchdata_callback, WC_Data->get_meta, WC_Data->is_internal_meta_key, wc_doing_it_wrong. This message was added in version 3.2.0.; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action, WP_Hook->apply_filters, load_serchdata_callback, WC_Data->get_meta, WC_Data->is_internal_meta_key, wc_doing_it_wrong. This message was added in version 3.2.0.; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action, WP_Hook->apply_filters, load_serchdata_callback, WC_Data->get_meta, WC_Data->is_internal_meta_key, wc_doing_it_wrong. This message was added in version 3.2.0.; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action, WP_Hook->apply_filters, load_serchdata_callback, WC_Data->get_meta, WC_Data->is_internal_meta_key, wc_doing_it_wrong. This message was added in version 3.2.0.; PHP message: is_internal_meta_key was called incorrectly. Generic add/update/get meta methods should not be used for internal meta data, including "total_sales". Use getters and setters. Backtrace: do_action('wp_ajax_nopriv_load_serchdata'), WP_Hook->do_action,
Assistant
The error messages in your log primarily concern two issues:

### 1. **`get_cart was called incorrectly`**:
– This error indicates that the `get_cart` function in WooCommerce was invoked before the `wp_loaded` action. WooCommerce functions related to the cart or session management should only be executed after WordPress has been fully loaded (`wp_loaded` action is triggered).
– **Resolution**:
– Ensure that any code referencing `get_cart` or WooCommerce cart functionalities is hooked into an appropriate action that runs after `wp_loaded`. For example:

add_action('wp_loaded', function() {
           // Safe WooCommerce cart operations here.
           $cart = WC()->cart;
       });

– Check for custom code or plugins that may be calling `get_cart` too early, especially during the `init` or `plugins_loaded` actions.

### 2. **`is_internal_meta_key was called incorrectly`**:
– This error is related to using generic meta methods (`get_meta`, `add_meta`, `update_meta`) for WooCommerce internal meta keys like `total_sales`. WooCommerce expects developers to use specific methods for accessing or updating internal meta fields.
– **Resolution**:
– Review the code causing this issue, likely within the `load_serchdata_callback` function. For example:

do_action('wp_ajax_nopriv_load_serchdata');

It might be using `get_meta('total_sales')` directly. Replace this with WooCommerce-specific getters or avoid manipulating internal meta directly.
– Example fix:

// Avoid direct meta calls:
         // $total_sales = $product->get_meta('total_sales');

         // Use appropriate getter:
         $total_sales = $product->get_total_sales();

### Next Steps:
1. **Debugging**:
– Identify the source of the problematic code by searching for the keywords `get_cart` and `get_meta` in your custom plugins or theme.
– Add logging to trace back the origin of the issue.

2. **Update WooCommerce and Plugins**:
– Ensure you’re running the latest versions of WooCommerce and its dependent plugins. Outdated code may not comply with WooCommerce's latest standards.

3. **Test Changes in a Staging Environment**:
– Make changes and verify functionality in a non-production environment to avoid impacting live users.

4. **Check Custom Scripts**:
– Look for any custom Ajax calls or functions that might be executing code too early (e.g., `load_serchdata_callback`).

If you're unsure which specific part of your code is causing this, share your `load_serchdata_callback` function, and I can assist further.

Leave a Comment

Your email address will not be published. Required fields are marked *