get( CostOfGoodsSoldController::class );
$this->cogs_is_enabled = $cogs_controller->feature_is_enabled();
$this->use_cogs_lookup_column = $this->cogs_is_enabled && $cogs_controller->product_meta_lookup_table_cogs_value_columns_exist();
}
/**
* Pre-warm the wp_count_posts cache before the list table renders.
*
* @internal
* @since 11.0.0
*
* @return void
*/
public function prime_status_counts_cache(): void {
$screen = get_current_screen();
if ( ! $screen || 'edit-product' !== $screen->id ) {
return;
}
// Performance note: the current listings architecture prevents us from isolating wp_count_posts calls.
// In the context of the products page, we can still isolate the underlying SQL by warming up the wp_count_posts cache.
$cache = (object) array_map(
static fn ( $count ) => $count ? (string) $count : (int) $count,
wc_get_container()->get( ProductUtil::class )->get_counts_for_type( 'product' )
);
// Trade-off: private-status tally may read slightly high for restricted roles (other users' privates included) — non-critical.
wp_cache_set_multiple(
array(
'posts-product' => $cache,
'posts-product_readable_' . get_current_user_id() => $cache,
),
'counts'
);
}
/**
* Prime featured image caches for product queries to avoid individual queries during rendering.
*
* @since 10.9.0
*
* @param \WP_Post[] $posts Posts from WP Query.
* @param \WP_Query $query Current query.
* @return array
*/
public function prime_thumbnail_caches( $posts, $query ) {
if ( $query instanceof \WP_Query && 'product' === $query->get( 'post_type' ) ) {
update_post_thumbnail_cache( $query );
}
return $posts;
}
/**
* Render blank state.
*/
protected function render_blank_state() {
echo '
' . esc_html( $this->object->get_cogs_value() ?? '0' ) . '
' :
'';
/**
* Product represented by the current list-table row.
* Narrow the inherited object type without adding a PHPStan baseline entry.
* In future we should correct the type of $this->object.
*
* @var WC_Product $product
*/
$product = $this->object;
$sale_date_from = $product->get_date_on_sale_from( 'edit' );
$sale_date_to = $product->get_date_on_sale_to( 'edit' );
$sale_date_from = $sale_date_from ? date_i18n( 'Y-m-d', $sale_date_from->getOffsetTimestamp() ) : '';
$sale_date_to = $sale_date_to ? date_i18n( 'Y-m-d', $sale_date_to->getOffsetTimestamp() ) : '';
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- the COGS value is already escaped.
/* Custom inline data for woocommerce. */
echo '
' . esc_html( $this->object->get_sku() ) . '
' . esc_html( $this->object->get_global_unique_id() ) . '
' . esc_html( $this->object->get_regular_price() ) . '
' . esc_html( $this->object->get_sale_price() ) . '
' . esc_html( $sale_date_from ) . '
' . esc_html( $sale_date_to ) . '
' . esc_html( $this->object->get_weight() ) . '
' . esc_html( $this->object->get_length() ) . '
' . esc_html( $this->object->get_width() ) . '
' . esc_html( $this->object->get_height() ) . '
' . esc_html( $this->object->get_shipping_class() ) . '
' . esc_html( $this->object->get_catalog_visibility() ) . '
' . esc_html( $this->object->get_stock_status() ) . '
' . esc_html( $this->object->get_stock_quantity() ) . '
' . esc_html( wc_bool_to_string( $this->object->get_manage_stock() ) ) . '
' . esc_html( wc_bool_to_string( $this->object->get_featured() ) ) . '
' . esc_html( $this->object->get_type() ) . '
' . esc_html( wc_bool_to_string( $this->object->get_virtual() ) ) . '
' . esc_html( $this->object->get_tax_status() ) . '
' . esc_html( $this->object->get_tax_class() ) . '
' . esc_html( $this->object->get_backorders() ) . '
' . esc_html( $this->object->get_low_stock_amount() ) . '
'
. $cogs_value_html .
'
';
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Render column: sku.
*/
protected function render_sku_column() {
echo $this->object->get_sku() ? esc_html( $this->object->get_sku() ) : '