Php 将值从自定义字段转移到电子商务中的属性

Php 将值从自定义字段转移到电子商务中的属性,php,woocommerce,Php,Woocommerce,我正在尝试自动将产品的自定义字段值转换为属性值 如果产品具有自定义字段(LagerPlacing)和属性(LagerPlacing):来自自定义字段的值将覆盖当前属性值。 如果产品具有自定义字段(LagerPlacing),但没有属性:将创建属性并添加值 我从家里醒来 这就是我所拥有的 // Define the function function update_lager( $product_id ) { //get the custom field $lagerplace

我正在尝试自动将产品的自定义字段转换为属性值

如果产品具有自定义字段(LagerPlacing)和属性(LagerPlacing):来自自定义字段的值将覆盖当前属性值。 如果产品具有自定义字段(LagerPlacing),但没有属性:将创建属性并添加值

我从家里醒来

这就是我所拥有的

// Define the function
function update_lager( $product_id ) {
    //get the custom field  
    $lagerplacering  = get_post_meta( $product_id ,'_Lagerplacering', true);

if ($lagerplacering) {
    // Get product attributes
    $product_attributes = get_post_meta( $product_id ,'_product_attributes', true);

    // Loop through product attributes
    foreach( $product_attributes as $attribute => $attribute_data ) {
            // Set the new value in the array
            $product_attributes[$attribute]['value'] = $lagerplacering; 
            break; // stop the loop
       }
    }
    // Set updated attributes back in database
    update_post_meta( $product_id ,'_product_attributes', $product_attributes );
}

//run the function when a product is being updated
add_action('save_post_product', 'transfer_value_on_product_save', 10, 1);
function transfer_value_on_product_save( $product_id) {
    $product = wc_get_product( $product_id );
   update_lager( $product_id );
}

它不起作用。

您打算将该值添加到现有属性(如果是,名称是什么?)还是创建新的产品属性?您想保留还是覆盖现有的属性/值?该属性是LagerPlacing,是的,我想重写该值。