• Resolved felipemonteiro44

    (@felipemonteiro44)


    I have the following code to save the data from my custom field of the add-product page. With simple products it works very well. More when it is a variable product I need to add a different value to the target for each variation. How do I do that?

    function save_qtd_atacado($product_id, $form_data){
    update_post_meta($product_id, ‘qtd_atacado’, $form_data[‘qtd_atacado’]);
    }
    add_action(‘after_wcmp_fpm_meta_save’, ‘save_qtd_atacado’, 10, 2);

Viewing 3 replies - 1 through 3 (of 3 total)
  • @felipemonteiro44 $form_data will return array of product data that needs to be saved. Now, in the case of variable products, you need to fetch variations from this array, just like $form_data[‘qtd_atacado’]

    Now, you need to run the variations in a foreach loop and then fetch variation_id from this. After that update the meta accordingly.

    Thread Starter felipemonteiro44

    (@felipemonteiro44)

    I tried this other way, but I have not had results yet:

    function save_qtd_atacado($variation_id, $i){
    global $post;
    if( $post->post_type == ‘product’ )
    $product = wc_get_product( $post->ID );

    $available_variations = $product->get_available_variations();
    foreach ( $available_variations as $variation_values ) {
    $variation_id = $variation_values[‘variation_id’]; // variation id
    }
    update_post_meta($variation_id, ‘qtd_atacado’, $_POST[‘qtd_atacado’][$i]);

    }
    add_action(‘woocommerce_save_product_variation’, ‘save_qtd_atacado’, 10, 2);

    @felipemonteiro44, the method you were using previously was right, i.e. it is for WCMp. The method you have posted in last thread, that is for WooCommerce.

    The $form_data you have used in save_qtd_atacado($product_id, $form_data), which you have created earlier, now fetch the variations like $form_data[‘variations’].

    Now, you need to run the variations in a foreach loop and then fetch variation_id from this. After that update the meta accordingly.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Save metada product variable’ is closed to new replies.