I am trying to create variable product with Toolset form.
we have created custom code for variable product
add_action('cred_save_data', 'convert_to_variable_product',10,3);
function convert_to_variable_product($post_id, $form_data) {
global $wpdb;
$array = array();
if(isset($_POST['variation_tool']) && (!empty($_POST['variation_tool']))){
$variation_tool_number = $_POST['variation_tool']['number'];
$variation_tool_color = $_POST['variation_tool']['color'];
$variation_tool_quantity = $_POST['variation_tool']['quantity'];
for($k=0;$k<count($variation_tool_color);$k++){
$array[$variation_tool_quantity[$k]."-".$k] = $variation_tool_color[$k]." - ".$variation_tool_number[$k];
}
}
$ids = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_parent='$post_id' and post_type='product_variation'",ARRAY_A);
//print_r($ids);
$id_del='';
if(!empty($ids)){
foreach($ids as $id){
$id_del .= "'".$id['ID']."',";
}
}
$del_k = rtrim($id_del,',');
$wpdb->query("DELETE FROM $wpdb->posts WHERE ID in (".$del_k.")");
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id in (".$del_k.")");
//$availableColors = array("Red - 34267546","Black - 56643453","green - 53534543","Red - 65653534","Black - 54354423","Pink - 54354254");
$availableColors = $array;
//$availableColors = array("Red","Pink");
//print_r($availableColors);
if(isset($_POST['is_variable_product']) && ($_POST['is_variable_product']==1)){
//print_r(implode(' | ', $availableColors));
wp_set_object_terms($post_id, 'variable', 'product_type');
wp_set_object_terms($post_id, implode(' | ', $availableColors), 'color');
$product_attributes = array();
$product_attributes['color'] = array(
'name' => 'Color',
'value' => implode(' | ', $availableColors),
'position' => 0,
'is_visible' => 0,
'is_variation' => 1,
'is_taxonomy' => 0
);
update_post_meta($post_id, '_product_attributes', $product_attributes);
$_transient_wc_product_children = array();
foreach ($availableColors as $quantity => $color) {
$quantity_t = explode("-",$quantity);
//print_r($color."==".$quantity_t[0]);
//print_r($quantity_t);
$colorClean = preg_replace("/[^0-9a-zA-Z_-]+/", "", $color);
$post_name = 'product-' . $post_id . '-color-' . $colorClean;
$my_post = array (
'post_title' => 'Color ' . $color . ' for #' . $post_id,
'post_name' => $post_name,
'post_status' => 'publish',
'post_parent' => $post_id,
'post_type' => 'product_variation',
'guid' => home_url() . '/?product_variation=' . $post_name
);
$attID = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_name like '$post_name'");
//echo "**".$post_name."**";
//echo "**".$attID."**";
if ($attID < 1) {
$attID = wp_insert_post($my_post);
}
//echo "--".$attID."--";
update_post_meta($attID, 'attribute_color', $color);
update_post_meta($attID, '_sale_price', $_POST['sale_price_var']);
update_post_meta($attID, '_regular_price', $_POST['regular_price_var']);
update_post_meta($attID, '_sku', $post_name);
update_post_meta($attID, '_virtual', 'no');
update_post_meta($attID, '_downloadable', 'no');
update_post_meta($attID, '_stock', $quantity_t[0]);
if($quantity_t[0]>0){
update_post_meta($attID, '_manage_stock', 'yes');
update_post_meta($attID, '_stock_status', 'instock');
}else{
update_post_meta($attID, '_manage_stock', 'no');
update_post_meta($attID, '_stock_status', 'outofstock');
}
$_transient_wc_product_children[] = $attID;
}
$array_transient = array('all'=>$_transient_wc_product_children,'visible'=>$_transient_wc_product_children);
update_option('_transient_wc_product_children_'.$post_id,$array_transient);
}else{
wp_set_object_terms($post_id, 'simple', 'product_type');
}
}
add_shortcode('form_to_html_variatons','form_to_html_variatons_function');
function form_to_html_variatons_function(){
global $wpdb,$post;
$product = wc_get_product($post->ID);
$available_variations = array();
$div_show = "style='display:none'";
$is_variable_yes = "";
$is_variable_no = "checked='checked'";
if( $product->is_type( 'variable' ) ){
$div_show = "style='display:block'";
$is_variable_no = "";
$is_variable_yes = "checked='checked'";
$available_variations = $product->get_available_variations();
}
$html = "";
$html .= '<ul class="wpt-form-set wpt-form-set-radios wpt-form-set-radios-post_status">
<li class="wpt-form-item wpt-form-item-radio">
<input type="radio" '.$is_variable_yes.' id="ccp_yes" name="is_variable_product" value="1" class="is_variable_product wpt-form-radio form-radio radio" /><label class="wpt-form-label wpt-form-radio-label" for="ccp_yes">Variable Product</label>
</li>
<li class="wpt-form-item wpt-form-item-radio">
<input type="radio" '.$is_variable_no.' id="ccp_no" name="is_variable_product" value="2" class="is_variable_product wpt-form-radio form-radio radio" /><label class="wpt-form-label wpt-form-radio-label" for="ccp_no">Simple Product</label>
</li>
</ul>';
$html .= '<div class="row div_to_append" '.$div_show.'>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<label>Regular Price For varitions</label>
<input type="text" class="form-control" name="regular_price_var" value="'.$available_variations[0]['display_regular_price'].'">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<label>Sale Price For varitions</label>
<input type="text" class="form-control" name="sale_price_var" value="'.$available_variations[0]['display_price'].'">
</div>
</div>';
if(!empty($available_variations)){
foreach($available_variations as $variation){
$var = explode(' - ',$variation['attributes']['attribute_color']);
$var_s = get_post_meta($variation['variation_id'],'_stock',true);
$max_qty = $var_s>0?$var_s:0;
$html .= '<div class="me_remove row col-lg-12 col-md-12">
<div class="col-lg-4 col-md-4">
<div class="form-group">
<label>Car Color</label>
<input value="'.$var[0].'" type="text" class="form-control" name="variation_tool[color][]">
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="form-group">
<label>Car Number</label>
<input value="'.$var[1].'" type="text" class="form-control" name="variation_tool[number][]">
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="form-group">
<label>Quantity</label>
<input value="'.$max_qty.'" type="text" class="form-control" name="variation_tool[quantity][]">
</div>
</div>
<a href="javascript:void(0)" class="remove_d">X</a>
</div>';
}
}else{
$html .= '<div class="me_remove row col-lg-12 col-md-12">
<div class="col-lg-4 col-md-4">
<div class="form-group">
<label>Car Color</label>
<input type="text" class="form-control" name="variation_tool[color][]">
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="form-group">
<label>Car Number</label>
<input type="text" class="form-control" name="variation_tool[number][]">
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="form-group">
<label>Quantity</label>
<input type="text" class="form-control" name="variation_tool[quantity][]">
</div>
</div>
<a href="javascript:void(0)" class="remove_d">X</a>
</div>';
}
$html .= '</div>';
$html .= '<div class="row" ><a href="javascript:void(0)" '.$div_show.' id="add_colr_nubr_div" class="btn btn-primary btn-lg submit-style wpt-form-submit form-submit submit" >Add More</a></div>';
$html .= '<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(\'body\').on(\'click\',\'.remove_d\',function(){
jQuery(this).closest(\'.me_remove\').remove();
});
jQuery(\'.is_variable_product\').click(function(){
if(jQuery(this).val()==1){
jQuery(\'.div_to_append\').css(\'display\',\'block\');
jQuery(\'#add_colr_nubr_div\').css(\'display\',\'block\');
}else{
jQuery(\'#add_colr_nubr_div\').css(\'display\',\'none\');
jQuery(\'.div_to_append\').css(\'display\',\'none\');
}
});
jQuery(\'#add_colr_nubr_div\').click(function(){
jQuery(\'.div_to_append\').append(\'<div class="me_remove row col-lg-12 col-md-12"><div class="col-lg-4 col-md-4"><div class="form-group"><label>Car Color</label><input type="text" class="form-control" name="variation_tool[color][]"></div></div><div class="col-lg-4 col-md-4"><div class="form-group"><label>Car Number</label><input type="text" class="form-control" name="variation_tool[number][]"></div></div><div class="col-lg-4 col-md-4"><div class="form-group"><label>Quantity</label><input type="text" class="form-control" name="variation_tool[quantity][]"></div></div><a href="javascript:void(0)" class="remove_d">X</a></div>\');
});
});
</script>
<style>
.remove_d{
position: absolute;
right: 0;
top: 36px;
}
</style>';
return $html;
}
then we have added a shortcode in our cred form
so now we can choose a simple product or variable. when its variable extra fields show up.. regular price for all variations, sale price for all variations, color field, number field, quantity and as well Add more button for variations.
Now once the variations added and the price of all variations for example like following:
Regular price: 50000 / Sale Price: 40000
after submitting the form the price change to:
Regular price: 42735 / Sale Price: 34188
Now if i try to Edit the product again i can see the variations prices are like following:
Regular price: 42735.042735043 / Sale Price: 34188.034188034
In the Backend we can see the right prices of the variations:
Regular price: 50000 / Sale Price: 40000
I have tried to deactivate all other functions the i have created: didnt help
I have tried to deactivate most of my plugins including Toolset plugins: didnt help
I have tried to update plugins one by one: didnt help
Is this Issue in the code we have created? did we follow the cred hook instruction in our code?
The thing is we are working on two sites ..one for test where the code work perfectly and no issues with variable prices. and one for the final code, that we have created and in this one we have this issue.