WooCommerce 3.0中的WooCommerce_Being_Calculate_Totals

时间:2017-07-04 作者:KKBSE2016

我最近从woocommerce 2.7跳到了3.1,我对woocommerce_before_calculate_totals 钩住下面的功能。

function calculate_embossing_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $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 );
有人知道为什么这会抛出一个错误以及我如何解决它吗?

我得到的错误是:确定:

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

1 个回复
最合适的回答,由SO网友:CodeMascot 整理而成

问题是你在打电话price 直接在$value[\'data\']->price. 成功吧$value[\'data\']->get_price() 我想你的问题会解决的。所以整个代码块将-

function calculate_embossing_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = 5;
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["embossing_fee"] ) ) {
                // Turn $value[\'data\']->price in to $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 );
希望这有帮助。

结束

相关推荐

Updates for a private plugin?

如果我写一个私有插件,有没有办法使用WordPress自动更新机制来更新它 我想封装这个功能,但它是我自己的5个博客特有的,所以它不是公共插件资源的好候选。但我喜欢这种简单的更新机制 有没有办法做到这一点