简体   繁体   English

通过 URL 添加到购物车上的 WooCommerce 自定义购物车商品价格

[英]WooCommerce custom cart item price on add-to cart via the URL

I have a simple product with a default price value of 100.我有一个简单的产品,默认价格值为 100。

As you choose a product it must go directly to checkout, this I handle simply like this:当你选择一个产品时,它必须直接去结账,我的处理方式是这样的:

/checkout/?add-to-cart=78

However, it must be possible to override the price of this product, as it's a voulenteer donation amount.但是,必须可以覆盖此产品的价格,因为它是志愿者捐赠金额。

I am trying this:我正在尝试这个:

/checkout/?add-to-cart=78&donation=200

And in my functions.php I have:在我的functions.php中,我有:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;



    foreach ( $cart->get_cart() as $cart ) {
        $id = $cart['data']->get_id();

        if ($id === 78 && $_GET['donation']) {
            $price = $_GET['donation'];
        }
        else {
            $price = $cart['data']->get_price();
        }

        $cart['data']->set_price( $price );

    }   
}

When the page is first loaded, it has set the price to 200. However, the JS doing an ajax call after reload to this url:当页面第一次加载时,它已将价格设置为 200。但是,JS 在重新加载到此 url 后执行 ajax 调用:

/?wc-ajax=update_order_review

Resets the price to 100.将价格重置为 100。

How can I stop it from resetting the price?我怎样才能阻止它重置价格?

You need to set the donation as custom cart item data on add-to-cart event, avoiding losing it when checkout get refreshed by ajax.您需要在 add-to-cart 事件中将捐赠设置为自定义购物车项目数据,避免在 ajax 刷新结帐时丢失它。

In the following code, we catch the donation value that we set as custom cart item data, then we add it to the corresponding cart item price.在以下代码中,我们捕获我们设置为自定义购物车项目数据的捐赠值,然后将其添加到相应的购物车项目价格中。

This happen via the url with something like: /checkout/?add-to-cart=78&donation=200这通过 url 发生,例如: /checkout/?add-to-cart=78&donation=200

add_filter( 'woocommerce_add_cart_item_data', 'catch_and_save_submited_donation', 10, 2 );
function catch_and_save_submited_donation( $cart_item_data, $product_id ){
    if( isset($_REQUEST['donation']) ) {
        // Get the WC_Product Object
        $product = wc_get_product( $product_id );

        // Get an set the product active price
        $cart_item_data['active_price'] = (float) $product->get_price();

        // Get the donation amount and set it
        $cart_item_data['donation'] = (float) esc_attr( $_REQUEST['donation'] );
        $cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

add_action( 'woocommerce_before_calculate_totals', 'add_donation_to_item_price', 10, 1);
function add_donation_to_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        // Add the donation to the product price
        if ( isset( $item['donation']) && isset( $item['active_price']) ) {
            $item['data']->set_price( $item['active_price'] + $item['donation'] );
        }

    }
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

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

相关问题 通过 WooCommerce 中的 URL (GET) 设置自定义添加到购物车的价格 - Set a custom add to cart price via the URL (GET) in WooCommerce 通过 WooCommerce 中的 URL 获取并设置自定义产品价格添加到购物车 - GET and set a custom product price on add to cart via URL in WooCommerce WooCommerce:如何通过 URL 将产品价格添加到购物车? - WooCommerce: how to add product price to cart via URL? 在 WooCommerce 中显示自定义计算的购物车商品价格 - Display a custom calculated cart item price in WooCommerce 在 Woocommerce mini-cart / Cart 中设置自定义计算的商品价格 - Set a custom calculated item price in Woocommerce mini-cart / Cart 在 Woocommerce 中添加到购物车操作时,将自定义选择的日期添加到购物车项目 - Add a custom chosen date to cart item on add to cart action in Woocommerce woocommerce 商店页面,如何通过“添加到购物车”按钮添加自定义价格 - woocommerce shop page, how to add custom price via "add to cart" button 基于Woocommerce中的维度自定义字段的自定义购物车商品价格计算 - Custom cart item price calculation based on dimentions custom fields in Woocommerce Woocommerce添加到购物车URL - Woocommerce add to cart URL 购物车商品价格计算,基于Woocommerce中选择的“天”自定义字段 - Cart item price calculation, based on chosen “days” custom field in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM