我正在尝试使用自定义插件代码向当前订单添加运费。我已经写了一个插件来显示一些单选按钮,用于传送选项和拾取选项,并为无线电检查事件嵌入一个javascript文件。以下是我的插件代码:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function radio_button_checks(){
wp_register_script('wp_head',plugin_dir_url(__FILE__).'radio_check.js',array( 'jquery' ), '1.0', true);
wp_enqueue_script('wp_head');
}
function delivery_form() {
$content = '<div style="width:90%; margin-top:10px; float:left; background-color:rgba(255, 255, 255, 0.85); border-radius:5px; padding:5%;">';
$content .= '<h3 id="first-self-id"><input type="radio" value="self_product_pick_up" class="distance_between_text" name="delivery" id="self-delivery" />PICK UP AT THE TROPICAL FRUIT TRIBE HQ KICTHEN AT 1/1 GENERAL BRIDGES CREC, DACEYVILLE 2032</h2>';
$content .= '<div id="self-delivery-functionality" class="delivery_functionality" style="width: 100%; display: inline-block;">';
$content .= '<div class="self_delivery_day" style="width: 45%; float: left; padding: 0% 2%; text-align: right; ">';
$content .= '<h4><input type="radio" value="tuesday" class="distance_between_text" />TUE</h4>';
$content .= '</div>';
$content .= '<div class="self_delivery_time" style="width: 45%; float: left; padding: 0% 2%; text-align: left;">';
$content .= '<h4><input type="radio" name="pickup_time" value="9AM-12PM" class="distance_between_text" />9AM-12PM</h4>';
$content .= '<h4><input type="radio" name="pickup_time" value="12PM-3PM" class="distance_between_text" />12PM-3PM</h4>';
$content .= '<h4><input type="radio" name="pickup_time" value="3PM-6PM" class="distance_between_text" />3PM-6PM</h4>';
$content .= '<h4><input type="radio" name="pickup_time" value="6PM-8PM" class="distance_between_text" />6PM-8PM</h4>';
$content .= '</div>';
$content .= '</div>';
$content .= '<h3 id="second-notSelf-id"><input type="radio" value="shop_keeper_delivery" class="distance_between_text" checked="checked" name="delivery" id="other-one-deliver" />DELIVERY OPTION ($12 only)</h2>';
$content .= '<div id="seller-delivery-functionality" style="display:block;">';
$content .= '<div class="delivery-info-text" style="text-align: left;">';
$content .= '<p style="margin:5px; font-size:1em;">PLEASE KEEP IN MIND THAT THIS IS A FROZEN PRODUTS AND NEEDS TO BE STORED IN FREEZER IMMEDIATLY UPON DELIVERY. PLEASE ENSURE YOU ARE AVAILABLE TO TAKE THIS DELIVERY AT THE DROP OFF LOCATION SPECIFIED.</p>';
$content .= '</div>';
$content .= '<div class="delivery_functionality" style="width: 100%; display: inline-block;">';
$content .= '<div class="shop_keeper_delivery_day" style="width: 45%; float: left; padding: 0% 2%; text-align: right;">';
$content .= '<h4><input type="radio" name="delivery_day" value="monday" class="distance_between_text" />Mon</h4>';
$content .= '<h4><input type="radio" name="delivery_day" value="fri" class="distance_between_text" />Fri</h4>';
$content .= '</div>';
$content .= '<div class="shop_keeper_delivery_time" style="width: 45%; float: left; padding: 0% 2%; text-align: left;">';
$content .= '<h4><input type="radio" name="delivery_time" value="9AM-12PM" name="" class="distance_between_text" />9AM-12PM</h4>';
$content .= '<h4><input type="radio" name="delivery_time" value="12PM-3PM" class="distance_between_text" />12PM-3PM</h4>';
$content .= '<h4><input type="radio" name="delivery_time" value="3PM-6PM" class="distance_between_text" />3PM-6PM</h4>';
$content .= '<h4><input type="radio" name="delivery_time" value="6PM-8PM" class="distance_between_text" />6PM-8PM</h4>';
$content .= '</div>';
$content .= '</div>';
$content .= '</div>';
$content .= '<input type="hidden" id="shipping_rate" value="0.00" /> ';
$content .= '</div>';
echo $content;
}
add_action("woocommerce_after_cart_table","delivery_form");
add_action("wp_enqueue_scripts","radio_button_checks");
和我的radio_check.js文件包含以下代码:
jQuery(document).ready(function(){
/* Checkout CheckBox Functionality for Self Delivery */
jQuery("#self-delivery-functionality").hide("slow");
jQuery("#seller-delivery-functionality").show("slow");
jQuery("#self-delivery").click( function() {
if(jQuery(this).is(':checked')) {
jQuery("#self-delivery-functionality").show("slow");
jQuery("#seller-delivery-functionality").hide("slow");
jQuery("#seller-delivery-functionality").find("input[type='checkbox']").prop("checked", false);
jQuery("#seller-delivery-functionality").find("input[type='radio']").prop("checked", false);
}
});
/* Checkout CheckBox Functionality for having Delivery Functionality */
jQuery("#other-one-deliver").click( function() {
if(jQuery(this).is(':checked')) {
jQuery("#seller-delivery-functionality").show("slow");
jQuery("#self-delivery-functionality").hide("slow");
jQuery("#self-delivery-functionality").find("input[type='checkbox']").prop("checked", false);
jQuery("#self-delivery-functionality").find("input[type='radio']").prop("checked", false);
}
});
jQuery('input[name=delivery_time]').click(function(){
if(jQuery(this).is(':checked'))
{
var curr_amount_dol = jQuery('.cart-subtotal .amount').html().replace('$','');
var total_amount = parseFloat(curr_amount_dol) + parseFloat(12.00);
//alert(total_amount.toFixed(2));
jQuery('#shipping_rate').val('$'+total_amount.toFixed(2));
jQuery('.order-total .amount').html('$'+total_amount.toFixed(2));
}
});
}); //ready function ends
此代码正在更新总金额(仅显示)以及运费(12美元),但是当我点击结帐按钮时,我将无法获得下一页的运费总额。
我应该在此代码中添加什么来更新选中的单选按钮上的运费?
注意:现在总金额通过javascript更新。
答案 0 :(得分:0)
'woocommerce_cart_calculate_fees'解决了我的问题。我刚刚添加了以下代码并对我的javascript进行了一些修改。
<script lenguage="javascript">
function preparar() {
document.forms[0].elements.numeroLicencia.focus();
document.forms[0].elements.nombreConductor.disabled = true;
document.forms[0].elements.botonEnviar.disabled = true;
document.forms[0].elements.botonActualizar.disabled = true;
}
function municipiosDepartamento() {
var arregloMunicipiosDepartamento = new Array();
var posicionMunicipio = document.forms[0].elements.menuDepartamento.value;
arregloMunicipiosDepartamento = municipiosColombia(posicionMunicipio);
if(document.forms[0].elements.menuMunicipios.options.length > 1){
var totalMunicipios = document.forms[0].elements.menuMunicipios.length;
for (var i = 1; i < totalMunicipios; i ++){
document.forms[0].elements.menuMunicipios.options[1] = null;
}
}
if(document.forms[0].elements.menuDepartamento.value === "x"){
document.forms[0].elements.menuMunicipios.selectedItem = 0;
document.forms[0].elements.menuMunicipios.disabled = true;
}
else
{
document.forms[0].elements.menuMunicipios.options.length = arregloMunicipiosDepartamento.length;
for (var i = 0; i < arregloMunicipiosDepartamento.length; i ++) {
var opcionTemporal = new Option(arregloMunicipiosDepartamento[i], (i+1));
***document.forms[0].elements.menuMunicipios.options[i+1].text = opcionTemporal.text;
document.forms[0].elements.menuMunicipios.options[i+1].value = opcionTemporal.value;***
}
document.forms[0].elements.menuMunicipios.disabled = false;
}
}
function municipiosColombia(posicion) {
var antioquia, atlantico, arregloTodos, arregloMunicipiosDepartamento = new Array();
antioquia=["Medellín","Abejorral","Abriaqui","Alejandria"];
atlantico = ["Barranquilla","Baranoa","Campo De La Cruz","Candelaria"];
arregloTodos = [antioquia, atlantico];
arregloMunicipiosDepartamento=arregloTodos[posicion];
return arregloMunicipiosDepartamento;
}
</script>
对于javascript我只是将同一页面与查询参数'shipping_method'一起重定向
function woo_add_cart_fee() {
$req = $_REQUEST['shipping_method'];
if($req=='delivery')
{
WC()->session->set('ship_val','12.00');
}
else if($req=='pickup')
{
WC()->session->set('ship_val','00.00');
}
//checkship($_REQUEST['shipping_method']);
WC()->cart->add_fee('Shipping ', WC()->session->get('ship_val'));
}
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');
在单选按钮上选中我的页面是否重定向查询?shipping_method = delivery /?shipping_method = pickup 我在插件文件中的php代码检查参数值并使用以下方式设置运费:
jQuery('input[name=delivery_time]').click(function(){
if(jQuery(this).is(':checked'))
{
window.location='http://examplesite.com/cart/?shipping_method=delivery';
}
});
jQuery('input[name=pickup_time]').click(function(){
if(jQuery(this).is(':checked'))
{
window.location='http://examplesite.com/cart/?shipping_method=pickup[';
}
});
并且购物车页面和结帐页面上的费用也会更新。