我已经在版本4.9.7和WooCommerce 3.4.4上设置了WordPress的新副本
我创建了具有以下变体的变量产品:
从图片中可以看到,版本ID为6、7、8和9。
但是,如果我尝试执行以下操作:
$v = new WC_Product_Variable( 6 );
然后我得到这个讨厌的错误:
Fatal error: Uncaught Exception: Invalid product. in /var/www/html/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php:149
Stack trace:
#0 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-data-store.php(156): WC_Product_Data_Store_CPT->read(Object(WC_Product_Variable))
#1 /var/www/html/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(133): WC_Data_Store->read(Object(WC_Product_Variable))
#2 /var/www/html/wp-content/themes/twentyseventeen/functions.php(592): WC_Product->__construct(6)
#3 /var/www/html/wp-settings.php(426): include('/var/www/html/w...')
#4 /var/www/html/wp-config.php(89): require_once('/var/www/html/w...')
#5 /var/www/html/wp-load.php(37): require_once('/var/www/html/w...')
#6 /var/www/html/wp-admin/admin.php(31): require_once('/var/www/html/w...')
#7 /var/www/html/wp-admin/post.php(12): require_once('/var/www/html/w...')
#8 {main}
thrown in /var/www/html/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php on line 149
我尝试通过
解决此问题$product_id = 5;
$_product = new WC_Product_Variable( $product_id );
$variations = $_product->get_available_variations();
// $variations = array(0) { }
但是$ variations会返回一个空数组。
当我碰到砖墙时,任何帮助都将受到赞赏。