使用php以编程方式更新*实际*woocommerce产品价格?

使用php以编程方式更新*实际*woocommerce产品价格?,php,woocommerce,Php,Woocommerce,我可以让购物车的价格改变,小计改变。。。然而,我想改变产品本身的实际价格,而不仅仅是在购物车中 function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) { if ( 5825 === $cart_item['product_id'] ) { $price = '$ 1500.00'; } return $price; } f

我可以让购物车的价格改变,小计改变。。。然而,我想改变产品本身的实际价格,而不仅仅是在购物车中

    function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
    
    if ( 5825 === $cart_item['product_id'] ) {
        $price = '$ 1500.00';
        
    }
    return $price;
}

function sv_change_product_price_actual( $price, $product ) {
    $product_id->get_id();
    return $product_id;
    if ( $product_id === 5825 ) {
        $price = "$ 6000.00";
    }
}

function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity, $instance ) { 
    $product_subtotal = $product->get_price() * $quantity;
    return $product_subtotal; 
  }; 

function sv_change_product_price( $product, $price ) {
    $product_id->get_id();
    return $product_id;
    if ($product_id == 5825) {
        $product_price = ' $ 1500.00';
    }
}
add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );
add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 4 );
add_filter( 'woocommerce_product_price', 'sv_change_product_price_actual', 10, 3 );
这是我已经知道的,当然为了测试的目的,价格是硬编码的

有什么想法或想法吗

下面的会话代码(JavaScript):


您可以在计算总计之前使用
woocommerce\u
action hook。循环查看
cart\u对象
并检查您的产品id,您可以使用
set\u price
覆盖价格。检查下面的代码。代码将放入活动的theme function.php文件中

add_action( 'woocommerce_before_calculate_totals', 'update_price_based_on_customer_selection' );

function update_price_based_on_customer_selection( $cart_object ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // change prices
    foreach ( $cart_object->get_cart() as $hash => $value ) {
        // check if the product is 5825 
        if(  $value['product_id'] == 5825 ) {
            $value['data']->set_price( 1500.00 );
        }

    }

}

你的意思是想在数据库中也进行更改?@Bhautik是的!我希望产品的实际价格根据特定的价格进行更新requirements@Bhautik校正很抱歉,我只是想根据客户在产品页面上的选择向他们收取一定的费用。如果我在数据库中更改它,它将为每个人更改它。那不是我想要的。谢谢!这个修好了@kylenielebeck欢迎…什么是用户选择?你能质疑会话值是什么样子的吗?让我们来看看。
add_action( 'woocommerce_before_calculate_totals', 'update_price_based_on_customer_selection' );

function update_price_based_on_customer_selection( $cart_object ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // change prices
    foreach ( $cart_object->get_cart() as $hash => $value ) {
        // check if the product is 5825 
        if(  $value['product_id'] == 5825 ) {
            $value['data']->set_price( 1500.00 );
        }

    }

}