Woocommerce $ cart_item ['data'] & # 8594; set_price () doesn't work

I am trying to dynamically change the price of a product. I installed the plugin WC_fields_Factory

and I am adding some fields to insert the price.

This is my code for the change price:

add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
        add_filter( 'woocommerce_add_cart_item', array( $this, 'simone_add_cart_item' ), 10, 1 );
        add_action( 'woocommerce_before_calculate_totals', array( $this, 'simone_update_price'), 99 );

      

[....]

private function _getPrice($product_id, $price) {
        $all_fields = apply_filters( 'wcff/load/all_fields', $product_id, 'wccpf' );
        $prezzoCalcolato = 0;
        foreach ( $all_fields as $title => $fields )
        {
            foreach ( $fields as $field )
            {
                if( isset($field['field_valore']) && $field['field_grouprice'] == 'price-var' )
                {
                    $quantita = isset($_REQUEST[ $field["name"] ]) ? intval($_REQUEST[ $field["name"] ]) : 0;
                    $prezzoCalcolato =  intval($quantita) * intval($field['field_valore']);
                } elseif( isset($field['field_valore']) && $field['field_grouprice'] == 'price-fix' ) {
                    $prezzoCalcolato =  intval($field['field_valore']) ;
                }
            }
        }
        $prezzoCalcolato += $price;

        return $prezzoCalcolato; 
    }

    public function simone_add_cart_item( $cart_item, $cart_item_key ) {
        $price = $this->_getPrice($cart_item['product_id'], $cart_item['data']->get_price());
        $cart_item['data']->set_price($price);

        return $cart_item;
    }

    public function get_cart_item_from_session( $cart_item, $values ) {
        $price = $this->_getPrice($cart_item['product_id'], $cart_item['data']->get_price());
        $cart_item['data']->set_price($price);

        return $cart_item;
    }

    public function simone_update_price($cart) {

        foreach($cart->get_cart() as $cart_key_item => $cart_item) {
            $price = $this->_getPrice($cart_item['product_id'], $cart_item['data']->get_price());
            $cart_item['data']->set_price($price);
        }
    }

      

and it doesn't work ... Now when I try to add value to the normal product price, here's what happens:

$cart_item['data']->set_price(57);

works fine otherwise i set price as:

$prezzo = 57;
$cart_item['data']->set_price($prezzo);

      

it doesn't work -.- I tried setting floatval or intval like this:

$cart_item['data']->set_price(intval($prezzo));

      

Nothing works: (

Online solutions:

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

 function add_custom_price( $cart_object ) {
     $custom_price = 10; // This will be your custome price  
     foreach ( $cart_object->cart_contents as $key => $value ) {
         $value['data']->price = $custom_price;


  } }

      

or

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

 function add_custom_price( $cart_object ) {
     $custom_price = 10; // This will be your custome price  
     foreach ( $cart_object->cart_contents as $key => $value ) {
         $value['data']->set_price($custom_price);
   } }

      

They do not work!!!!

+3


source to share





All Articles