动态更改产品价格 woocommerce
Change product price dynamically woocommerce
我正在尝试动态地向 woocommerce 中的产品价格添加自定义费用。我过去做过很多次,但现在不起作用。这是我的代码。
function calculate_eyehole_fee( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
$eyeHoleFee = 30.00;
$ribbonFee = 20.00;
// Get exchange rate details and defined fees
$strCurrencyCode = get_woocommerce_currency();
$arrExchangeRates = get_option('wc_aelia_currency_switcher');
$fltExchangeRate = $arrExchangeRates['exchange_rates'][$strCurrencyCode]['rate'];
$eveHoleCurrFee = $eyeHoleFee * $fltExchangeRate;
$ribbonCurrFee = $ribbonFee * $fltExchangeRate;
foreach ( $cart_object->cart_contents as $key => $value ) {
$additionCost = 0.0;
if( isset( $value["eyeHoleReq"] ) && $value["eyeHoleReq"] == 'yes' ) {
$fltEyeFee = $eveHoleCurrFee;
$additionCost = $fltEyeFee;
}
if( isset( $value["eyeRibbon"] ) && $value["eyeRibbon"] == 'yes' ) {
$fltRibbonFee = $ribbonCurrFee;
$additionCost += $fltRibbonFee;
}
$cart_object->cart_contents[$key]['data']->price += $additionCost;
}
$isProcessed = true;
}
print('<pre>');print_r($cart_object);print('</pre>');
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_eyehole_fee', 99 );
我的价格已在购物车对象中更新,但未反映在何处。因此总数也计算错误。
woocommerce 3.0 更新后我们无法直接设置价格。相反,我们必须使用 set_price() 方法。所以以下应该有效:
function calculate_eyehole_fee( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
$eyeHoleFee = 30.00;
$ribbonFee = 20.00;
// Get exchange rate details and defined fees
$strCurrencyCode = get_woocommerce_currency();
$arrExchangeRates = get_option('wc_aelia_currency_switcher');
$fltExchangeRate = $arrExchangeRates['exchange_rates'][$strCurrencyCode]['rate'];
$eveHoleCurrFee = $eyeHoleFee * $fltExchangeRate;
$ribbonCurrFee = $ribbonFee * $fltExchangeRate;
foreach ( $cart_object->get_cart() as $key => $value ) {
$additionCost = 0.0;
if( isset( $value["eyeHoleReq"] ) && $value["eyeHoleReq"] == 'yes' ) {
$fltEyeFee = $eveHoleCurrFee;
$additionCost = $fltEyeFee;
}
if( isset( $value["eyeRibbon"] ) && $value["eyeRibbon"] == 'yes' ) {
$fltRibbonFee = $ribbonCurrFee;
$additionCost += $fltRibbonFee;
}
$defPrice = $value['data']->get_price('edit');
$value['data']->set_price((float) $defPrice + $additionCost);
}
$isProcessed = true;
}
print('<pre>');print_r($cart_object);print('</pre>');
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_eyehole_fee', 99 );
我正在尝试动态地向 woocommerce 中的产品价格添加自定义费用。我过去做过很多次,但现在不起作用。这是我的代码。
function calculate_eyehole_fee( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
$eyeHoleFee = 30.00;
$ribbonFee = 20.00;
// Get exchange rate details and defined fees
$strCurrencyCode = get_woocommerce_currency();
$arrExchangeRates = get_option('wc_aelia_currency_switcher');
$fltExchangeRate = $arrExchangeRates['exchange_rates'][$strCurrencyCode]['rate'];
$eveHoleCurrFee = $eyeHoleFee * $fltExchangeRate;
$ribbonCurrFee = $ribbonFee * $fltExchangeRate;
foreach ( $cart_object->cart_contents as $key => $value ) {
$additionCost = 0.0;
if( isset( $value["eyeHoleReq"] ) && $value["eyeHoleReq"] == 'yes' ) {
$fltEyeFee = $eveHoleCurrFee;
$additionCost = $fltEyeFee;
}
if( isset( $value["eyeRibbon"] ) && $value["eyeRibbon"] == 'yes' ) {
$fltRibbonFee = $ribbonCurrFee;
$additionCost += $fltRibbonFee;
}
$cart_object->cart_contents[$key]['data']->price += $additionCost;
}
$isProcessed = true;
}
print('<pre>');print_r($cart_object);print('</pre>');
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_eyehole_fee', 99 );
我的价格已在购物车对象中更新,但未反映在何处。因此总数也计算错误。
woocommerce 3.0 更新后我们无法直接设置价格。相反,我们必须使用 set_price() 方法。所以以下应该有效:
function calculate_eyehole_fee( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
$eyeHoleFee = 30.00;
$ribbonFee = 20.00;
// Get exchange rate details and defined fees
$strCurrencyCode = get_woocommerce_currency();
$arrExchangeRates = get_option('wc_aelia_currency_switcher');
$fltExchangeRate = $arrExchangeRates['exchange_rates'][$strCurrencyCode]['rate'];
$eveHoleCurrFee = $eyeHoleFee * $fltExchangeRate;
$ribbonCurrFee = $ribbonFee * $fltExchangeRate;
foreach ( $cart_object->get_cart() as $key => $value ) {
$additionCost = 0.0;
if( isset( $value["eyeHoleReq"] ) && $value["eyeHoleReq"] == 'yes' ) {
$fltEyeFee = $eveHoleCurrFee;
$additionCost = $fltEyeFee;
}
if( isset( $value["eyeRibbon"] ) && $value["eyeRibbon"] == 'yes' ) {
$fltRibbonFee = $ribbonCurrFee;
$additionCost += $fltRibbonFee;
}
$defPrice = $value['data']->get_price('edit');
$value['data']->set_price((float) $defPrice + $additionCost);
}
$isProcessed = true;
}
print('<pre>');print_r($cart_object);print('</pre>');
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_eyehole_fee', 99 );