Php Woocommerce:最新更新2.3.5正在使用snippet(用户访问时添加项目)创建问题

Php Woocommerce:最新更新2.3.5正在使用snippet(用户访问时添加项目)创建问题,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,今天我更新到了Woocommerce的最新版本,我的网站突然停止了工作。经过一些检查,我发现最新的更新正在崩溃,我目前正在使用一个片段在用户访问时自动添加项目,我在这里找到了该片段: 是否有任何本机代码段可以替代此代码段 add_action( 'template_redirect', 'add_product_to_cart' ); function add_product_to_cart() { if ( ! is_admin() ) { $product_id =

今天我更新到了Woocommerce的最新版本,我的网站突然停止了工作。经过一些检查,我发现最新的更新正在崩溃,我目前正在使用一个片段在用户访问时自动添加项目,我在这里找到了该片段:

是否有任何本机代码段可以替代此代码段

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 64;
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}