我可以改变购物车的价格,改变小计......但是,我想改变产品本身的实际价格,而不仅仅是在购物车中。
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):
function getColorOption() {
selectElement =
document.querySelector('#selectedColor');
outputColor = selectElement.value;
document.querySelector('.outputColor').textContent
= outputColor;
// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
sessionStorage.setItem("hiddenColor", document.getElementById('selectedColor').value);
// Retrieve
document.getElementById("result2").innerHTML = sessionStorage.getItem("hiddenColor");
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
};
答案 0 :(得分:2)
您可以使用 woocommerce_before_calculate_totals
动作挂钩。循环遍历 cart_object
并检查您的产品 ID,您可以使用 set_price
来覆盖价格。检查下面的代码。代码将进入您的活动主题 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 );
}
}
}