Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableDataStore::save_cogs_data
Save the Cost of Goods Sold value of a given order to the database.
Method of the class: OrdersTableDataStore{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->save_cogs_data( $order );
- $order(WC_Abstract_Order) (required)
- The order to save the COGS value for.
OrdersTableDataStore::save_cogs_data() OrdersTableDataStore::save cogs data code WC 10.4.3
private function save_cogs_data( WC_Abstract_Order $order ) {
$cogs_value = $order->get_cogs_total_value();
/**
* Filter to customize the Cost of Goods Sold value that gets saved for a given order,
* or to suppress the saving of the value (so that custom storage can be used).
*
* @since 9.5.0
*
* @param float|null $cogs_value The value to be written to the database. If returned as null, nothing will be written.
* @param WC_Abstract_Order $item The order for which the value is being saved.
*/
$cogs_value = apply_filters( 'woocommerce_save_order_cogs_value', $cogs_value, $order );
if ( is_null( $cogs_value ) ) {
return;
}
$existing_meta = $this->data_store_meta->get_metadata_by_key( $order, '_cogs_total_value' );
if ( 0.0 === $cogs_value && $existing_meta ) {
$existing_meta = current( $existing_meta );
$this->data_store_meta->delete_meta( $order, $existing_meta );
} elseif ( $existing_meta ) {
$existing_meta = current( $existing_meta );
$existing_meta->key = '_cogs_total_value';
$existing_meta->value = $cogs_value;
$this->data_store_meta->update_meta( $order, $existing_meta );
} else {
$meta = new \WC_Meta_Data();
$meta->key = '_cogs_total_value';
$meta->value = $cogs_value;
$this->data_store_meta->add_meta( $order, $meta );
}
}