Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function x_amount_of_items_left()
- {
- global $product;
- if ($product->is_type( 'variable' )){
- $available_variations = $product->get_available_variations();
- $variations_count = count($available_variations);
- $total_stock = 0;
- write_log('Product::: ' . $product->get_name());
- foreach( $available_variations as $key => $values ) {
- $variation_obj = wc_get_product( $values['variation_id'] );
- $stock_qty = $variation_obj->get_stock_quantity();
- $variaton_name = $variation_obj->get_formatted_name();
- write_log('Stock qty for::: ' . $variaton_name . ' ' . $stock_qty);
- $total_stock += $stock_qty;
- }
- write_log('Total stock::: ' . $total_stock);
- write_log('======================================');
- if($total_stock <= 5 && $total_stock > 1){
- echo '<p class="latest-items stock in-stock">' . $total_stock . ' ' . esc_html__('items left', 'nm-framework') . '</p>';
- } else if ($total_stock == 1) {
- echo '<p class="latest-items stock in-stock">' . esc_html__('Only', 'nm-framework') . ' ' . $total_stock . ' ' . esc_html__('item left', 'nm-framework') . '</p>';
- }
- }
- }
- add_action('woocommerce_before_shop_loop_item_title', 'x_amount_of_items_left');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement