Woocommerce增加了自定义费用,试图通过AJAX操作复选框

时间:2016-03-15 22:45:11

标签: php ajax wordpress woocommerce

我希望能否得到一些关于这里发生的事情的信息。

我在结帐的总计部分添加了额外的费用,它增加了小计的10%。

现在这可行,因为我通过插件添加了以下代码片段:

add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
global $woocommerce;

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$subtotal = $woocommerce->cart->subtotal;
$fee_percentage = 0.1; // 10 percent
$fee = ($subtotal * $fee_percentage);
$woocommerce->cart->add_fee( 'Seguro de Envio', $fee, true, 'standard' );
}

它确实有效,但我必须能够通过复选框激活它。

复选框是通过进入购物车总数并使用以下代码创建的:

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
<tr class="fee">
<th><?php echo esc_html( $fee->name ); ?> <input type="checkbox" id="envio">         </th>
<td id="seguroenvio" data-title="<?php echo esc_html( $fee->name ); ?>"><?     php wc_cart_totals_fee_html( $fee ); ?></td>
</tr>
<?php endforeach; ?>

同样在cart-totals.php文件的底部,我添加了这个:

<?php do_action( 'woocommerce_after_cart_totals' ); ?>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script type="text/javascript">
$("#envio").on("change", function() {
var mee = ($(this).is(":checked") == true) ? "yes" : "no";
$.ajax({
type: 'post',
url: 'http://valaidp.com/mrelectronicstest/wp-       content/plugins/woocommerce/templates/cart/fields.php',
data: {status:mee},
success: function(output) {
$('#seguroenvio').text(output);
}
});
});
</script>
<?php
function custom_checkbox_checker () {
if ( is_checkout() ) {
wp_enqueue_script( 'jquery' ); ?>
<script type="text/javascript">

jQuery(document).ready( function (e) {

var $ = jQuery;

// wc_checkout_params is required to continue, ensure the object exists
if ( typeof wc_checkout_params === 'undefined' )
return false;

var updateTimer,
dirtyInput = false,
xhr;

function update_shipping() {

if ( xhr ) xhr.abort();

var liftgate = $( '#envio:checked' ).val();

var data = {
action: 'woocommerce_update_order_review',
security: wc_checkout_params.update_order_review_nonce,
liftgate: liftgate,
post_data: $( 'form' ).serialize()
};

xhr = $.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: data,
success: function( response ) {
if ( response ) {
//$( '#order_review' ).html( $.trim( response ) );
$( '#order_review' ).find( '#envio:checked' ).trigger('click');
$( 'body' ).trigger('updated_checkout' );
}
}
});
}

jQuery('#envio').on('click', function() {
update_shipping();
$woocommerce->cart->add_fee( 'Seguro de Envio', $fee, true, 'standard' );
});

});

</script>
<?php }
}

最后,在cart-totals.php的同一位置,我添加了一个我创建的fields.php文件,添加了这段代码:

<?php
global $woocommerce;
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );

/*global $woocommerce;

$subtotal = $woocommerce->cart->subtotal;
$fee_percentage = 0.1; // 10 percent
$fee = ($subtotal * $fee_percentage);
$woocommerce->cart->add_fee( 'Seguro de Envio', $fee, true, 'standard' );
echo $fee." ";

require('../../../../../wp-includes/functions.php'); */

function endo_handling_fee() {
global $woocommerce;

if (is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$subtotal = $woocommerce->cart->subtotal;
$fee_percentage = 0.1; // 10 percent
$fee = ($subtotal * $fee_percentage);
if($_POST["status"] == "yes"){
$woocommerce->cart->add_fee( 'Seguro de Envio', $fee, true, 'standard' );
echo $fee." ";
}
else
{
$woocommerce->cart->add_fee( 'Seguro de Envio', 0, true, 'standard' );
echo "0";
}
echo 0;
}
?>

出现复选框,但单击时不会触发任何内容。

非常感谢任何帮助。

Woocommerce版本4.4.2

网站网址:valaidp.com/mrelectronicstest

0 个答案:

没有答案