is_rest_api_request()) { return; } // Load frontend includes (required for cart) WC()->frontend_includes(); // Load cart using WooCommerce's official method if (null === WC()->cart && function_exists('wc_load_cart')) { wc_load_cart(); } } /** * Load custom variation attributes from post meta for WooCommerce admin * This ensures WooCommerce's native admin displays custom attributes correctly */ public static function load_variation_attributes($variation) { if (!$variation instanceof \WC_Product_Variation) { return; } $parent = wc_get_product($variation->get_parent_id()); if (!$parent) { return; } $attributes = []; foreach ($parent->get_attributes() as $attr_name => $attribute) { if (!$attribute->get_variation()) { continue; } // Read from post meta (stored as lowercase) $meta_key = 'attribute_' . strtolower($attr_name); $value = get_post_meta($variation->get_id(), $meta_key, true); if (!empty($value)) { $attributes[strtolower($attr_name)] = $value; } } if (!empty($attributes)) { $variation->set_attributes($attributes); } } }