I use a plugin in woocommerce that adds a credit card payment method.
I have customized the plugin and I added a dropdown as a payment field in which the customer can choose istallments. The installments have an interest rate so I want everytime that the customer chooses the number of installments he wants it will update the orders total accordingly.
I have used a piece of code with jquery and ajax that posts the value of the dropdown...
While the checkout field later updates normaly the price doesn't change because the $_POST
parameter doesn't seem to get any values.
How can solve this issue and get the price to be updated.
The ajax and the php functions are in the same file don't know if that has to do anything. Here is the jquery:
jQuery(document).ready(function () {
jQuery('#installments').change(function () {
var billing_district = jQuery('#installments').val();
console.log(installments);
var data = {
action: 'woocommerce_apply_district',
security: wc_checkout_params.apply_district_nonce,
district: billing_district
};
jQuery.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: data,
success: function (code) {
console.log(code);
if (code === '0') {
//$form.before(code);
jQuery('body').trigger('update_checkout');
}
},
dataType: 'html'
});
return false;
});
});
Here is the Screenshot:
(edit)
Here is the php code
<?php
// reproduce/modify this function in your class
$price = $woocommerce->cart->total;
if ($price > 15) {
$installments = static::getInstallments($price);
$table = "";
$table ='<label for="installments">Δόσεις</label>';
$table.='<select class="francotecnologia_wc_parcpagseg_table" id="installments" class="update_totals_on_change">';
$table.='<option value="0">Χωρίς Δόσεις</option>';
/* $table .= 'francotecnologia_wc_parcpagseg_table_with_variation_id_' . ($variationId > 0 ? $variationId : '0') . '" ';
$table .= ($variationDisplay === false ? 'style="display:none"' : '');
$table .= '>';
// $tableColspan = (2 + (static::$showColumnTotal?1:0)) *static::$numberOfTableColumns;
// if (static::$tableHeaderMessage != '') {
// $table .= '<tr><th class="francotecnologia_wc_parcpagseg_table_header_message_tr_th" colspan="'
//. $tableColspan . '">' . static::$tableHeaderMessage . '</th> </tr>';
// }
// $table .= '<tr class="francotecnologia_wc_parcpagseg_table_header_tr">';
// $table .= str_repeat('<th>' . static::$language['Installments'] . '</th><th>' . static::$language['Amount'] . '</th>'
// . (static::$showColumnTotal ? '<th>' . static::$language['Total'] . '</th>':''), static::$numberOfTableColumns);
// $table .= '</tr>';
// $table .= '</thead><tbody>'; */
$tdCounter = 0;
foreach (range(1, $installments) as $parcel) {
$calc = static::calculateInstallment($price, $parcel);
$tdCounter = 1 + $tdCounter % static::$numberOfTableColumns;
// if ($tdCounter == 1) {
// $table .= '<tr>';
//}.$parcel.' x '.wc_price($calc->price).' = '. wc_price($calc->total).
$table.= '<option value="'.$parcel.'">'.$parcel.'</options>';
//$table .= '<th>' . $parcel . '</th><td>' . wc_price($calc->price) . '</td>' . (static::$showColumnTotal ? '<td>' . wc_price($calc->total) . '</td>' : '');
// if ($tdCounter == static::$numberOfTableColumns) {
// $table .= '</tr>';
//}
}
// if (substr( $table, -5 ) != '</tr>') {
// $table .= '</tr>';
// }
// $table .= '</tbody>';
//if (static::$tableFooterMessage != '') {
// $table .= '<tfoot><tr><th class="francotecnologia_wc_parcpagseg_table_footer_message_tr_th" colspan="'
// . $tableColspan . '">' . static::$tableFooterMessage . '</th></tr></tfoot>';
// }
$table .= '</select>';
echo $table;
} else {
return '';
}
}
add_action('wp_ajax_woocommerce_apply_district', 'calculate', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_district', 'calculate', 10);
wp_enqueue_script('district', get_template_directory_uri() . '/js/district.js', array('jquery'));
wp_localize_script('district', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));
function calculate() {
if (isset($_POST['installments'])){
global $woocommerce;
$district = $_POST['installments'];
if ($district == 1) {
$val = 100;
} elseif ($district == 2){
$val = 200;
}
session_start();
$_SESSION['val'] = $val;
}
}
add_action('woocommerce_cart_calculate_fees', 'wpi_add_ship_fee');
function wpi_add_ship_fee() {
@session_start();
$customshipcost = $_SESSION['val'];
WC()->cart->add_fee('Υποσύνολο με τόκους ', $customshipcost);
}
?>
I found this in a tutorial for custom fields.
http://phpwpinfo.com/how-to-update-shipping-cost-in-cart-dynamically-based-on-a-custom-field-in-woocommerce/
There is a link that shows how to make custom fields but I used plain php!