get_changes(); $data = []; $insert = FALSE; $row = $this->get_product_row_from_db( $product->get_id() ); $columns = (array) apply_filters( 'atum/data_store/columns', array( 'purchase_price', 'supplier_id', 'supplier_sku', 'atum_controlled', 'out_stock_date', 'out_stock_threshold', 'inheritable', 'inbound_stock', 'stock_on_hold', 'reserved_stock', 'sold_today', 'sales_last_days', 'customer_returns', 'warehouse_damage', 'lost_in_post', 'other_logs', 'out_stock_days', 'lost_sales', 'has_location', 'update_date', ) ); // Columns data need to be converted to datetime. $date_columns = (array) apply_filters( 'atum/data_store/date_columns', array( 'out_stock_date', 'update_date', ) ); // Switches, checkboxes and flags. $yes_no_columns = (array) apply_filters( 'atum/data_store/yes_no_columns', array( 'atum_controlled', 'inheritable', 'has_location', ) ); // Values which can be null in the database. $allow_null = (array) apply_filters( 'atum/data_store/allow_null_columns', array( 'purchase_price', 'out_stock_date', 'out_stock_threshold', 'inbound_stock', 'stock_on_hold', 'reserved_stock', 'sold_today', 'sales_last_days', 'customer_returns', 'warehouse_damage', 'lost_in_post', 'other_logs', 'out_stock_days', 'lost_sales', 'has_location', 'update_date', ) ); // We should make an insert if the returning row is empty or has none of the ATUM columns. if ( ! $row || empty( array_intersect( array_keys( $row ), $columns ) ) ) { $insert = TRUE; } elseif ( empty( $changes ) ) { return; } foreach ( $columns as $column ) { if ( ( $insert || array_key_exists( $column, $changes ) ) && is_callable( array( $product, "get_$column" ) ) ) { $value = call_user_func( array( $product, "get_$column" ), 'edit' ); if ( in_array( $column, $date_columns, TRUE ) ) { $data[ $column ] = empty( $value ) ? NULL : gmdate( 'Y-m-d H:i:s', $product->{"get_$column"}( 'edit' )->getOffsetTimestamp() ); } elseif ( in_array( $column, $yes_no_columns, TRUE ) ) { // Some yes/no columns could allow NULL values too. if ( in_array( $column, $allow_null, TRUE ) && is_null( $value ) ) { $data[ $column ] = NULL; } else { $data[ $column ] = 'yes' === $value ? 1 : 0; // These columns are saved as integers in db. } } else { $data[ $column ] = '' === $value && in_array( $column, $allow_null, TRUE ) ? NULL : $value; } $this->updated_props[] = $column; } } if ( empty( $data ) ) { return; } // If not passing the updated date, add the current date. if ( ! array_key_exists( 'update_date', $changes ) ) { $data['update_date'] = gmdate( 'Y-m-d H:i:s' ); $this->updated_props[] = 'update_date'; } do_action( 'atum/data_store/before_saving_product_data', $data ); if ( $insert ) { $data['product_id'] = $product->get_id(); $wpdb->insert( $wpdb->prefix . Globals::ATUM_PRODUCT_DATA_TABLE, $data ); } elseif ( ! empty( $data ) ) { $wpdb->update( $wpdb->prefix . Globals::ATUM_PRODUCT_DATA_TABLE, $data, array( 'product_id' => $product->get_id(), ) ); } do_action( 'atum/data_store/after_saving_product_data', $data, $product->get_id() ); } /** * Method to delete a product from the database. * * @since 1.5.0 * * @param \WC_Product $product The product object. * @param array $args Array of args to pass to the delete method. */ public function delete( &$product, $args = array() ) { global $wpdb; // Delete the ATUM data for this product. if ( ! empty( $args['force_delete'] ) && TRUE === $args['force_delete'] ) { $wpdb->delete( $wpdb->prefix . Globals::ATUM_PRODUCT_DATA_TABLE, array( 'product_id' => $product->get_id() ), array( '%d' ) ); } // TODO: REMOVE PL AND MI DATA. do_action( 'atum/data_store/after_deleting_product_data', $product, $args ); } /** * Clear any ATUM's data store caches. * * @since 1.5.0 * * @param \WC_Product $product The product object. */ protected function clear_caches( &$product ) { /* @noinspection PhpUndefinedClassInspection */ parent::clear_caches( $product ); $cache_key = AtumCache::get_cache_key( 'product_data', $product->get_id() ); AtumCache::delete_cache( $cache_key ); } }