native_cogs_api_available(); $native_enabled = $this->native_cogs_feature_enabled(); $third_party = $this->detect_third_party_cogs_plugins(); return array( 'wc_version' => defined( 'WC_VERSION' ) ? (string) constant( 'WC_VERSION' ) : null, 'native_cogs_api' => $native_api, 'native_cogs_enabled' => $native_enabled, 'native_cogs_status' => $native_enabled ? 'enabled' : ( $native_api ? 'available_disabled' : 'unavailable' ), 'native_product_edit_cogs_ui' => $this->native_product_edit_cogs_ui_available(), 'csv_importer_url' => admin_url( 'edit.php?post_type=product&page=product_importer' ), 'third_party_cogs' => $third_party, 'has_third_party_cogs' => ! empty( $third_party ), ); } /** * Whether WooCommerce's product-edit screen ships a native `_cogs_value` Cost field. * * Feature flag alone is insufficient: some WC builds enable COGS without the * product general / variation admin markup. Inspect the installed view files. * * @return bool */ public function native_product_edit_cogs_ui_available() { static $cached = null; if ( null !== $cached ) { /** * Filter native product-edit COGS UI detection (tests / rare overrides). * * @param bool $available Detected availability. */ return (bool) apply_filters( 'aksmb_native_product_edit_cogs_ui_available', $cached ); } $detected = false; if ( $this->native_cogs_feature_enabled() && defined( 'WC_ABSPATH' ) ) { $wc_abspath = (string) constant( 'WC_ABSPATH' ); $candidates = array( $wc_abspath . 'includes/admin/meta-boxes/views/html-product-data-general.php', ); foreach ( $candidates as $path ) { if ( ! is_readable( $path ) ) { continue; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local installed plugin file, not remote. $chunk = file_get_contents( $path ); if ( is_string( $chunk ) && false !== strpos( $chunk, '_cogs_value' ) ) { $detected = true; } break; } } $cached = $detected; /** * Filter native product-edit COGS UI detection (tests / rare overrides). * * @param bool $available Detected availability. */ return (bool) apply_filters( 'aksmb_native_product_edit_cogs_ui_available', $cached ); } /** * Whether WC product exposes COGS getters. * * @return bool */ public function native_cogs_api_available() { if ( ! function_exists( 'wc_get_product' ) ) { return false; } // Feature flag / controller present on modern Woo. if ( class_exists( '\Automattic\WooCommerce\Internal\CostOfGoodsSold\CostOfGoodsSoldController' ) ) { return true; } return false; } /** * Whether native COGS feature is turned on in store settings. * * @return bool */ public function native_cogs_feature_enabled() { if ( class_exists( '\Automattic\WooCommerce\Internal\CostOfGoodsSold\CostOfGoodsSoldController' ) ) { try { $container = function_exists( 'wc_get_container' ) ? wc_get_container() : null; if ( $container && method_exists( $container, 'get' ) ) { $controller = $container->get( \Automattic\WooCommerce\Internal\CostOfGoodsSold\CostOfGoodsSoldController::class ); if ( $controller && is_callable( array( $controller, 'feature_is_enabled' ) ) ) { return (bool) $controller->feature_is_enabled(); } } } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Fall through to option heuristics. } } $option = get_option( 'woocommerce_feature_cost_of_goods_sold_enabled', null ); if ( null !== $option ) { return 'yes' === $option || true === $option || 1 === $option || '1' === $option; } return false; } /** * Known third-party COGS/cost plugins (basename => label). * * @return array */ private function known_third_party() { return array( 'woocommerce-cost-of-goods/woocommerce-cost-of-goods.php' => 'WooCommerce Cost of Goods (SkyVerge)', 'cost-of-goods-for-woocommerce/cost-of-goods-for-woocommerce.php' => 'Cost of Goods for WooCommerce', 'ati-wc-cost-of-goods/ati-wc-cost-of-goods.php' => 'ATI Cost of Goods', ); } /** * @return array */ private function detect_third_party_cogs_plugins() { if ( ! function_exists( 'is_plugin_active' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $found = array(); foreach ( $this->known_third_party() as $basename => $name ) { if ( is_plugin_active( $basename ) ) { $found[] = array( 'basename' => $basename, 'name' => $name, ); } } return $found; } }