Search code examples
phpwordpresswoocommerceforeachvariable-product

Add single custom field and its static value to all Woocommerce product variations


I am trying to add a custom field and a static value to all of my variable products in Woocommerce. I have gone through other questions but they relate to creating whole new variations. I just need to add/update custom field to existing variaitons. Here is the thing I need to add in each variation postmeta: add_post_meta($variation_id, '_wc_facebook_product_image_source', 'parent_product'); I dont know how to execute for each loop for all variations.

I need to do this is because we have thousands of products being imported through a data feed (Dropshipping site). And this custom field is used to send data to Facebook for Catalogue. We need to hook an action that whenever a new variable product is added, all of it's variation has custom field in postmeta added with value as 'parent_product'. I have already added this filter to change the data for simple products. And is working perfect for all simple products. But the problem is that variable products having variations dont get this custom field.

Here is my code for simple products:

add_filter ('facebook_for_woocommerce_integration_prepare_product', 'fix_image_url', 100, 2);
function fix_image_url( $product_data, $product_id ){
if( empty( $product_data ) || empty( $product_id ) )
    {
        return $product_data;
    }
$product_image = get_post_meta( $product_id, "_bdroppy_url", true );
if( isset( $product_image['img_url'] ) && !empty($product_image['img_url'] ) ) {
    $image_override = get_post_meta($product_id, 'fb_product_image', true);
    $image_option_override = get_post_meta($product_id, '_wc_facebook_product_image_source', true);
    if ( empty($image_override ) )
        {
            add_post_meta($product_id, 'fb_product_image', $product_image['img_url'] );
        }
    if ( !empty($image_override ) )
        {
            update_post_meta($product_id, 'fb_product_image', $product_image['img_url'] );
        }
    if ( empty($image_option_override) )
        {
            add_post_meta($product_id, '_wc_facebook_product_image_source', 'custom');
        }
    if ( !empty($image_option_override) && $image_option_override = 'product' )
        {
            update_post_meta($product_id, '_wc_facebook_product_image_source', 'custom');
        }
    
}
return $product_data;}

Would be too grateful for your help!


Solution

  • I think i found the answer to add for each loop of variations:

    if ( $product->is_type( 'variable' ) ) {
            foreach ( $product->get_visible_children() as $variation_id ) {
                //gets the product variation based on its id but not used here
                $get_product_variation = wc_get_product( $variation_id );
                $image_option_override_var = get_post_meta($variation_id, '_wc_facebook_product_image_source', true);
                    if ( empty($image_option_override_var) )
                    {
                        add_post_meta($variation_id, '_wc_facebook_product_image_source', 'parent_product');
                    }
                    if ( !empty($image_option_override_var) && $image_option_override = 'product' )
                    {
                        update_post_meta($variation_id, '_wc_facebook_product_image_source', 'parent_product');
                    }
        }}