Last active
January 29, 2018 14:16
-
-
Save shreyans94/500e105a441279ac0cd945ad93ab7c3b to your computer and use it in GitHub Desktop.
Product Variation Custom Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' ); | |
function load_variation_settings_fields($variations) { | |
// duplicate the line for each field | |
/*$variations['veg_nonveg'] = get_post_meta( $variations[ 'variation_id' ], '_veg-nonveg'); | |
$variations['no_serving'] = get_post_meta( $variations[ 'variation_id' ], '_no-serving', true ); | |
$variations['serving_size'] = get_post_meta( $variations[ 'variation_id' ], '_serving-size', true ); | |
$variations['total_protein'] = get_post_meta( $variations[ 'variation_id' ], '_total-protein', true ); | |
$variations['protein_percentage'] = get_post_meta( $variations[ 'variation_id' ], '_protein-percentage', true ); | |
$variations['protein_per_serving'] = get_post_meta( $variations[ 'variation_id' ], '_protein-per-serving', true ); | |
$variations['Price_per_serving'] = get_post_meta( $variations[ 'variation_id' ], '_price-per-serving', true ); | |
$variations['price_per_gram_protein'] = get_post_meta( $variations[ 'variation_id' ], '_price-per-g-protein', true ); | |
$variations['price_per_kg'] = get_post_meta( $variations[ 'variation_id' ], '_price-per-kg', true ); | |
$variations['manufactured'] = get_post_meta( $variations[ 'variation_id' ], '_manufactured', true ); | |
$variations['weight_kg'] = get_post_meta( $variations[ 'variation_id' ], '_weight-kg', true ); | |
$variations['weight_lbs'] = get_post_meta( $variations[ 'variation_id' ], '_weight-lbs', true ); | |
$variations['product_form'] = get_post_meta( $variations[ 'variation_id' ], '_product-form', true ); | |
$variations['packaging'] = get_post_meta( $variations[ 'variation_id' ], '_packaging', true ); | |
$variations['goal_concern'] = get_post_meta( $variations[ 'variation_id' ], '_goal-concern', true ); | |
$variations['gender'] = get_post_meta( $variations[ 'variation_id' ], '_gender', true );*/ | |
$variations['veg_nonveg'] = get_post_meta( $variations[ 'variation_id' ], 'vegetariannon-vegetarian',true ); | |
$variations['no_serving'] = get_post_meta( $variations[ 'variation_id' ], 'number_of_servings', true ); | |
$variations['serving_size'] = get_post_meta( $variations[ 'variation_id' ], 'serving_size', true ); | |
$variations['total_protein'] = get_post_meta( $variations[ 'variation_id' ], 'total_protein_per_containerpouch', true ); | |
$variations['protein_percentage'] = get_post_meta( $variations[ 'variation_id' ], 'protein_percentage', true ); | |
$variations['protein_per_serving'] = get_post_meta( $variations[ 'variation_id' ], 'protein_per_serving', true ); | |
//$variations['Price_per_serving'] = get_post_meta( $variations[ 'variation_id' ], '_price-per-serving', true ); | |
//$variations['price_per_gram_protein'] = get_post_meta( $variations[ 'variation_id' ], '_price-per-g-protein', true ); | |
//$variations['price_per_kg'] = get_post_meta( $variations[ 'variation_id' ], '_price-per-kg', true ); | |
$variations['manufactured'] = get_post_meta( $variations[ 'variation_id' ], 'manufctured_in', true ); | |
$variations['weight_kg'] = get_post_meta( $variations[ 'variation_id' ], 'product_weight_kg', true ); | |
$variations['weight_lbs'] = get_post_meta( $variations[ 'variation_id' ], 'product_weight_lb', true ); | |
$variations['product_form'] = get_post_meta( $variations[ 'variation_id' ], 'form', true ); | |
$variations['packaging'] = get_post_meta( $variations[ 'variation_id' ], 'packaging', true ); | |
$variations['goal_concern'] = get_post_meta( $variations[ 'variation_id' ], 'goalconcern', true ); | |
$variations['gender'] = get_post_meta( $variations[ 'variation_id' ], 'gender', true ); | |
$sale = get_post_meta( $variations[ 'variation_id' ], '_sale_price', true); | |
if( ! empty($variations['no_serving']) ) { | |
$calculated_price_per_serving = round($sale/$variations['no_serving'],1); | |
$variations['calculated_price_per_serving'] = $calculated_price_per_serving; | |
}else { | |
$variations['calculated_price_per_serving'] = ''; | |
} | |
if( ! empty( $variations['total_protein']) ) { | |
$calculated_price_per_gram_protein = round($sale / $variations['total_protein'],1); | |
$variations['calculated_price_per_gram_protein'] = $calculated_price_per_gram_protein; | |
}else { | |
$variations['calculated_price_per_gram_protein'] = ''; | |
} | |
if(! empty($variations['weight_kg'])) { | |
$calculated_price_per = round($sale / $variations['weight_kg'],0); | |
$variations['calculated_price_per'] = $calculated_price_per; | |
}else { | |
$variations['calculated_price_per'] = ''; | |
} | |
return $variations; | |
} | |
add_action( 'woocommerce_single_product_summary', 'single_product_variable_customization', 3, 0 ); | |
function single_product_variable_customization() { | |
global $post,$product; | |
$id = $post->ID; | |
if( function_exists('get_product') ){ | |
if( $product->is_type( 'variable' ) ){ | |
?> | |
<script> | |
jQuery(function($) { | |
$variation_form = $( '.variations_form'); | |
$variation_form.on( 'show_variation', function( event, variation ){ | |
//console.log(variation); | |
if(variation.veg_nonveg == '' && variation.no_serving == '' && variation.serving_size =='' && variation.total_protein == '' && variation.protein_percentage == '' && variation.protein_per_serving == '' && variation.calculated_price_per_gram_protein == '' && variation.calculated_price_per == '' && variation.product_form == '' && variation.packaging == '' && variation.goal_concern == '' && variation.gender == '' && variation.calculated_price_per_serving == ''){ | |
console.log('empty'); | |
//$('.description_tab a').trigger('click'); | |
$('.product-tabs li:nth-child(2) a').trigger('click'); | |
$('.product-tabs li:first').css('display','none'); | |
}else { | |
console.log(variation); | |
<?php add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 ); | |
?> | |
$('.product-tabs li:first').css('display','block'); | |
$('.new_tab_tab a').trigger('click'); | |
//var interest = $("ul[data-attribute] = 'attribute_pa_weight'").find('li.active').data('interest'); | |
//console.log(variation.attributes['']); | |
var table =''; | |
table+='<table id="segment" class="table-striped">'; | |
table+='<tbody>'; | |
if(variation.attributes['attribute_pa_weight'] != undefined ) { | |
var attweight = $('label[for="pa_weight"] .iconic-was-chosen-attribute').text(); | |
table+='<tr><td><p>Weight</p></td><td><p>' + attweight+ '</p></td></tr>'; | |
} | |
if(variation.attributes['attribute_pa_flavour'] != undefined ) { | |
var flavour1 = $('#pa_flavour').find('option:selected').text(); | |
console.log(flavour1); | |
table+='<tr><td><p>Flavour</p></td><td><p>' + flavour1+ '</p></td></tr>'; | |
} | |
if(variation.attributes['attribute_pa_pack-size'] != undefined) { | |
var packsize = $('#pa_pack-size').find('option:selected').text(); | |
console.log(packsize); | |
table+='<tr><td><p>Pack Size</p></td><td><p>' + packsize+ '</p></td></tr>'; | |
} | |
if(variation.veg_nonveg != '' || variation.veg_nonveg.length != 0 ){ | |
table+='<tr><td><p>Vegetarian/Non-Vegetarian</p></td><td><p>' + variation.veg_nonveg+ '</p></td></tr>'; | |
} | |
if(variation.no_serving != '' || variation.no_serving.length != 0 ){ | |
table+='<tr><td><p>Number of Servings</p></td><td><p>' + variation.no_serving+ '</p></td></tr>'; | |
} | |
if(variation.serving_size != '' || variation.serving_size.length != 0 ){ | |
table+='<tr><td><p>Serving Size</p></td><td><p>' + variation.serving_size+ '</p></td></tr>'; | |
} | |
if(variation.total_protein != '' || variation.total_protein.length != 0 ){ | |
table+='<tr><td><p>Total Protein Per Container/Pouch (Kg)</p></td><td><p>' + variation.total_protein+ 'Kg</p></td></tr>'; | |
} | |
if(variation.protein_percentage != '' || variation.protein_percentage.length != 0 ){ | |
table+='<tr><td><p>Protein Percentage (%)</p></td><td><p>' + variation.protein_percentage+ '%</p></td></tr>'; | |
} | |
if(variation.protein_per_serving != '' || variation.protein_per_serving.length != 0 ){ | |
table+='<tr><td><p>Protein Per Serving (g)</p></td><td><p>' + variation.protein_per_serving+ 'g</p></td></tr>';} | |
if(variation.calculated_price_per_serving != '' || variation.calculated_price_per_serving.length != 0 ){ | |
table+='<tr><td><p>Price Per Serving</p></td><td><p><span class="woocommerce-Price-currencySymbol">₹</span>' + variation.calculated_price_per_serving+ '</p></td></tr>';} | |
if(variation.calculated_price_per_gram_protein != '' || variation.calculated_price_per_gram_protein.length != 0 ){ | |
table+='<tr><td><p>Price Per Gram of Protein</p></td><td><p><span class="woocommerce-Price-currencySymbol">₹</span>' + variation.calculated_price_per_gram_protein+ '</p></td></tr>'; | |
} | |
if(variation.calculated_price_per != '' || variation.calculated_price_per.length != 0 ){ | |
table+='<tr><td><p>Price Per Kg</p></td><td><p><span class="woocommerce-Price-currencySymbol">₹</span>' + variation.calculated_price_per+ '</p></td></tr>'; | |
} | |
if(variation.manufactured != '' || variation.manufactured.length != 0 ){ | |
table+='<tr><td><p>Manufactured In</p></td><td><p>' + variation.manufactured+ '</p></td></tr>'; | |
} | |
if(variation.weight_kg != '' || variation.weight_kg.length != 0 ){ | |
table+='<tr><td><p>Weight (kg)</p></td><td><p>' + variation.weight_kg+ '</p></td></tr>'; | |
} | |
if(variation.weight_lbs != '' || variation.weight_lbs.length != 0 ){ | |
table+='<tr><td><p>Weight (lb)</p></td><td><p>' + variation.weight_lbs+ '</p></td></tr>'; | |
} | |
if(variation.product_form != '' || variation.product_form.length != 0 ){ | |
table+='<tr><td><p>Form</p></td><td><p>' + variation.product_form+ '</p></td></tr>'; | |
} | |
if(variation.packaging != '' || variation.packaging.length != 0 ){ | |
table+='<tr><td><p>Packaging</p></td><td><p>' + variation.packaging+ '</p></td></tr>'; | |
} | |
if(variation.goal_concern != '' || variation.goal_concern.length != 0 ){ | |
table+='<tr><td><p>Goal/Concern</p></td><td><p>' + variation.goal_concern+ '</p></td></tr>'; | |
} | |
if(variation.gender != '' || variation.gender.length != 0 ){ | |
table+='<tr><td><p>Gender</p></td><td><p>' + variation.gender+ '</p></td></tr>'; | |
} | |
table+='</tbody>'; | |
table+='</table>'; | |
$('#segment').remove(); | |
$('#table-container').append(table); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
}elseif($product->is_type( 'simple' )){ | |
?> | |
<script> | |
jQuery(function($) { | |
//console.log('simple'); | |
//console.log('<?php echo $id; ?>'); | |
<?php | |
$variations = get_post_meta_simple_variable($id); | |
$empty_notempty = empty_notempty($variations); | |
if($empty_notempty){ | |
?> | |
//console.log('empty'); | |
<?php | |
}else{ | |
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 ); | |
?> | |
var table =''; | |
table+='<table id="segment" class="table-striped">'; | |
table+='<tbody>'; | |
<?php if($variations['veg_nonveg'] != ''){ | |
?> | |
table+='<tr><td><p>Vegetarian/Non-Vegetarian</p></td><td><p><?php echo $variations['veg_nonveg']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['no_serving'] != ''){ | |
?> | |
table+='<tr><td><p>Number of Servings</p></td><td><p><?php echo $variations['no_serving']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['serving_size'] != ''){ | |
?> | |
table+='<tr><td><p>Serving Size</p></td><td><p><?php echo $variations['serving_size']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['total_protein'] != ''){ | |
?> | |
table+='<tr><td><p>Total Protein Per Container/Pouch (Kg)</p></td><td><p><?php echo $variations['total_protein']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['protein_percentage'] != ''){ | |
?> | |
table+='<tr><td><p>Protein Percentage (%)</p></td><td><p><?php echo $variations['protein_percentage']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['protein_per_serving'] != ''){ | |
?> | |
table+='<tr><td><p>Protein Per Serving (g)</p></td><td><p><?php echo $variations['protein_per_serving']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['calculated_price_per_serving'] != ''){ | |
?> | |
table+='<tr><td><p>Price Per Serving</p></td><td><p><span class="woocommerce-Price-currencySymbol">₹</span><?php echo $variations['calculated_price_per_serving']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['calculated_price_per_gram_protein'] != ''){ | |
?> | |
table+='<tr><td><p>Price Per Gram of Protein</p></td><td><p><span class="woocommerce-Price-currencySymbol">₹</span><?php echo $variations['calculated_price_per_gram_protein']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['calculated_price_per'] != ''){ | |
?> | |
table+='<tr><td><p>Price Per Kg</p></td><td><p><span class="woocommerce-Price-currencySymbol">₹</span><?php echo $variations['calculated_price_per']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['manufactured'] != ''){ | |
?> | |
table+='<tr><td><p>Manufactured In</p></td><td><p><?php echo $variations['manufactured']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['product_form'] != ''){ | |
?> | |
table+='<tr><td><p>Form</p></td><td><p><?php echo $variations['product_form']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['packaging'] != ''){ | |
?> | |
table+='<tr><td><p>Packaging</p></td><td><p><?php echo $variations['packaging']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['goal_concern'] != ''){ | |
?> | |
table+='<tr><td><p>Goal/Concern</p></td><td><p><?php echo $variations['goal_concern']; ?></p></td></tr>'; | |
<?php } ?> | |
<?php if($variations['gender'] != ''){ | |
?> | |
table+='<tr><td><p>Gender</p></td><td><p><?php echo $variations['gender']; ?></p></td></tr>'; | |
<?php } ?> | |
table+='</tbody>'; | |
table+='</table>'; | |
$('#segment').remove(); | |
$('#table-container').append(table); | |
<?php | |
} | |
?> | |
}); | |
</script> | |
<?php | |
} | |
} | |
} | |
function get_post_meta_simple_variable($post_variationid){ | |
$variations['veg_nonveg'] = get_post_meta( $post_variationid, 'vegetariannon-vegetarian',true ); | |
$variations['no_serving'] = get_post_meta($post_variationid, 'number_of_servings', true ); | |
$variations['serving_size'] = get_post_meta( $post_variationid, 'serving_size', true ); | |
$variations['total_protein'] = get_post_meta( $post_variationid, 'total_protein_per_containerpouch', true ); | |
$variations['protein_percentage'] = get_post_meta( $post_variationid, 'protein_percentage', true ); | |
$variations['protein_per_serving'] = get_post_meta( $post_variationid, 'protein_per_serving', true ); | |
//$variations['Price_per_serving'] = get_post_meta( $post_variationid, '_price-per-serving', true ); | |
//$variations['price_per_gram_protein'] = get_post_meta( $post_variationid, '_price-per-g-protein', true ); | |
//$variations['price_per_kg'] = get_post_meta( $post_variationid, '_price-per-kg', true ); | |
$variations['manufactured'] = get_post_meta( $post_variationid, 'manufctured_in', true ); | |
$variations['weight_kg'] = get_post_meta( $post_variationid, 'product_weight_kg', true ); | |
$variations['weight_lbs'] = get_post_meta($post_variationid, 'product_weight_lb', true ); | |
$variations['product_form'] = get_post_meta( $post_variationid, 'form', true ); | |
$variations['packaging'] = get_post_meta( $post_variationid, 'packaging', true ); | |
$variations['goal_concern'] = get_post_meta( $post_variationid, 'goalconcern', true ); | |
$variations['gender'] = get_post_meta( $post_variationid, 'gender', true ); | |
$sale = get_post_meta( $post_variationid, '_sale_price', true); | |
if( ! empty($variations['no_serving']) && $variations['no_serving'] !='' ) { | |
$calculated_price_per_serving = round($sale/$variations['no_serving'],1); | |
$variations['calculated_price_per_serving'] = $calculated_price_per_serving; | |
} | |
if( ! empty( $variations['total_protein']) && $variations['total_protein'] !='') { | |
$calculated_price_per_gram_protein = round($sale / $variations['total_protein'],1); | |
$variations['calculated_price_per_gram_protein'] = $calculated_price_per_gram_protein; | |
} | |
if(! empty($variations['weight_kg']) && $variations['weight_kg'] !='') { | |
$calculated_price_per = round($sale / $variations['weight_kg'],0); | |
$variations['calculated_price_per'] = $calculated_price_per; | |
} | |
return $variations; | |
} | |
function woo_rename_tabs( $tabs ) { | |
$tabs['new_tab'] = array( | |
'title' => __( 'Product Info', 'woocommerce' ), | |
'priority' => 0, | |
'callback' => 'woo_tab_content' | |
); | |
return $tabs; | |
} | |
function woo_tab_content(){ | |
?> | |
<div id="table-container"></div> | |
<?php | |
} | |
function empty_notempty($variations){ | |
/*if(empty($variations['veg_nonveg']) && empty($variations['no_serving']) && empty($variations['serving_size']) && empty($variations['total_protein']) && empty($variations['protein_percentage']) && empty($variations['protein_per_serving']) && empty($variations['manufactured']) && empty($variations['weight_kg']) && empty($variations['weight_lbs']) && empty($variations['product_form']) && empty($variations['packaging']) && empty($variations['goal_concern']) ){ | |
return true; | |
}else { | |
return false; | |
}*/ | |
$count = 0; | |
foreach($variations as $key => $val){ | |
if($val !='' || ! empty($val)){ | |
$count++; | |
} | |
if($count > 0){ | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} | |
// Display Price For Variable Product With Same Variations Prices | |
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) { | |
if ($value['price_html'] == '') { | |
$value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>'; | |
} | |
return $value; | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment