2016-09-20 3 views
3

woocommerce를 사용하고 있으며 "https://woocommerce.com/products/print-invoices-packing-lists/"에서 사용할 수있는 "Invoice Print & 포장 목록" 을 사용하고 있습니다. 디스플레이의 경우 이 코드를 사용하고있어 woocommerce 카트 & 체크 아웃 페이지에 총 저축은 완벽하게 작동합니다 :WooCommerce - PDF 송장의 주문 루프에서 장바구니 루프 바꿈

function bbloomer_wc_discount_total() { 

    global $woocommerce; 

    $cart_subtotal = $woocommerce->cart->cart_contents; 

    $discount_total = 0; 

    foreach ($woocommerce->cart->cart_contents as $product_data) { 

     if ($product_data['variation_id'] > 0) { 
      $product = wc_get_product($product_data['variation_id']); 
     } else { 
      $product = wc_get_product($product_data['product_id']); 
     } 

     if (!empty($product->sale_price)) { 
     $discount = ($product->regular_price - $product->sale_price) * $product_data['quantity']; 
     $discount_total += $discount; 
     } 

    } 

    if ($discount_total > 0) { 
    echo '<tr class="cart-discount"> 
    <th>'. __('Total Saving :', 'woocommerce') .'</th> 
    <td data-title=" '. __('Total Saving :', 'woocommerce') .' ">' 
    . wc_price($discount_total+$woocommerce->cart->discount_cart) .'</td> 
    </tr>'; 
    } 
} 

add_action('woocommerce_cart_totals_after_order_total', 'bbloomer_wc_discount_total'); 
add_action('woocommerce_review_order_after_order_total', 'bbloomer_wc_discount_total'); 

woocommerce 카트의 내용을 기반으로이 기능을. 나는 인쇄 송장에 의해 생성 된 html로 송장의 총 저축을 표시하는 방법과 같은 플러그인 후크에 (대신 장바구니 내용의 순서에 따라) 플러그인 & 포장 목록 :

add_action('wc_pip_after_header', 'bbloomer_wc_discount_total'); add_action('wc_pip_document_header', 'bbloomer_wc_discount_total');

답변

2

- 업데이트 2-(WooCommerce 버전에 대한 추가 호환성 3 이상)

Now as I know you are using WooCommerce PDF Invoices & Packing Slips plugin.

이 코드는 invoice.php PIP 템플릿에서 직접 사용할 수 있습니다 이것에 의해 <?php global $wpo_wcpdf ?>를 교체 시작 부분에 :

<?php 

    global $wpo_wcpdf, $woocommerce; 

    // Get the order object 
    $_order = $wpo_wcpdf->export->order; 

    foreach ($_order->get_items() as $item) { 
     // Added WC 3+ compatibility 
     $quantity = method_exists($item, 'get_quantity') ? $item->get_quantity() : $item['quantity']; 
     $variation_id = method_exists($item, 'get_variation_id') ? $item->get_variation_id() : $item['variation_id']; 
     $_product = version_compare(WC_VERSION, '3.0', '<') ? wc_get_product($item['product_id']) : $item->get_product(); 

     // Get the product object when it's a product variation (Before version WC 3+) 
     if (version_compare(WC_VERSION, '3.0', '<')) 
      if ($item['variation_id'] > 0) $_product = wc_get_product($item['variation_id']); 

     // Get prices 
     $sale_price = $_product->get_sale_price(); 
     $regular_price = $_product->get_regular_price(); 

     // Only when sale price exits 
     if (! empty($sale_price) && $sale_price > 0) { 
      $discount = ($regular_price - $sale_price) * $item->get_quantity(); 
      $discount_total += $discount; 
     } 

    } 
    if ($discount_total > 0) { 
     $display_discount = '<tr class="cart-discount"> 
      <th>'. __('Total you saved :', 'woocommerce') .'</th> 
      <td data-title=" '. __('Total you saved :', 'woocommerce') .' ">' . wc_price($discount_total + $_order->get_total_discount()) .'</td> 
     </tr>'; 
    } 

?> 

당신은 출력 템플릿에서 원하는 위치 그럼 당신은 그것을 사용할 수 있습니다 그것은, HTML (내부)이 방법 :

<?php echo $display_discount; ?> 

또는 (내부 PHP 코드) :

echo $display_discount; 

이 코드는 테스트되었으며 작동합니다.

+0

귀하의 답변에 감사드립니다. 내 코드의 function.php에이 코드가 추가되었으며 Woocommerce pip 플러그인을 사용하면 html 송장 페이지에 다음과 같은 내용이 표시됩니다. 치명적인 오류 : 부울에서 get_items() 멤버 함수 호출 – Mani

+0

감사합니다. 귀하의 회신으로 "WooCommerce Print Invoices & Packing Lists"를 사용하고 있습니다.이 플러그인을 다운로드 할 수있는 플러그인입니다 : https://www.dropbox.com/s/7mmsuh7o0tjaa45/woocommerce-pip.zip?dl=0 그리고 이것은 Hooks refrence입니다 : https://docs.woocommerce.com/document/woocommerce-print-invoices-packing-lists-developer-reference/. 어떤 코드를 function.php에 추가하여 Html 송장에 Total Saving을 표시 할 수 있습니까? 대단히 감사합니다! – Mani

+0

고마워, 내 functions.php에 코드를 추가 할 수 있습니까? 코드를 어떻게 사용할 수 있습니까? 고마워요, – Mani

관련 문제