Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce自定义Ajax添加到购物车:如何设置自定义价格?_Php_Jquery_Ajax_Wordpress_Woocommerce - Fatal编程技术网

Php WooCommerce自定义Ajax添加到购物车:如何设置自定义价格?

Php WooCommerce自定义Ajax添加到购物车:如何设置自定义价格?,php,jquery,ajax,wordpress,woocommerce,Php,Jquery,Ajax,Wordpress,Woocommerce,我正在尝试添加签出时点击事件中的产品。 该产品的价格是通过外部API动态计算的,因此它在woocommerce中注册为价格为0。我正在尝试将此产品添加到购物车(工作),然后将该产品的价格设置为我从API收到的价格(不工作)。以下是相关代码: 点击AJAX调用: jQuery('#theftdaminsurance').on('click', function(){ jQuery.ajax({ type: "POST",

我正在尝试添加签出时点击事件中的产品。 该产品的价格是通过外部API动态计算的,因此它在woocommerce中注册为价格为0。我正在尝试将此产品添加到购物车(工作),然后将该产品的价格设置为我从API收到的价格(不工作)。以下是相关代码:

点击AJAX调用:

       jQuery('#theftdaminsurance').on('click', function(){
        jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: {
            action: 'add_product_to_cart_checkout',
            // add your parameters here
            price: <?php echo $year_dam_price; ?>,
            productid: ins_product_id
        },
        success: function (output) {
        document.getElementById("loaderdiv").style.display = "none";
        jQuery(document.body).trigger("update_checkout");
        }
        });
        });
jQuery('theftdaminsurance')。在('click',function()上{
jQuery.ajax({
类型:“POST”,
url:“/wp admin/admin ajax.php”,
数据:{
操作:“将商品添加到购物车结帐”,
//在此处添加您的参数
价格:,
productid:ins\u product\u id
},
成功:功能(输出){
document.getElementById(“loaderdiv”).style.display=“无”;
jQuery(document.body).trigger(“update_checkout”);
}
});
});
我检查了这里的一切,电话打得很好,传递了所有正确的信息。 下面是正在调用的函数:

add_action('wp_ajax_add_product_to_cart_checkout', 'add_product_to_cart_checkout');

// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_add_product_to_cart_checkout', 'add_product_to_cart_checkout');

function add_product_to_cart_checkout() {
    $product_id = $_REQUEST['productid'];
    $price = floatval($_REQUEST['price']);
    $ebike_ids = array(17386,17385,17382,17378,17375,17372,17370,17369,17364,16132,16130,4561,4550,3490,2376);
    $found = false;
    //check if there is something in cart
    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
        //Delete all preexisting insurance products and find qty of ebikes
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product_idebike = $cart_item['product_id'];
            if ( ($cart_item['product_id'] == "16600") || ($cart_item['product_id'] == "16653") || ($cart_item['product_id'] == "16654") || ($cart_item['product_id'] == "16655") ||($cart_item['product_id'] == "16659") || ($cart_item['product_id'] == "16660")) {
                        WC()->cart->remove_cart_item( $cart_item_key );
            }           
        //get ebike quantity
    for ($x = 0; $x <= 14; $x++) {
        if ($product_idebike == $ebike_ids[$x]) {
            $quantity =  $cart_item['quantity'];
            break;
        }
    }
        }
            // if command is not to remove add relevant products
                if ($price != "-1") {
                WC()->cart->add_to_cart( $product_id, $quantity );
                foreach( WC()->cart->get_cart() as $cart_item ){
                    $id = $cart_item['product_id'];
                    if ($product_id == $id) {
                        $cart_item['data']->set_price( $price );
                    }
                }
                }
            
    }

}
add_action('wp_ajax_add_product_to_cart_checkout','add_product_to_cart_checkout');
//为未经身份验证的用户注册ajax操作
添加操作(“wp\u ajax\u nopriv\u将产品添加到购物车结帐”、“将产品添加到购物车结帐”);
函数添加商品到购物车结账(){
$product\U id=$\U请求['productid'];
$price=floatval($_请求['price']);
$ebike_id=array(17386173851738217378173751737217370173691736416132161304561455034902376);
$found=false;
//检查购物车里是否有东西
如果(sizeof(WC()->cart->get_cart())>0){
//删除所有先前存在的保险产品并查找ebikes的数量
foreach(WC()->cart->get_cart()作为$cart\u item\u key=>$cart\u item){
$product_idebike=$cart_项目['product_id'];
如果($cart_item['product_id']=“16600”)| |($cart_item['product_id']=“16653”)|($cart_item['product_id']=“16654”)|($cart_item['product_id']=“16655”)|($cart_item['product_id']=“16659”)|($cart_item['product id'=“16660; 16660;){
WC()->购物车->删除购物车项目($cart\u item\u key);
}           
//获取ebike数量
对于($x=0;$x购物车->添加到购物车($product\U id,$QUOTE);
foreach(WC()->cart->get_cart()作为$cart_项目){
$id=$cart\u item['product\u id'];
如果($product\U id==$id){
$cart_item['data']->set_price($price);
}
}
}
}
}
在这里,我基本上仔细地调试了所有东西,除了set_price函数外,所有东西都正常工作。我无法让它将产品的价格更新为AJAX调用中传递的价格。在更新签出后和之前,价格仍然为0。我调用或使用此函数的方式是否错误


非常感谢您的帮助!

似乎您正在使事情变得比Ajax接收器函数中应有的复杂得多

请注意,您可以在使用
WC\u cart
add\u to\u cart()
方法时添加自定义购物车项目数据,因此您将添加您的保险价格作为自定义购物车项目数据,然后使用它来设置价格

以下是您重新访问的Ajax PHP接收器函数:

add_action('wp_ajax_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
add_action('wp_ajax_nopriv_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
function add_product_to_cart_checkout() {
    if ( isset($_POST['productid']) && isset($_POST['price']) && ! WC()->cart->is_empty() ) {
        $product_id     = intval($_POST['productid']);
        $price          = floatval($_POST['price']);
        $ebike_ids      = array(17386,17385,17382,17378,17375,17372,17370,17369,17364,16132,16130,4561,4550,3490,2376);
        $insurance_ids  = array(16600,16653,16654,16655,16659,16660);
        $found          = false;
        $ebike_quantity = 0;

        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            // Delete all preexisting insurance products
            if ( in_array( $cart_item['product_id'],  $insurance_ids ) ) {
                WC()->cart->remove_cart_item( $cart_item_key );
            }

            // Get ebikes total quantity
            if ( in_array( $cart_item['product_id'],  $ebike_ids ) ) {
                $ebike_quantity +=  $cart_item['quantity'];
            }
        }

        // If command is not to remove add insurance product with price as custom cart item data
        if ( $price >= 0 && $cart_item['insurance_price'] > 0 ) {
            WC()->cart->add_to_cart( $product_id, $ebike_quantity, 0, array(), array( 'insurance_price' => $price ) );
        }
        die();
    }
}
然后设置您将使用的价格:

// Set insurance price from custom cart item data
add_action( 'woocommerce_before_calculate_totals', 'set_insurance_price' );
function set_insurance_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
       return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['insurance_price']) && $cart_item['insurance_price'] > 0 ) {
            $cart_item['data']->set_price( $cart_item['insurance_price'] );
        }
    }
}

现在它应该可以工作了。

我意识到,如果我使用像这样简单的东西:添加动作('woocommerce'u before'u calculate'u totals','set'u insurance'u price');函数set'u insurance'u price($cart\u object){foreach($cart\u object->get\u cart()as$hash=>$value){$value['data']->set\u price(10)}价格被切换到10,那么有没有办法使其适应AJAX调用?非常感谢!它工作得很好!我刚刚删除了isset($\u POST['productid']),因为我没有将其传递给删除请求,并且您可以解释为什么添加了:&&&$cart\u item['insurance\u price']>0)在“添加产品”部分中,由于未定义此项,因此会阻止添加产品。再次感谢你的帮助!