WooCommerce get_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,

Can you show me exact function

Assistant
The error messages indicate several issues in your WordPress setup related to WooCommerce. Here's a breakdown of the problems:

### Key Errors:
1. **`get_cart was called incorrectly.`**
– The `get_cart` function is being used before the `wp_loaded` action. This is not allowed, as certain WooCommerce functions depend on WordPress fully loading.

2. **`is_internal_meta_key was called incorrectly.`**
– This error appears because you're trying to directly get or set internal meta data (`total_sales`) using generic methods instead of WooCommerce-specific getters and setters.

### Problematic Function Context:

The `get_cart` error is likely being triggered during the registration of WooCommerce blocks, as per the backtrace:
– The call happens in WooCommerce Blocks, likely while registering block assets or scripts (`register_block_type_assets`).

The `is_internal_meta_key` error is related to an AJAX function (`load_serchdata_callback`) that improperly accesses meta data.

### Example Problematic Code:

For **`get_cart`**, the issue may look something like this:

$cart = WC()->cart->get_cart();

If this is used before the `wp_loaded` action, it will throw the error.

For **`is_internal_meta_key`**, it might look like this:

$total_sales = get_post_meta($product_id, 'total_sales', true);

This directly accesses `total_sales`, which WooCommerce considers internal metadata.

### Correct Usage:

1. **Delay WooCommerce `get_cart` calls until `wp_loaded`:**

add_action('wp_loaded', function() {
       $cart = WC()->cart->get_cart();
       // Your code here
   });

2. **Use WooCommerce-specific methods for `total_sales`:**

$product = wc_get_product($product_id);
   $total_sales = $product->get_total_sales();

3. **For AJAX handlers, ensure WooCommerce is loaded:**

add_action('wp_ajax_nopriv_load_serchdata', 'load_serchdata_callback');
   add_action('wp_ajax_load_serchdata', 'load_serchdata_callback');

   function load_serchdata_callback() {
       if ( ! defined( 'WC_ABSPATH' ) ) {
           // Include WooCommerce core files.
           include_once dirname(__FILE__) . '/woocommerce.php';
       }

       // Safely access WooCommerce functions or product metadata here.
   }

### Fixing the Errors in Your Code:

#### WooCommerce `get_cart` Fix:
Make sure you're not calling `WC()->cart->get_cart()` before `wp_loaded`. Wrap it in a proper hook:

add_action('wp_loaded', 'custom_cart_function');

function custom_cart_function() {
    $cart_items = WC()->cart->get_cart();
    // Handle cart items.
}

#### Avoid Direct `get_post_meta` for Internal Keys:
Replace this:

$total_sales = get_post_meta($product_id, 'total_sales', true);

With:

$product = wc_get_product($product_id);
$total_sales = $product->get_total_sales();

If you can share more of the relevant code (especially around the `load_serchdata_callback` or other custom WooCommerce integrations), I can help refine these solutions further.

Leave a Comment

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