Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableDataStore::read_cogs_data
Read the Cost of Goods Sold value for a given order from the database, if available, and apply it to the order.
Method of the class: OrdersTableDataStore{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->read_cogs_data( $order, $meta_data );
- $order(WC_Abstract_Order) (required)
- The order to get the COGS value for.
- $meta_data(object{meta_key:string,meta_value:string}[]) (required)
- The original meta-data fetched for the order.
OrdersTableDataStore::read_cogs_data() OrdersTableDataStore::read cogs data code WC 10.7.0
private function read_cogs_data( WC_Abstract_Order $order, array $meta_data ) {
$meta_entry = array_filter( $meta_data, fn( object $meta ) => '_cogs_total_value' === $meta->meta_key );
$cogs_value = array() === $meta_entry ? 0 : (float) current( $meta_entry )->meta_value;
/**
* Filter to customize the Cost of Goods Sold value that gets loaded for a given order.
*
* @since 9.5.0
*
* @param float $cogs_value The value as read from the database.
* @param WC_Abstract_Order $product The order for which the value is being loaded.
*/
$cogs_value = apply_filters( 'woocommerce_load_order_cogs_value', $cogs_value, $order );
$order->set_cogs_total_value( (float) $cogs_value );
$order->apply_changes();
}