s_value_columns_exist() ) { $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_product_meta_lookup ADD COLUMN cogs_total_value DECIMAL(19,4)" ); $wpdb->query( "UPDATE {$wpdb->prefix}wc_product_meta_lookup AS lookup JOIN {$wpdb->prefix}postmeta AS pm ON lookup.product_id = pm.post_id SET lookup.cogs_total_value = CAST(pm.meta_value AS DECIMAL(19, 4)) WHERE pm.meta_key = '_cogs_total_value';" ); } } /** * Handler for the "remove COGS value column to the product meta lookup table" admin tool. * * @internal Tool callback, not to be explicitly used from outside the class. */ public function remove_lookup_cogs_columns() { global $wpdb; if ( $this->product_meta_lookup_table_cogs_value_columns_exist() ) { $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_product_meta_lookup DROP COLUMN cogs_total_value" ); } } /** * Tells if the COGS value column exists in the product meta lookup table. * * @return bool True if the column exists, false otherwise. */ public function product_meta_lookup_table_cogs_value_columns_exist(): bool { global $wpdb; return (bool) $wpdb->get_var( $wpdb->prepare( "SHOW COLUMNS FROM {$wpdb->prefix}wc_product_meta_lookup LIKE %s", 'cogs_total_value' ) ); } /** * Get the tooltip text for the COGS value field in the product editor. * * @param bool $for_variable_products True to get the value for variable products, false for other types of products. * @return string The string to use as tooltip (translated but not escaped). */ public function get_general_cost_edit_field_tooltip( bool $for_variable_products ) { return $for_variable_products ? __( 'Add the amount it costs you to buy or make this product. This will be applied as the default value for variations.', 'woocommerce' ) : __( 'Add the amount it costs you to buy or make this product.', 'woocommerce' ); } }