首页 > 解决方案 > 在 woo-commerce wordpress 中添加到购物车操作之前应用 jQuery

问题描述

在用户继续添加到购物车之前,我需要一个确认弹出窗口。如果用户在确认框中单击是,则应将产品添加到购物车并重定向到购物车页面,就像在正常流程中发生的那样,在 woo commerce wordpress 中。我在woocommerce/templates/single-product/add-to-cart/simple.php.

当单击“是”按钮时,它会重定向到页面并且不将产品添加到购物车。以下是我的相同代码:

var cat_name = document.getElementById('cat_name').value;
    if(cat_name != ''){
        var el = document.getElementById('add_to_cart_submit');
        el.addEventListener('submit', function(e){
            e.preventDefault();
            $("#dialog-confirm").dialog({
                resizable: false,
                modal: true,
                title: "Terms and Conditions",
                height: "auto",
                width: 400,
                open: function(event, ui) { 
                    $('.ui-widget-overlay').bind('click', function()
                    { 
                        $("#dialog-confirm").dialog('close');
                    }); },
                create: function (e, ui) {
                    var pane = $(this).dialog("widget").find(".ui-dialog-buttonpane")
                    $("<label class='confirm-check' ><input  type='checkbox'/> I acknowledge I have read and accepted the above statements</label><p id='insideStyle' style='display:none;color:red; margin: 0; font-size: 14px;'>Required*</p>").prependTo(pane)
                },
                buttons: {
                    "Yes": function () {
                        if($('.confirm-check input').is(":checked")){
                            $(this).dialog('close');
                            $("#add_to_cart_submit").submit();
                        }else{
                            $("#insideStyle").show();
                        }
                    },
                        "No": function () {
                        $(this).dialog('close');
                    }
                }
            });
        }, false);
    }

标签: jquerywordpresswoocommerce

解决方案


推荐阅读