简体   繁体   English

Woocommerce - 发送自定义 通过 ajax 发送自定义设置的产品价格到购物车

[英]Woocommerce - Send custom send custom set product price via ajax to cart

I have set a custom discounted product price in a variable (i just want to show the discount for an order bump) and added a checkbox which sends an ajax request when it´s checked so the discounted product gets added to the cart.我在变量中设置了自定义折扣产品价格(我只想显示订单增加的折扣)并添加了一个复选框,该复选框在选中时发送 ajax 请求,以便将折扣产品添加到购物车。

The product gets added to the cart fine, but it doesn´t have the discounted price.该产品可以很好地添加到购物车,但它没有折扣价。

Any ideas what i´m doing wrong here?任何想法我在这里做错了什么?

The checkbox with the data-attributes:具有数据属性的复选框:

<input type="checkbox" class="add-to-cart-checkbox" data-product-id="' . $upsell_product_id . '" data-product-price="' . $discounted_price . '" />



The Ajax Script for adding the product with the discounted price: Ajax 添加打折商品的脚本:

$('.add-to-cart-checkbox').click(function() {
                var product_id = $(this).data('product-id');
                var product_price = $(this).data('product-price');

                if (this.checked) {
                    $.post(woocommerce_params.ajax_url, {
                        'action': 'woocommerce_add_to_cart',
                        'product_id': product_id,
                        'product_price': product_price,
                    }, function(response) {
                        console.log(response);
                    });
                }
            });

This problem is interesting and not easy to solve.这个问题很有趣,也不容易解决。 The problem is that the cart item is recalculated all the time and price will get back to original after you set it.问题是购物车商品一直在重新计算,价格会在您设置后恢复原状。 Here is one approach you can use to trick the WooCommerce.这是您可以用来欺骗 WooCommerce 的一种方法。

First, we check if there is product_price in $_POST variable when we add the product into the cart.首先,我们在将产品添加到购物车时检查$_POST变量中是否有product_price If the product_price is there we store the special price into the $wc_session so we can use it later, for example when other product is added and cart recalculated.如果product_price在那里,我们将特价存储到$wc_session ,以便我们以后可以使用它,例如,当添加其他产品并重新计算购物车时。

add_action( 'woocommerce_add_to_cart', 'store_special_price_to_session', 10, 3 );
function store_special_price_to_session( $cart_item_key, $product_id, $quantity ) {
    if( isset( $_POST['product_price'] )) {
        // Sanitize the product price
        $product_price = wp_kses( $_POST['product_price'], array() );
        // Get the WC_Session instance
        $wc_session = WC()->session;
        $wc_session->set( 'special_price_'.$product_id, $product_price );
    }
}

Then, in the second snippet, we change the price of the item every time when the cart is recalculated.然后,在第二个片段中,每次重新计算购物车时,我们都会更改商品的价格。 We pull the special price we stored in the session and replace the original cart item price.我们拉出我们存储在 session 中的特价并替换原来的购物车商品价格。 If we don't do this, the price will return to the original product price.如果我们不这样做,价格将恢复到原始产品价格。

add_action( 'woocommerce_before_calculate_totals', 'apply_special_price_to_cart_item', 10, 1 );
function apply_special_price_to_cart_item( $cart_object ) {
    // Get the WC_Session instance
    $wc_session = WC()->session;

    foreach ( $cart_object->get_cart() as $cart_item ) {
        // Get the special price from the session data
        $special_price = $wc_session->get( 'special_price_'.$cart_item['product_id'] );

        // Sanitize the special price
        $special_price = wp_kses( $special_price, array() );

        if( ! empty( $special_price ) ) {
            $cart_item['data']->set_price( $special_price );
        }
    }
}

Important note : This code is a proof of concept, it should do the work you need but someone can easily change the javascript call and set a price of any product to $0 (for current user only).重要说明:此代码是概念证明,它应该可以完成您需要的工作,但有人可以轻松更改 javascript 电话并将任何产品的价格设置为 0 美元(仅限当前用户)。 It would be better to use the special price stored on the server instead of trusting something coming from the javascript request.最好使用存储在服务器上的特价而不是相信来自 javascript 请求的东西。 Also, it's highly recommended to use wp_verify_nonce to make the request more secured.此外,强烈建议使用wp_verify_nonce使请求更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM