Woocommerce: custom price based on user input

后端 未结 4 1561
深忆病人
深忆病人 2021-02-04 08:24

I did not want to post here but I could not find the answer I was looking for and I do not have enough reputation to comment on other VERY SIMILAR questions to get my exact answ

4条回答
  •  萌比男神i
    2021-02-04 09:18

    I found a solution which is not elegant but works for my purposes.

    I was trying to use cookies before but I didn't set the cookie path so it was only available to the page it was set on.

    I am now setting a cookie with the donation price:

    setcookie("donation", $_GET['donation'], 0, "/");
    

    Then I am setting the price like so:

    add_action( 'woocommerce_before_calculate_totals', 'woo_add_donation');
    
    function woo_add_donation() {
        global $woocommerce;
    
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if($cart_item['data']->id == 358 && ! empty($_COOKIE['donation'])){
                $cart_item['data']->set_price($_COOKIE['donation']);
            }
        }
    }
    

提交回复
热议问题