WordPress
- 1 ответ
- 0 вопросов
0
Вклад в тег
add_filter('woocommerce_variation_option_name', 'display_price_and_stock_status_in_variation_option_name');
function display_price_and_stock_status_in_variation_option_name($term)
{
global $product;
if (empty($term)) {
return $term;
}
if (empty($product->get_id())) {
return $term;
}
$variation_id = $product->get_children();
foreach ($variation_id as $id) {
$_product = new WC_Product_Variation($id);
$variation_data = $_product->get_variation_attributes();
$stock_status = $_product->get_stock_status();
$stock_status = str_replace(array('instock', 'outofstock', 'onbackorder'), array('In Stock', 'Out of Stock', 'Pre-order'), $stock_status);
foreach ($variation_data as $key => $data) {
if ($data == $term) {
$remaining_stock = $_product->get_stock_quantity();
if ($remaining_stock > 2) {
$stock_status_text = 'In stock';
} elseif ($remaining_stock == 2) {
$stock_status_text = 'Low in Stock';
} elseif ($remaining_stock == 1) {
$stock_status_text = 'Only 1 left';
} elseif ($remaining_stock == 0 && $_product->is_on_backorder()) {
$stock_status_text = 'Pre-order now';
} else {
$stock_status_text = 'Sold Out';
}
$html = $term . ' (' . $stock_status_text . ')';
return $html;
}
}
}
return $term;
}