WooCommerce 更新版本 3.0.x 破坏站点 -

WooCommerce update version 3.0.x breaks site -

我从版本 2.6.x 更新到 WooCommerce 版本 3.0.x,我的网站奇怪地停止正确加载。

我收到消息:

"Object of class WC_Product_Variable could not be converted to string"

错误的来源是 woocommerce/includes/wc-product-functions.php 文件中的一行

$value = ucwords( str_replace( '-', ' ', $value ) );

通过注释掉包含该行的代码块,我能够让网站再次正常工作,从“if ( taxonomy_exists( $name ) ) {”开始,但我没有这样的拼凑解决方案真的让人感觉很舒服。

有人可以解释这个错误的原因吗?

这是我的代码:

foreach ( $variation_attributes as $name => $value ) {
        if ( ! $value ) {
            continue;
        }

        // If this is a term slug, get the term's nice name
        /*if ( taxonomy_exists( $name ) ) {
            $term = get_term_by( 'slug', $value, $name );
            if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) {
                $value = $term->name;
            }
        } else {
            $value = ucwords( str_replace( '-', ' ', $value ) );
        }

        if ( $include_names ) {
            if ( $flat ) {
                $variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
            } else {
                $variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
            }
        } else {
            if ( $flat ) {
                $variation_list[] = rawurldecode( $value );
            } else {
                $variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
            }
        }*/
    }

谢谢

这意味着$value不是字符串而是对象数组 的值的值。

因此使用 str_replace() PHP 函数无法使用它,因为它适用于字符串。

你最好尝试在其上使用 foreach 循环或 implode() PHP 函数将其转换为字符串,但是...

The real problem location:

  • Your active child theme (or theme) and function.php file where most of the time we put some custom code.
  • Third party plugins involved in WooCommerce

WooCommerce version 3.0+ is a major release that is much more strict. A new syntax around getters and setters is now necessary to access to the objects in most WC Classes.


供参考:

Here is the Legacy and deprecated functions that are here to keep the WC_Abstract_Product clean (This class will be removed in future versions):