woocommerce change price while add to cart

后端 未结 4 2141
时光取名叫无心
时光取名叫无心 2021-02-10 05:13

i have a one product 1€ and use e GET parameter to change the price at runtime:

http://url/warenkorb/?add-to-cart=1539&price=18.45

This change not the price

4条回答
  •  一整个雨季
    2021-02-10 06:02

    Its not working for me, i modified to modify price passed by a parameter

    spainbox.com/carro/?add-to-cart=28792&shippingprice=141
    

    Product have a price of 1 euro, and i need to add to cart this product because its a service that will have the price of the shipping cost

    adjust_price( $extra_cost );
            // here the real adjustment is going on...
    
        endif;
    
        return $cart_item;
    
    }
    
    add_filter( 'woocommerce_add_cart_item_data', 'c_other_options_add_cart_item_data', 10, 2 );
    function c_other_options_add_cart_item_data($cart_item_meta, $product_id){
        global $woocommerce;
    
        $product = new WC_Product( $product_id);
        $price = $product->price;
    
        if(empty($cart_item_meta['_other_options']))
            $cart_item_meta['_other_options'] = array();
    
        $cart_item_meta['_other_options']['product-price'] = esc_attr($_REQUEST['shippingprice']);
    
    // as woocommerce allows to adjust the price (i don't know if there is any way to reset it, but this procedure works), we need to return the adjustable price
    
        return $cart_item_meta;
    }
    ?>
    

提交回复
热议问题