我试图插入插件我正在编写产品的变体ID。这是我写的:
class mass {
public function __construct()
{
add_action('woocommerce_product_after_variable_attributes',array($this,'thfo_mass'));
}
public function thfo_mass()
{
$id = WC_Product_Variation::get_variation_id();
//$lenght = get_post_meta($id,'_length');
//$dimensions = wc_get_dimension(24750, 'cm');
var_dump($id);
}
我只收到错误:
不推荐使用:非静态方法WC_Product_Variation :: get_variation_id()不应该静态调用,假设$ this来自path / to / plugins / thfo-raw-material-for-woocommerce / class / mass.php中的不兼容上下文第19行
注意:未定义属性:mass :: $ variation_id in path / to / wp-content / plugins / woocommerce / includes / class-wc-product-variation.php on 257 int(0)
答案 0 :(得分:6)
试试这个。
<?php
$product_obj = new WC_Product_Factory();
$product = $product_obj->get_product($product);
if ($product->product_type == 'variable'):
$children = $product->get_children( $args = '', $output = OBJECT );
foreach ($children as $key=>$value) {
$product_variatons = new WC_Product_Variation($value);
if ( $product_variatons->exists() && $product_variatons->variation_is_visible() ) {
$variations[$value] = $product_variatons->get_variation_attributes();
}
}
endif;
答案 1 :(得分:1)
get_variation_id
根本不是静态方法。
您需要创建WC_Product_Variation
的实例/对象,然后才能使用它。
喜欢---
$variable_product = new WC_Product_Variation($value);
$variable_product->get_variation_id();
答案 2 :(得分:0)
到目前为止,我已经设法解决了这个问题:
首先,要获得颜色:
$colour = isset( $_REQUEST[ 'attribute_' . sanitize_title( 'pa_colour' ) ] ) ? wc_clean( stripslashes( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( 'pa_colour' ) ] ) ) ) : $product->get_variation_default_attribute( 'pa_colour' );
然后在我的foreach循环中生成复选框:
$variation_id = searchForVariants($colour, $size, $available_variations);
$product_shipping_classes = get_the_terms( $variation_id, 'product_shipping_class' );
$product_shipping_class_name = ( $product_shipping_classes && ! is_wp_error( $product_shipping_classes ) ) ? current( $product_shipping_classes )->name : '';
然后,您可以使用current( $product_shipping_classes )->term_id
或current( $product_shipping_classes )->slug
或current( $product_shipping_classes )->name
。
searchForVariants函数位于我的functions.php
文件中:
function searchForVariants($colour, $size, $array) {
foreach ($array as $key => $sub_array) {
if ($sub_array['attributes']['attribute_pa_colour'] === $colour && $sub_array['attributes']['attribute_pa_size'] === $size) {
return $sub_array['variation_id'];
} else {
continue;
}
}
}
$available_variations
数组已经在variable.php
的店面主题中,但我正在使用子主题,因此复制了模板文件以进行这些更改。
答案 3 :(得分:0)
请在此处检查此结果:How to get Woocommerce Variation ID?
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => get_the_ID() // get parent post-ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
// get variation ID
$variation_ID = $variation->ID;
// get variations meta
$product_variation = new WC_Product_Variation( $variation_ID );
// get variation featured image
$variation_image = $product_variation->get_image();
// get variation price
$variation_price = $product_variation->get_price_html();
get_post_meta( $variation_ID , '_text_field_date_expire', true );
}