2017-09-21 4 views
1

をいくつかのバリエーションの値を表示し、Iは可変製品の各variationためheightwidthregular pricesale price有する単純なHTMLテーブルを出力する関数を作成したいです。WooCommerce変数製品:HTMLテーブルにWoocommerceで

たとえば、のは、変数の製品は、異なる寸法の3つのバリエーションが付属していますし、私は、このHTML私の関数の出力を行う必要があるとしましょう:

<table> 
<thead> 
    <tr> 
     <th>Height</th> 
     <th>Width</th> 
     <th>Regular price</th> 
     <th>Sale price</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td>180cm</td> 
     <td>100cm</td> 
     <td>224€</td> 
     <td>176€</td> 
    </tr> 
    <tr> 
     <td>210cm</td> 
     <td>125cm</td> 
     <td>248€</td> 
     <td>200€</td> 
    </tr> 
    <tr> 
     <td>240cm</td> 
     <td>145cm</td> 
     <td>288€</td> 
     <td>226€</td> 
    </tr> 
</tbody> 

私が構築する方法を確認していませんこのために私はそれをwoocommerce_after_single_productアクション内に追加することができますcontent-single-product.phpの中に追加することができます。

これはどのように行うことができますか?

ご迷惑をおかけして申し訳ありません。ここで

答えて

1

woocommerce_after_single_productアクションフックでそれをフック達成するための正しい方法である:

add_action('woocommerce_after_single_product', 'custom_table_after_single_product'); 
function custom_table_after_single_product(){ 
    global $product; 

    $available_variations = $product->get_available_variations(); 

    if(count($available_variations) > 0){ 

     $output = '<table> 
      <thead> 
       <tr> 
        <th>'. __('Height', 'woocommerce') .'</th> 
        <th>'. __('Width', 'woocommerce') .'</th> 
        <th>'. __('Regular price', 'woocommerce') .'</th> 
        <th>'. __('Sale price', 'woocommerce') .'</th> 
       </tr> 
      </thead> 
      <tbody>'; 

     foreach($available_variations as $variation){ 
      // Get an instance of the WC_Product_Variation object 
      $product_variation = wc_get_product($variation['variation_id']); 

      $sale_price = $product_variation->get_sale_price(); 
      if(empty($sale_price)) $sale_price = __('<em>(empty)</em>', 'woocommerce'); 

      $output .= ' 
      <tr> 
       <td>'. $product_variation->get_height() .'</td> 
       <td>'. $product_variation->get_width() .'</td> 
       <td>'. $product_variation->get_regular_price() .'</td> 
       <td>'. $sale_price .'</td> 
      <tr>'; 
     } 
     $output .= ' 
      </tbody> 
     <table>'; 

     echo $output; 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルに行きます。

すべてのコードは、Woocommerce 3+でテストされ、動作します。

関連する問題