在WooCommerce中的选择属性下拉列表中显示变化数据

时间:2018-08-29 00:38:28

标签: php wordpress woocommerce drop-down-menu variations

我对如何在已重新设置为切换框样式的选项字段中显示产品的变化数据的正确方法感到困惑。

我想这样显示数据:

  • 版本名称
  • 变动价格
  • 如果可购买
  • 版本描述
  • 变化图片

我已经很接近了,使用脚本(下面是我唯一可以找到的脚本),但是似乎存在一些问题。邓诺(Dunno)也许没有foreach。

我只能检索名称和价格。

而且,我不确定是否有更好,更清洁的方法来实现这一目标。

这是我的代码:

add_filter( 'woocommerce_variation_option_name', 'display_data_in_variation_option_name' );

function display_data_in_variation_option_name( $term ) {
  global $wpdb, $product;

  $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

  $term_slug = ( !empty( $result ) ) ? $result[0] : $term;

  $query = "SELECT postmeta.post_id AS product_id
  FROM {$wpdb->prefix}postmeta AS postmeta
  LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
  WHERE postmeta.meta_key LIKE 'attribute_%'
  AND postmeta.meta_value = '$term_slug'
  AND products.post_parent = $product->id";

  $variation_id = $wpdb->get_col( $query );

  $parent = wp_get_post_parent_id( $variation_id[0] );

  if ( $parent > 0 ) {


    $variation = new WC_Product_Variation( $variation_id[0] );
    //$variation = wc_get_product( $variation_id[0] );


    $tPrice = "";
    $isPurchasable = "";
    $isVisible = "";
    $tVariations = "";
    $inStock = "";
    $tImages = "";


    $tPrice = strip_tags(woocommerce_price( $variation->get_price() ));
    $isPurchasable = $variation->is_purchasable();
    $isVisible = $variation->variation_is_visible();
    $tVariations = $this->get_attributes( $variation );
    //$inStock = $variation->is_in_stock();
    //$tImages = $variation->get_images($variation);

    //Shows all data of vars
    //var_dump($variation);



return   "Name: " . $term . " Price: " .$tPrice . " Purchable?:" . $isPurchasable . " Visible?: " . $isVisible . " Variations(an array): " . $tVariations . " In stock?: " .$inStock;

  }

  //return $term;


} 

0 个答案:

没有答案