5

I try to change product's price dynamically. I have installed plugin WC_fields_Factory, and I add some fields to insert price.

That is my code for 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 cost to regular price of product, this is what happens:

$cart_item['data']->set_price(57); work fine else i set price like:

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

it doesn't work -.- i tried to set floatval or intval like this:

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

Nothing not work :(

Online solutions are:

 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 doesn't work!!!!

Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
Doretto
  • 51
  • 6

0 Answers0