2017-04-21 3 views
0

こんにちは私は今、3.0Woocommerce 3.0のgetバリエーション名

にWCのupgarde後にもう動作しませんバリエーション名とその価格の抽出物を得るためにWoocommerceの古いバージョンで働いていた以下のコードを、持っていました特に
function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "") 
{ 
    $output = ""; 
    $meta['el_class']; 

    global $woocommerce, $product; 
    if(!is_object($woocommerce) || !is_object($woocommerce->query) || empty($product)) return; 

    // $product = wc_get_product(); 

    $output .= "<div class='av-woo-calendar-button ".$meta['el_class']."'>"; 
    ob_start();?> 

     <table cellspacing="0" cellpadding="2"> 
      <thead> 
       <tr> 
        <th scope="col" style="text-align:left; background-color:rgba(155, 199, 239, 0.5); vertical-align:middle;"><?php _e('Cruise', 'whale_dolphins'); ?></th> 
        <th scope="col" style="text-align:center;background-color:rgba(155, 199, 239, 0.5);"><?php _e('Places<br/>available', 'whale_dolphins'); ?></th> 
       </tr> 
      </thead> 
      <tbody> 
      <?php 

      $args = array(
       'post_type'   => 'product_variation', 
       'post_status'  => 'publish', 
       'posts_per_page' => -1, 
       'orderby'   => 'title', 
       'order'    => 'ASC', 
       'meta_query' => array(
        array(
         'key'  => '_stock', 
         'value'  => array('', false, null), 
         'compare' => 'NOT IN' 
        ) 
       ) 
      ); 

      $loop = new WP_Query($args); 

      while ($loop->have_posts()) : $loop->the_post(); 

          $product = new WC_Product_Variation($loop->post->ID); 
        if (get_the_title($loop->post->post_parent) == 'CSR Expeditions Payment' || get_the_title($loop->post->post_parent) == 'Pagamento Spedizioni CSR') 
        {  
         $variation_formatted_name = $product->get_formatted_name(); 
         $variation_name_array = explode("&ndash;", $variation_formatted_name); 
         $variation_name = $variation_name_array[2]; 
         $variation_price = $variation_name_array[3]; 
         $variation_sku = $variation_name_array[0]; 
         $variation_name_only = explode(":", $variation_name); 
         $variation_name_only = $variation_name_only[1]; 
       ?> 

       <tr> 
        <td><?php echo $variation_name_only ."&ndash;" . $variation_price; ?></td> 
        <?php 
         if (intval ($product->stock) >0) 
         { ?> 
          <td style="text-align:center; font-size: 13px; background-color:#70C940; color:rgba(255,255,255,1);"><?php echo intval ($product->stock); ?></td> 
         <?php 
         } 
         else 
         { ?> 
          <td style="text-align:center; font-size: 13px; background-color:#D11E1B;color:rgba(255,255,255,1);"> <?php echo intval ($product->stock); ?></td> 
         <?php  
         } 

        ?> 
       </tr> 
       <?php 
        } 
      endwhile; 

      ?> 
      </tbody> 
     </table> 

    <?php 

    $output .= ob_get_clean(); 
    $output .= "</div>"; 

    return $output; 
} 

私は、次のコードの一部は、

$product = new WC_Product_Variation($loop->post->ID); 
        if (get_the_title($loop->post->post_parent) == 'CSR Expeditions Payment' || get_the_title($loop->post->post_parent) == 'Pagamento Spedizioni CSR') 
        {  
         $variation_formatted_name = $product->get_formatted_name(); 
         $variation_name_array = explode("&ndash;", $variation_formatted_name); 
         $variation_name = $variation_name_array[2]; 
         $variation_price = $variation_name_array[3]; 
         $variation_sku = $variation_name_array[0]; 
         $variation_name_only = explode(":", $variation_name); 
         $variation_name_only = $variation_name_only[1]; 

私はそれを修正する方法上の任意の提案を修正することを想像?非常に

+0

例えば、 '$製品 - >株式;'今でなければなりません[** '$製品 - > get_stock_quantity();' **](https://docs.woocommerce.com/wc-apidocs/ $ product = new WC_Product_Variation($ loop-> post-> ID); '$ product = wc_get_product($ loop-> post-> ID)の代わりに' $ product = new WC_Product_Variation($ loop-> post-> ID) > ID); '...オブジェクトと配列に対して' print_r() 'や' var_dump() 'を使ってテストする必要があります... – LoicTheAztec

+0

こんにちは@LoicTheAztec、私はあなたの提案を試みましたが、うまくいかなかった。株式については、$ product->株式は、$ product-> get_stock_quantity()と同じ方法で崩壊します。私は適切な在庫量を得ることができます。しかし、この問題はまだバリエーション名に残っています。私は、たとえ私がwc_get_product関数を使っていても、バリエーション・スラッグは取得できますが、フル・ネームは取得できません。 –

+0

ところで、コードは次の例に基づいています。https://mikejolley.com/2011/12/05/woocommerce-output- a-simple-printable-stockinventory-report / –

答えて

0

くらい私はそれを必要とするかもしれないanyboduìyのためにここに下記掲載された解決策を見つけた

を解決しましたありがとうございます。新Woocommerce「製品名(#variation_id)」を次のように文字列を返します$product->get_formatted_name();コマンドで

はvariation_idで

は、しかし、postmetaテーブルから属性要素スラグを得ることが可能です。スラッグを使用すると、用語表から属性要素名を取得できます。

$product = new WC_Product_Variation ($loop->post->ID); 
      if (get_the_title($loop->post->post_parent) == 'CSR Expeditions Payment' || get_the_title($loop->post->post_parent) == 'Pagamento Spedizioni CSR') 
      { 
      //1) I need to get the variation id == post id in wp_postmeta table 
      // get the name and id of the variation in a string     
      $variation_formatted_name = $product->get_formatted_name(); 
      //explode the string to get an array of the string 
      $variation_formatted_name_array = explode("#", $variation_formatted_name); 
      //get only the variation id (which comes along with a closed parenthesis 
      $variation_id_ = $variation_formatted_name_array[1]; 
      //explode the string in an array made by the variation id and the parenthesis 
      $variation_id = explode(")", $variation_id_); 
      //get only variation id 
      $variation_id_only = $variation_id[0]; 

      //2) 
      //get the taxonomy needed 
      $taxonomy = 'pa_csr-dates'; 
      //get the meta value (= attribute slug) from the postmeta table through the variation_id (= post_id) 
      $meta = get_post_meta($variation_id_only, 'attribute_'.$taxonomy, true); 
      //get the variation name from the terms table through the slug (=$meta) 
      $term = get_term_by('slug', $meta, $taxonomy); 
      //get the variation name (attribute element custom name) 
      $variation_name_only = $term->name; 
      $variation_price = $product->get_price(); 
      } 
関連する問題