Woo 3.0 deprecation warnings from br_wc_get_product_post
-
There is a bug since WC 3.0 inside the plugin’s “bc_wc_get_product_post” function (in includes/functions.php).
The function is supposed to detect a Woo 3 friendly method for accessing the post, and only fall through to the Woo 2 style direct property access if there is no $product->get_post() method found by a method_exists() check:
if( method_exists($product, 'get_post') ) { return $product->get_post(); } else { return @ $product->post; }
However, there is no such WC_Product::get_post() or WC_Product_Variable::get_post() method for product or variable product classes in Woo 3. So, the logic branches incorrectly and uses the WC 2 version, and that in turn fires deprecation warnings to error_log:
AH01071: Got error ‘PHP message: post was called incorrectly. Product properties should not be accessed directly. Backtrace: require_once(‘wp-load.php’), require_once(‘wp-config.php’), require_once(‘wp-settings.php’), do_action(‘wp_loaded’), WP_Hook->do_action, WP_Hook->apply_filters, WC_Cart_Session->get_cart_from_session, WC_Cart->calculate_totals, do_action(‘woocommerce_after_calculate_totals’), WP_Hook->do_action, WP_Hook->apply_filters, BeRocket_MM_Quantity::cart_calculate_total, br_wc_get_product_post, WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong. This message was added in version 3.0.\n’
The solution for WC3 is to refactor the logic so that it’s able to correctly find the post. I used the following replacement for br_wc_get_product_post() – see attached patch. I have not tested this logic with WC 2.x.
function br_wc_get_product_post($product) { $product_id = $product->get_id(); $post = get_post($product_id); return $post; }
Here is a patch of the change, against latest SVN trunk:
Index: includes/functions.php =================================================================== --- includes/functions.php (revision 1799907) +++ includes/functions.php (working copy) @@ -41,11 +41,9 @@ } if( ! function_exists( 'br_wc_get_product_post' ) ){ function br_wc_get_product_post($product) { - if( method_exists($product, 'get_post') ) { - return $product->get_post(); - } else { - return @ $product->post; - } + $product_id = $product->get_id(); + $post = get_post($product_id); + return $post; } } if( ! function_exists( 'br_get_woocommerce_version' ) ){
- The topic ‘Woo 3.0 deprecation warnings from br_wc_get_product_post’ is closed to new replies.