【问题标题】:WooCommerce custom cart item price on add-to cart via the URL通过 URL 加入购物车的 WooCommerce 自定义购物车商品价格
【发布时间】:2019-12-21 15:46:42
【问题描述】:

我有一个默认价格值为 100 的简单产品。

当您选择产品时,它必须直接结帐,我的处理方式如下:

/checkout/?add-to-cart=78

但是,必须可以覆盖该产品的价格,因为它是志愿者捐赠金额。

我正在尝试这个:

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

在我的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 );

    }   
}

当页面第一次加载时,它已将价格设置为 200。但是,JS 在重新加载到此 url 后进行 ajax 调用:

/?wc-ajax=update_order_review

将价格重置为 100。

如何阻止它重置价格?

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    您需要在添加到购物车事件中将捐赠设置为自定义购物车项目数据,以避免在 ajax 刷新结帐时丢失。

    在下面的代码中,我们捕获了我们设置为自定义购物车商品数据的捐赠值,然后将其添加到相应的购物车商品价格中。

    这通过 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'] );
            }
    
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 2021-06-28
      • 2015-11-28
      • 2019-10-02
      • 2015-12-10
      • 2015-11-15
      相关资源
      最近更新 更多