Risolvere l'errore woocommerce_before_calculate_totals in WooCommerce 3.0
4 lug 2017, 12:46:44
Visualizzazioni: 17.6K
Voti: 2
Ho recentemente effettuato l'aggiornamento da WooCommerce 2.7 a 3.1 e sto riscontrando problemi con l'hook woocommerce_before_calculate_totals
nella funzione qui sotto.
function calculate_embossing_fee( $cart_object ) {
if( !WC()->session->__isset( "reload_checkout" )) {
/* Prezzo per incisione personalizzata */
$additionalPrice = 5;
foreach ( $cart_object->cart_contents as $key => $value ) {
if( isset( $value["embossing_fee"] ) ) {
$orgPrice = floatval( $value['data']->price);
$discPrice = $orgPrice + $additionalPrice;
$value['data']->set_price($discPrice);
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_embossing_fee', 99 );
Qualcuno sa perché questo codice genera un errore e come posso risolverlo?
L'errore che ottengo è:
Notice: price was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/bdop/page.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, call_user_func_array, do_shortcode, preg_replace_callback, do_shortcode_tag, call_user_func, WC_Shortcodes::cart, WC_Shortcodes::shortcode_wrapper, call_user_func, WC_Shortcode_Cart::output, WC_Cart->calculate_totals, do_action('woocommerce_before_calculate_totals'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, calculate_embossing_fee, WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in /Applications/MAMP/htdocs/bdop/wp-includes/functions.php on line 4139

KKBSE2016
113
Commenti
Tutte le risposte alla domanda
2
1
Bene, il problema è che stai chiamando direttamente price
con $value['data']->price
. Modificalo in $value['data']->get_price()
e penso che il tuo problema sarà risolto. Quindi l'intero blocco di codice sarà-
function calculate_embossing_fee( $cart_object ) {
if( !WC()->session->__isset( "reload_checkout" )) {
/* Prezzo per incisione personalizzata */
$additionalPrice = 5;
foreach ( $cart_object->cart_contents as $key => $value ) {
if( isset( $value["embossing_fee"] ) ) {
// Cambia $value['data']->price in $value['data']->get_price()
$orgPrice = floatval( $value['data']->get_price() );
$discPrice = $orgPrice + $additionalPrice;
$value['data']->set_price($discPrice);
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_embossing_fee', 99 );
Spero che questo ti aiuti.

CodeMascot
4.54K
4 lug 2017 16:44:50
0
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Ottieni il metodo di pagamento
$chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
$chosen_default_deposit = WC()->session->get('deposit_enabled');
// Confronta
if(empty($chosen_default_deposit))
{
if ( $chosen_payment_method == 'bacs' ) {
// Cicla attraverso gli elementi del carrello
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Ottieni il prezzo
//echo "<pre>";
//print_r($cart_item);
//$price = 7000;
$custom_bank_transfer_price_text_field = get_post_meta($cart_item['product_id'], '_custom_bank_transfer_price_text_field', true);
if(!empty($custom_bank_transfer_price_text_field))
{
$cart_item['data']->set_price( $custom_bank_transfer_price_text_field );
}
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

Maulik patel
109
23 dic 2024 12:48:45
Domande correlate
2
risposte