
<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


class Elex_FB_Add_Custom_Fields {

	public function __construct() {
		add_filter( 'woocommerce_product_data_tabs', array( $this, 'elex_fpf_add_product_tab' ) );
		add_action( 'woocommerce_product_data_panels', array( $this, 'elex_fpf_add_custom_metas' ) );
		add_action( 'woocommerce_process_product_meta', array( $this, 'elex_fpf_save_metas' ) );
		if ( elex_fb_premium() ) {
			add_action( 'woocommerce_product_after_variable_attributes', array( $this, 'elex_fpf_variable_custom_meta_fields' ), 10, 3 );
		}
		add_action( 'woocommerce_save_product_variation', array( $this, 'elex_fpf_save_variable_metas' ), 10, 1 );
	}

	//function to add a prodcut tab in product page
	public function elex_fpf_add_product_tab( $product_data_tabs ) {
		$product_data_tabs['elex_fpf_product_data'] = array(
			'label'  => __( 'ELEX Facebook Feed', 'elex-product-feed' ),
			'target' => 'elex_fpf_custom_meta_fields',
			'class'  => array(),
		);
		return $product_data_tabs;
	}

	public function elex_fpf_add_custom_metas() {
		?>
		<div id="elex_fpf_custom_meta_fields" class="panel woocommerce_options_panel hidden">
		<?php include  'elex-html-add-product-custom-fields.php' ; ?>
		</div>
		<?php
	}

	

	public function elex_fpf_save_metas( $post_id ) {
		if ( ! ( isset( $_POST['woocommerce_meta_nonce'] ) || wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) ) { // Input var okay.
			return false;
		}
		$elex_fpf_gtin = isset( $_POST['_elex_fpf_gtin'] ) ? sanitize_text_field( $_POST['_elex_fpf_gtin'] ) : '';
		
		$elex_fpf_simple_gtin = isset( $_POST['_elex_fpf_simple_gtin'] ) ? sanitize_text_field( $_POST['_elex_fpf_simple_gtin'] ) : '';
		$elex_fpf_brand       = isset( $_POST['_elex_fpf_brand'] ) ? sanitize_text_field( $_POST['_elex_fpf_brand'] ) : '';
		$elex_fpf_mpn         = isset( $_POST['_elex_fpf_mpn'] ) ? sanitize_text_field( $_POST['_elex_fpf_mpn'] ) : '';
		$elex_fpf_sync_enable = isset( $_POST['_elex_fpf_sync_enable'] ) ? sanitize_text_field( $_POST['_elex_fpf_sync_enable'] ) : 'enable';
		$elex_fpf_prod_desc   = isset( $_POST['_elex_fpf_prod_desc'] ) ? sanitize_text_field( $_POST['_elex_fpf_prod_desc'] ) : '';
		$elex_fpf_image_link  = isset( $_POST['_elex_fpf_image_link'] ) ? sanitize_text_field( $_POST['_elex_fpf_image_link'] ) : '';
		$elex_fpf_price       = isset( $_POST['_elex_fpf_price'] ) ? sanitize_text_field( $_POST['_elex_fpf_price'] ) : '';

		$additional_field_india        = isset( $_POST['_additional_field_india'] ) ? sanitize_text_field( $_POST['_additional_field_india'] ) : '';      
		$elex_fpf_importer_name        = isset( $_POST['_elex_fpf_importer_name'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_name'] ) : '';      
		$elex_fpf_importer_street1     = isset( $_POST['_elex_fpf_importer_street1'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_street1'] ) : '';
		$elex_fpf_importer_street2     = isset( $_POST['_elex_fpf_importer_street2'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_street2'] ) : '';
		$elex_fpf_importer_city        = isset( $_POST['_elex_fpf_importer_city'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_city'] ) : '';
		$elex_fpf_importer_region      = isset( $_POST['_elex_fpf_importer_region'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_region'] ) : '';
		$elex_fpf_importer_postal_code = isset( $_POST['_elex_fpf_importer_postal_code'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_postal_code'] ) : '';
		$elex_fpf_importer_country     = isset( $_POST['_elex_fpf_importer_country'] ) ? sanitize_text_field( $_POST['_elex_fpf_importer_country'] ) : '';
		$elex_fpf_origin_country       = isset( $_POST['_elex_fpf_origin_country'] ) ? sanitize_text_field( $_POST['_elex_fpf_origin_country'] ) : '';

		$elex_fpf_manufacturer_name    = isset( $_POST['_elex_fpf_manufacturer_name'] ) ? sanitize_text_field( $_POST['_elex_fpf_manufacturer_name'] ) : '';
		$elex_fpf_manufacturer_address = isset( $_POST['_elex_fpf_manufacturer_address'] ) ? sanitize_text_field( $_POST['_elex_fpf_manufacturer_address'] ) : '';
		$product                       = wc_get_product( $post_id );
		$product->update_meta_data( '_additional_field_usa', isset( $_POST['_additional_field_usa'] ) );

		$elex_fpf_size_usa = isset( $_POST['_elex_fpf_size_usa'] ) ? sanitize_text_field( $_POST['_elex_fpf_size_usa'] ) : '';
		$elex_fpf_sell_usa = isset( $_POST['_elex_fpf_sell_usa'] ) ? sanitize_text_field( $_POST['_elex_fpf_sell_usa'] ) : '';

		if ( isset( $elex_fpf_origin_country ) ) {
			$product->update_meta_data( '_elex_fpf_origin_country', $elex_fpf_origin_country );
		}
		if ( isset( $additional_field_india ) ) {
			$product->update_meta_data( '_additional_field_india', $additional_field_india );
		}
		
		if ( isset( $elex_fpf_importer_name ) ) {
			$product->update_meta_data( '_elex_fpf_importer_name', $elex_fpf_importer_name );
		}
		if ( isset( $elex_fpf_importer_street1 ) ) {
			$product->update_meta_data( '_elex_fpf_importer_street1', $elex_fpf_importer_street1 );

		}
		if ( isset( $elex_fpf_importer_street2 ) ) {
			$product->update_meta_data( '_elex_fpf_importer_street2', $elex_fpf_importer_street2 );
		}
		if ( isset( $elex_fpf_importer_city ) ) {
			$product->update_meta_data( '_elex_fpf_importer_city', $elex_fpf_importer_city );
		}
		if ( isset( $elex_fpf_importer_region ) ) {
			$product->update_meta_data( '_elex_fpf_importer_region', $elex_fpf_importer_region );
		}
		if ( isset( $elex_fpf_importer_postal_code ) ) {
			$product->update_meta_data( '_elex_fpf_importer_postal_code', $elex_fpf_importer_postal_code );
		}
		if ( isset( $elex_fpf_importer_country ) ) {
			$product->update_meta_data( '_elex_fpf_importer_country', $elex_fpf_importer_country );
		}
		if ( isset( $elex_fpf_importer_street1 ) || isset( $elex_fpf_importer_street2 ) || isset( $elex_fpf_importer_city ) || isset( $elex_fpf_importer_region ) || isset( $elex_fpf_importer_postal_code ) ) {
			$importer_addr                = array();
			$importer_addr['street1']     = $elex_fpf_importer_street1;
			$importer_addr['street2']     = $elex_fpf_importer_street2 ;
			$importer_addr['city']        = $elex_fpf_importer_city ;
			$importer_addr['region']      = $elex_fpf_importer_region ;
			$importer_addr['postal_code'] = $elex_fpf_importer_postal_code ;
			$importer_addr['country']     = $elex_fpf_importer_country ;

			$product->update_meta_data( '_elex_fpf_importer_addr', $importer_addr );
		}
		
		if ( isset( $elex_fpf_manufacturer_name ) ) {
			$product->update_meta_data( '_elex_fpf_manufacturer_name', $elex_fpf_manufacturer_name );
		}
		if ( isset( $elex_fpf_manufacturer_address ) ) {
			$product->update_meta_data( '_elex_fpf_manufacturer_address', $elex_fpf_manufacturer_address );
		}
		if ( isset( $elex_fpf_brand ) ) {
			$product->update_meta_data( '_elex_fpf_brand', $elex_fpf_brand );
		}

		if ( isset( $elex_fpf_mpn ) ) {
			$product->update_meta_data( '_elex_fpf_mpn', esc_attr( $elex_fpf_mpn ) );
		}

		if ( isset( $elex_fpf_gtin ) ) {
			$product->update_meta_data( '_elex_fpf_gtin', esc_attr( $elex_fpf_gtin ) );
		}
		if ( isset( $elex_fpf_simple_gtin ) ) {
			$product->update_meta_data( '_elex_fpf_simple_gtin', esc_attr( $elex_fpf_simple_gtin ) );
		}
		
		
		if ( isset( $elex_fpf_sync_enable ) ) {
			$product->update_meta_data( '_elex_fpf_sync_enable', esc_attr( $elex_fpf_sync_enable ) );
		}
		if ( isset( $elex_fpf_prod_desc ) ) {
			$product->update_meta_data( '_elex_fpf_prod_desc', esc_attr( $elex_fpf_prod_desc ) );
		}
		if ( isset( $elex_fpf_image_link ) ) {
			$product->update_meta_data( '_elex_fpf_image_link', esc_attr( $elex_fpf_image_link ) );
		}
		if ( isset( $elex_fpf_price ) ) {
			$product->update_meta_data( '_elex_fpf_price', esc_attr( $elex_fpf_price ) );
		}

		if ( isset( $elex_fpf_size_usa ) ) {
			$product->update_meta_data( '_elex_fpf_size_usa', esc_attr( $elex_fpf_size_usa ) );
		}
		if ( isset( $elex_fpf_sell_usa ) ) {
			$product->update_meta_data( '_elex_fpf_sell_usa', esc_attr( $elex_fpf_sell_usa ) );
		}
		$product->save();       
	}

	public function elex_fpf_variable_custom_meta_fields( $variation_count, $variation_id, $variation_prod ) {
		$product = wc_get_product( $variation_prod->ID );
		echo '<div class="elex_fb_custom_fields" >';
		esc_html( '<h4 style="text-align: left;">' ) . __( 'ELEX Catalog Feed for Facebook & Instagram', 'elex-product-feed' ) . '</h4>';
		wp_nonce_field( basename( __FILE__ ), 'elex-epf-variation-custom-fields-nonce' );

		woocommerce_wp_select(
			array(
				'id'          => '_elex_fpf_variable_gtin_type[' . $variation_count . ']',
				'custom_attributes' => array( 'variation_num' => '' . $variation_count ),

				'label'       => __( 'Select GTIN Type', 'elex-product-feed' ),
				'options'     => array(
					'' => '',
					'elex_fpf_gtin_upc'  => 'GTIN-USA',
					'elex_fpf_gtin_ean' => 'GTIN-EAN',
					'elex_fpf_gtin_jan'  => 'GTIN-JAN',
					'elex_fpf_gtin_isbn' => 'GTIN-ISBN',
				),
				'value'         => $product->get_meta( '_elex_fpf_variable_gtin_type' ),
				'desc_tip'    => 'true',
				'description' => __( 'Select your GTIN Type', 'elex-product-feed' ),
			)
		);
		if ( $product->get_meta( '_elex_fpf_variable_gtin_type' ) ) {
			$hidden = '';
		} else {
			$hidden = 'hide';
		}
		woocommerce_wp_text_input(
			array(
				'id'            => '_elex_fpf_gtin[' . $variation_count . ']',

				'custom_attributes' => array(
					'variation_num' => '' . $variation_count,
				),
				'label'         => __( '<br>Enter-GTIN', 'elex-product-feed' ),
				'desc_tip'      => 'true',
				'description'   => __( 'The Global Trade Item Number (GTIN) is an identifier for trade items', 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_gtin' ),
				'wrapper_class' => 'form-row-full elex_fpf_gtin_number ' . $hidden,
			)
		);
		esc_html( '<div class="_elex_fpf_gtin_var_error hide" style="color:red;"> ' ) . __( 'Please Enter Numeric Value for GTIN', 'elex-product-feed' ) . '</div>';

		
		woocommerce_wp_text_input(
			array(
				'id'            => '_elex_fpf_variable_mpn[' . $variation_count . ']',
				'label'         => __( 'MPN', 'elex-product-feed' ),
				'desc_tip'      => 'true',
				'description'   => __( 'A manufacturer part number (MPN) is a series of numbers and/or letters given to a part by its manufacturer', 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_mpn' ),
				'wrapper_class' => 'form-row-full',
			)
		);
		
		woocommerce_wp_textarea_input(
			array(
				'id'            => '_elex_fpf_variable_prod_desc[' . $variation_count . ']',
				'label'         => __( 'Product Description', 'elex-product-feed' ),
				'desc_tip'      => 'true',
				'description'   => __( 'Enter Facebook product description.', 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_prod_desc' ),
				'wrapper_class' => 'form-row-full',
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'            => '_elex_fpf_variable_image_link[' . $variation_count . ']',
				'label'         => __( 'Product Image Link', 'elex-product-feed' ),
				'desc_tip'      => 'true',
				'description'   => __( 'Facebook Product Image Link', 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_image_link' ),
				'wrapper_class' => 'form-row-full',
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'            => '_elex_fpf_variable_price[' . $variation_count . ']',
				'label'         => __( 'Product Price', 'elex-product-feed' ),
				'desc_tip'      => 'true',
				'description'   => __( 'Facebook product Price', 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_price' ),
				'wrapper_class' => 'form-row-full',
			)
		);
		global $post;
		$var_post_id = $post->ID;
		$product_var = wc_get_product( $var_post_id );
		?>
		
			<p class="form-field _additional_field_india[<?php esc_attr_e( '' . $variation_count ); ?>]_field">
				<input type="checkbox" name="_additional_field_india[<?php esc_attr_e( '' . $variation_count ); ?>]" id="_additional_field_india[<?php esc_attr_e( '' . $variation_count ); ?>]" variation_num = "<?php esc_attr_e( '' . $variation_count ); ?>"
				<?php 
				$product             = wc_get_product( $variation_prod->ID );
				$checked_india_field = $product->get_meta( '_additional_field_india' ) ? $product->get_meta( '_additional_field_india' ) : $product_var->get_meta( '_additional_field_india' );

			
				if ( $checked_india_field ) {
					echo 'checked';
					$hidden = '';
				} else {
					$hidden = 'hide';
				}

				?>

				"> Show Additional required field for selling in  India </input> 
			</p>


<?php
		$product = wc_get_product( $variation_prod->ID );

		wp_kses_post( '<div class="elex_fb_india_field ' . $hidden . ' " variation_num = "' . $variation_count . '" >' );

		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_name[' . $variation_count . ']',
				'label'       => __( 'Importers Name', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( "If the country of origin is not India, provide the legal entity name of the item's importer.", 'elex-product-feed' ),
				'value'  => $product->get_meta( '_elex_fpf_importer_name' ) ? $product->get_meta( '_elex_fpf_importer_name' ) : $product_var->get_meta( '_elex_fpf_importer_name' ), 

			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_street1[' . $variation_count . ']',
				'label'       => __( 'Street 1', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Required. The first line of the street address.', 'elex-product-feed' ),
				'value'  => $product->get_meta( '_elex_fpf_importer_street1' ) ? $product->get_meta( '_elex_fpf_importer_street1' ) : $product_var->get_meta( '_elex_fpf_importer_street1' ), 
				
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_street2[' . $variation_count . ']',
				'label'       => __( 'Street 2', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Optional. Second line of the street address.', 'elex-product-feed' ),
				'value'  => $product->get_meta( '_elex_fpf_importer_street2' ) ? $product->get_meta( '_elex_fpf_importer_street2' ) : $product_var->get_meta( '_elex_fpf_importer_street2' ), 


			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_city[' . $variation_count . ']',
				'label'       => __( 'City', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Required. The city name ', 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_importer_city' ) ? $product->get_meta( '_elex_fpf_importer_city' ) : $product_var->get_meta( '_elex_fpf_importer_city' ), 
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_region[' . $variation_count . ']',
				'label'       => __( 'Region', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( ' Optional. The region, state, county or province.', 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_importer_region' ) ? $product->get_meta( '_elex_fpf_importer_region' ) : $product_var->get_meta( '_elex_fpf_importer_region' ), 
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_postal_code[' . $variation_count . ']',
				'label'       => __( 'Postal Code', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Optional.The postal code or zip code', 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_importer_postal_code' ) ? $product->get_meta( '_elex_fpf_importer_postal_code' ) : $product_var->get_meta( '_elex_fpf_importer_postal_code' ), 
			)
		);

		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_importer_country[' . $variation_count . ']',
				'label'       => __( 'Country', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Required. Enter the two-letter ISO country code.', 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_importer_country' ) ? $product->get_meta( '_elex_fpf_importer_country' ) : $product_var->get_meta( '_elex_fpf_importer_country' ), 
			)
		);

		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_origin_country[' . $variation_count . ']',
				'label'       => __( 'Origin Country', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( "The item's country of origin. Enter the two-letter ISO country code.", 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_origin_country' ) ? $product->get_meta( '_elex_fpf_origin_country' ) : $product_var->get_meta( '_elex_fpf_origin_country' ),

			)
		);

		echo '<h5 style="text-align: left;">Manufacturers Info</h5>';


		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_manufacturer_name[' . $variation_count . ']',
				'label'       => __( 'Manufacture Name', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Required. Manufacturer name', 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_manufacturer_name' ) ? $product->get_meta( '_elex_fpf_manufacturer_name' ) : $product_var->get_meta( '_elex_fpf_manufacturer_name' ),

			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_manufacturer_address[' . $variation_count . ']',
				'label'       => __( 'Manufacture Address', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'Required. Manufacturer Address', 'elex-product-feed' ),
				'value'       => $product->get_meta( '_elex_fpf_manufacturer_address' ) ? $product->get_meta( '_elex_fpf_manufacturer_address' ) : $product_var->get_meta( '_elex_fpf_manufacturer_address' ),

			)
		);

		echo '</div>';

?>
		
		<p class="form-field _additional_field_usa[<?php esc_attr_e( '' . $variation_count ); ?>]_field">
				<input type="checkbox" name="_additional_field_usa[<?php esc_attr_e( '' . $variation_count ); ?>]" id="_additional_field_usa[<?php esc_attr_e( '' . $variation_count ); ?>]" variation_num = "<?php esc_attr_e( '' . $variation_count ); ?>" 
				<?php 
				$product           = wc_get_product( $variation_prod->ID );
				$checked_usa_field = $product->get_meta( '_additional_field_usa', true ) ? $product->get_meta( '_additional_field_usa', true ) : $product_var->get_meta( '_additional_field_usa', true );
				
				if ( $checked_usa_field ) {
					$hidden = '';
					echo 'checked';
				} else {
					$hidden = 'hide';
				}
				?>

				"> Show Additional required field for selling in USA </input> 
			</p>
		<?php
		
		wp_kses_post( '<div class="elex_fb_usa_field  ' . $hidden . ' " variation_num = "' . $variation_count . '" >' );

		
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_size_usa[' . $variation_count . ']',
				'label'       => __( 'Size', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( 'The size of the item. Enter the size as a word, abbreviation or number, such as "small", "XL", "12" or "one size". Character limit: 200.', 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_size_usa' ) ? $product->get_meta( '_elex_fpf_size_usa' ) : $product->get_meta( '_elex_fpf_size_usa' ),
				
			)
		);
		woocommerce_wp_text_input(
			array(
				'id'          => '_elex_fpf_sell_usa[' . $variation_count . ']',
				'label'       => __( 'Sell on Facebook', 'elex-product-feed' ),
				'desc_tip'    => 'true',
				'description' => __( "The quantity of this item that you have available to sell. Enter a whole number.  To prevent overselling, an item's quantity is automatically reduced each time a purchase order is confirmed through checkout. When the quantity reaches 0, the item is marked as sold out in your shop on Facebook and Instagram. After 56 days, out-of-stock items stop appearing in your shop.", 'elex-product-feed' ),
				'value'         => $product->get_meta( '_elex_fpf_sell_usa' ) ? $product->get_meta( '_elex_fpf_sell_usa' ) : $product_var->get_meta( '_elex_fpf_sell_usa' ),

			)
		);

		echo '</div>';

		echo '</div>';

	}

	public function elex_fpf_save_variable_metas( $id ) {
		if ( ! isset( $_POST['elex-epf-variation-custom-fields-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['elex-epf-variation-custom-fields-nonce'] ), basename( __FILE__ ) ) ) {
			return false;
		}
		$count = isset( $_POST['variable_post_id'] ) ? max( array_keys( array_map( 'sanitize_text_field', wp_unslash( $_POST['variable_post_id'] ) ) ) ) : 0;
		for ( $i = 0; $i <= $count; $i++ ) {
			if ( isset( $_POST['variable_post_id'][ $i ] ) ) {
				$product = wc_get_product( sanitize_text_field( $_POST['variable_post_id'][ $i ] ) );
				if ( isset( $_POST['_elex_fpf_gtin'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_gtin', stripslashes( sanitize_text_field( $_POST['_elex_fpf_gtin'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_variable_gtin_type'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_variable_gtin_type', stripslashes( sanitize_text_field( $_POST['_elex_fpf_variable_gtin_type'][ $i ] ) ) );
				}
			
				if ( isset( $_POST['_elex_fpf_variable_mpn'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_mpn', stripslashes( sanitize_text_field( $_POST['_elex_fpf_variable_mpn'][ $i ] ) ) );
				}
			
				if ( isset( $_POST['_elex_fpf_variable_prod_desc'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_prod_desc', stripslashes( sanitize_text_field( $_POST['_elex_fpf_variable_prod_desc'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_variable_image_link'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_image_link', stripslashes( sanitize_text_field( $_POST['_elex_fpf_variable_image_link'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_variable_price'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_price', stripslashes( sanitize_text_field( $_POST['_elex_fpf_variable_price'][ $i ] ) ) );
				}
				if ( isset( $_POST['_additional_field_india'][ $i ] ) ) {
					$product->update_meta_data( '_additional_field_india', stripslashes( sanitize_text_field( $_POST['_additional_field_india'][ $i ] ) ) );
				}
				if ( isset( $_POST['_additional_field_usa'][ $i ] ) ) { 
					$product->update_meta_data( '_additional_field_usa', stripslashes( sanitize_text_field( $_POST['_additional_field_usa'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_importer_name'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_name', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_name'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_importer_street1'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_street1', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_street1'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_importer_street2'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_street2', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_street2'][ $i ] ) ) );
				}
				
				if ( isset( $_POST['_elex_fpf_importer_city'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_city', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_city'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_importer_region'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_region', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_region'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_importer_postal_code'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_postal_code', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_postal_code'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_importer_country'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_importer_country', stripslashes( sanitize_text_field( $_POST['_elex_fpf_importer_country'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_origin_country'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_origin_country', stripslashes( sanitize_text_field( $_POST['_elex_fpf_origin_country'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_manufacturer_name'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_manufacturer_name', stripslashes( sanitize_text_field( $_POST['_elex_fpf_manufacturer_name'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_manufacturer_address'][ $i ] ) ) {
					$product->update_meta_data( '_elex_fpf_manufacturer_address', stripslashes( sanitize_text_field( $_POST['_elex_fpf_manufacturer_address'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_size_usa'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_size_usa', stripslashes( sanitize_text_field( $_POST['_elex_fpf_size_usa'][ $i ] ) ) );
				}
				if ( isset( $_POST['_elex_fpf_sell_usa'][ $i ] ) ) { 
					$product->update_meta_data( '_elex_fpf_sell_usa', stripslashes( sanitize_text_field( $_POST['_elex_fpf_sell_usa'][ $i ] ) ) );
				}  
				$product->save();         
			}
		}
	}

}

new Elex_FB_Add_Custom_Fields();
