如何解决从可变产品的 WooCommerce 变体中获取权重值
我正在开发一个自动调整产品价格的插件。我所有的产品都是可变产品,这让我无法解决这个问题。
if( $product->is_type( 'variable' ) ){
foreach ( $product->get_available_variations() as $key => $variation ) {
foreach ($variation['attributes'] as $attribute ) {
if ( ! $attribute->get_weight() ) {
我进行了检查以确保产品是可变的。唯一的产品属性是“尺寸”。在这里,我对每个产品有 4-8 种不同的尺寸变化。每一个都有一个权重值,这似乎是 Woocommerce 默认实现的。我无法从每个变体中获得重量。好奇我是否从错误的地方调用 get_weight() ,或者是否有不同的方法。 get_weight() 当然不起作用,所以我想知道从变体中获取属性是否完全错误?
解决方法
使用 gridColumnApi.autoSizeColumns(colId)
WC_Variable_Product
(或 get_visible_children()
) 方法,尝试:
get_children()
或者您可以使用 global $product; // If needed | or $product = wc_get_product( $product_id );
if( $product->is_type( 'variable' ) ){
foreach ( $product->get_visible_children() as $variation_id ) {
$variation = wc_get_product( $variation_id ); // Get the product variation object
$weight = $variation->get_weight(); // Get weight from variation
if ( ! $weight ) {
echo '<div>' __("No weight") . '</div>';
} else {
echo '<div><strong>' __("Weight") . ':</strong> ' . wc_format_weight( $weight ) . '</div>';
}
}
}
WC_Variable_Product
如下:
get_available_variations()
两个代码片段都有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。