2016-07-14 2 views
1

내 워드 프레스/WooCommerce 사이트 내 functions.php 파일에이 코드 조각을 사용하고 있습니다 :상점 페이지에는 재고 없음 문자열이 표시되지만 재고 수량에는 표시되지 않습니다.

function envy_stock_catalog() { 
    global $product; 
    if ($product->is_in_stock()) { 
     echo $product->get_stock_quantity() ; 
    } else { 
     echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 
       add_action('init','remove_loop_button'); 
    } 
} 
add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 

이 코드는 상점 페이지에 공지 '품절'모든 제품처럼 표시되는 위치 표시 이 :

enter image description here


문제는 또한 홍보 옆에있는 제품의 사용 가능한 재고 번호의 원인이다 이 같은 oduct 제목 :

enter image description here

나는 이것을 원하지 않는다.

내 질문 :
나는의 왼쪽에있는 나의 여전히 (제품 재고가없는 경우) 'out of stock' 통지를 표시하는 코드와 상점 페이지가 아닌 재고 제품 가용성 (어떻게 바꾸 추가 기능에 -cart 버튼)?

미리 감사드립니다. 코드에 대한

+0

난 그냥 첫 번째 이미지에서 당신은 '나는 파란색 원했습니다 볼 수 있습니다 첫 번째 이미지를 변경했습니다 ... 활성 자식 테마 또는 테마의 function.php 파일에 간다 코드가 구현하는 재고 없음 '태그가 있습니다. 함수 코드가 없으면이를 표시하지 않습니다. –

답변

2

설명 : 제품 (심지어 using the working tricks of this answer 심지어 상점 페이지) 재고 (다른) 두 번째 부분

  • 이었다로

    1. 첫 번째 부분 (있는 경우)에서는 한 재고 수량을했다 표시했다 '재고 없음' 문자열을 표시하고 장바구니에 추가 버튼을 제거하십시오 (제품 재고 없음). 아래의 코드와

    , 지금 우리는이 :

    • 한 제품이기 때문에 재고 수량을 표시 재고하고 가게에없는 !is_shop()에 추가 (있는 경우) 조건 페이지.
    • 상점 페이지에 대한 추가 (else)는 재고 수량을 표시하지 않고 기능에서 벗어납니다.
    • 그리고 마지막 손질을하지 않은 상태 (이전과 마찬가지로).그래서 여기

    원하는대로 완벽하게 작동 솔루션입니다 :

    add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 
    function envy_stock_catalog() { 
        global $product; 
        if ($product->is_in_stock()) { // If product is in stock 
    
         if (!is_shop()) { // If is NOT Shop page 
    
          // Displays the stock quantity 
          echo '<span class="qty">' . $product->get_stock_quantity() . '<span>'; 
         } else { // If is shop page (and product is in stock) 
          return; // Don't display stock quantity (and removes nothing). 
         } 
        // If product is NOT in stock 
        } else { 
         // Display 'out of stock' string 
         echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 
    
         // Removes "add to cart" button 
         add_action('init','remove_loop_button'); 
        } 
    } 
    

    그냥에만 상점 페이지에서 '품절'하나의 제품 페이지에 재고 수량을 표시 할 경우 및 그냥 돈 경우 '

    add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 
    function envy_stock_catalog() { 
        global $product; 
        // If product is in stock but is not shop page 
        if ($product->is_in_stock() && !is_shop()) { 
    
         // Displays the stock quantity 
         echo '<span class="qty">' . $product->get_stock_quantity() . '<span>'; 
        // If product is NOT in stock and is shop page 
        } elseif (!$product->is_in_stock() && is_shop()) { 
         // Display 'out of stock' string 
         echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 
    
         // Removes "add to cart" button 
         add_action('init','remove_loop_button'); 
        } 
        // other cases goes out the function 
        return; 
    } 
    

    자 :이 코드가 될 것입니다

    add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 
    function envy_stock_catalog() { 
        global $product; 
        if ($product->is_in_stock()) { // If product is in stock 
         return; // Don't display stock quantity (and removes nothing). 
    
        // If product is NOT in stock 
        } else { 
         // Display 'out of stock' string 
         echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 
    
         // Removes "add to cart" button 
         add_action('init','remove_loop_button'); 
        } 
    } 
    

    을 그리고이 경우에 당신이 대답에서 모든 작업 트릭을 사용할 수 있습니다 : t은 어떤 재고 수량을 표시하려면, 코드는 다음과 같이 될 것입니다
    Woocommerce - Remove the available product inventory number from the shop page


    모든 코드 테스트되고 완벽하게 작동합니다.

    코드는

  • 관련 문제