我想找到一个变量股票文本,我用下面的代码,但我正在寻找任何适当的方法。我从woocommerece插件的templates/single-product/add-to-cart/variable.php
文件中获取代码。请任何帮助将不胜感激。
$terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
$product_variations_cus = $product->get_available_variations(); // my custom code start from here
foreach($product_variations_cus as $var_cus){
foreach($var_cus['attributes'] as $stock) {
if($term->slug==$stock)
$availability_html = $var_cus['availability_html']; // end here
}
}
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) .' '.$availability_html.'</option>';
}
}
答案 0 :(得分:2)
不要编辑模板。 将其粘贴到主题的functions.php中。
add_filter( 'woocommerce_variation_option_name', 'woocommerce_variation_option_name', 10 );
function woocommerce_variation_option_name( $name ) {
global $product;
foreach( $product->get_children() as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( in_array( strtolower( $name ), $variation->get_attributes() ) ) {
$availability = $variation->get_availability();
return $name . ' ' . $availability['availability'];
}
}
return $name;
}
答案 1 :(得分:1)
<?php
global $product;
$product_variations = $product->get_available_variations();
foreach ($product_variations as $variation) {
$var_data = $variation['attributes'];
$var_data['in_stock'] = $variation['is_in_stock'];
}
?>
检查出来