Php Woocommerce$cart_项目[';数据->;设置价格()不';行不通

Php Woocommerce$cart_项目[';数据->;设置价格()不';行不通,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我试图动态地改变产品的价格。我已经安装了pluginWC\u fields\u Factory,并添加了一些字段以插入价格 这是我更改价格的代码: 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, 'sim

我试图动态地改变产品的价格。我已经安装了plugin
WC\u fields\u Factory
,并添加了一些字段以插入价格

这是我更改价格的代码:

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);
        }
    }
但它不起作用。。。 现在,当我尝试将成本添加到产品的常规价格时,会发生以下情况:

$cart\u item['data']->定价(57)工作正常
否则,我会设定如下价格:

$prezzo = 57;
$cart_item['data']->set_price($prezzo);
它不起作用--我试着像这样设置floatval或intval:

$cart_item['data']->set_price(intval($prezzo));
没有什么不起作用:(

在线解决方案包括:

 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;


  } }

他们不工作

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);
   } }