WC_Product_Variation_Data_Store_CPT::update_attributes()
Update attribute meta values.
Method of the class: WC_Product_Variation_Data_Store_CPT{}
No Hooks.
Return
null
. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->update_attributes( $product, $force );
- $product(WC_Product) (required) (passed by reference — &)
- Product object.
- $force(true|false)
- Force update. Used during create.
Default: false
Changelog
Since 3.0.0 | Introduced. |
WC_Product_Variation_Data_Store_CPT::update_attributes() WC Product Variation Data Store CPT::update attributes code WC 9.2.3
protected function update_attributes( &$product, $force = false ) { $changes = $product->get_changes(); if ( $force || array_key_exists( 'attributes', $changes ) ) { global $wpdb; $product_id = $product->get_id(); $attributes = $product->get_attributes(); $updated_attribute_keys = array(); foreach ( $attributes as $key => $value ) { update_post_meta( $product_id, 'attribute_' . $key, wp_slash( $value ) ); $updated_attribute_keys[] = 'attribute_' . $key; } // Remove old taxonomies attributes so data is kept up to date - first get attribute key names. $delete_attribute_keys = $wpdb->get_col( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration "SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE %s AND meta_key NOT IN ( '" . implode( "','", array_map( 'esc_sql', $updated_attribute_keys ) ) . "' ) AND post_id = %d", $wpdb->esc_like( 'attribute_' ) . '%', $product_id ) ); foreach ( $delete_attribute_keys as $key ) { delete_post_meta( $product_id, $key ); } } }