Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. function x_amount_of_items_left()
  2. {
  3.     global $product;
  4.  
  5.     if ($product->is_type( 'variable' )){
  6.  
  7.         $available_variations = $product->get_available_variations();
  8.         $variations_count = count($available_variations);
  9.  
  10.         $total_stock = 0;
  11.  
  12.  
  13.         write_log('Product::: ' . $product->get_name());
  14.  
  15.         foreach( $available_variations as $key => $values ) {
  16.             $variation_obj = wc_get_product( $values['variation_id'] );
  17.             $stock_qty = $variation_obj->get_stock_quantity();
  18.             $variaton_name = $variation_obj->get_formatted_name();
  19.  
  20.  
  21.  
  22.             write_log('Stock qty for::: ' . $variaton_name . ' ' . $stock_qty);
  23.             $total_stock += $stock_qty;
  24.         }
  25.  
  26.         write_log('Total stock::: ' . $total_stock);
  27.  
  28.  
  29.         write_log('======================================');
  30.  
  31.         if($total_stock <= 5 && $total_stock > 1){
  32.             echo '<p class="latest-items stock in-stock">' . $total_stock . ' ' . esc_html__('items left', 'nm-framework') . '</p>';
  33.         } else if ($total_stock == 1) {
  34.             echo '<p class="latest-items stock in-stock">' . esc_html__('Only', 'nm-framework') . ' ' . $total_stock . ' ' . esc_html__('item left', 'nm-framework') . '</p>';
  35.         }
  36.     }
  37. }
  38. add_action('woocommerce_before_shop_loop_item_title', 'x_amount_of_items_left');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement