WC_Product_Variation_Data_Store_CPT::update_attributes( WC_Product $product, bool $force = false )
Update attribute meta values.
Description Description
Parameters Parameters
- $product
-
(Required) Product object.
- $force
-
(Optional) Force update. Used during create.
Default value: false
Source Source
File: includes/data-stores/class-wc-product-variation-data-store-cpt.php
protected function update_attributes( &$product, $force = false ) {
$changes = $product->get_changes();
if ( $force || array_key_exists( 'attributes', $changes ) ) {
global $wpdb;
$attributes = $product->get_attributes();
$updated_attribute_keys = array();
foreach ( $attributes as $key => $value ) {
update_post_meta( $product->get_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->get_id()
)
);
foreach ( $delete_attribute_keys as $key ) {
delete_post_meta( $product->get_id(), $key );
}
}
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |