wordpress Woocommerce:如何显示变量产品选项的图标

flseospp  于 5个月前  发布在  WordPress
关注(0)|答案(1)|浏览(62)

如何查看可变产品的价格选项中的价格代码?我已经尝试使用此代码,但它只是为了查看woocommerce中的价格选项。因为最近我使用其他代码,但当我更改和变量选项时,价格代码不会动态。

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

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

  if( isset( $product ) ) {

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

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

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

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

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

    if ( $parent > 0 ) {
      $_product = new WC_Product_Variation( $variation_id[0] );

      $itemPrice = strip_tags (wc_price( $_product->get_price() ));
      //this is where you can actually customize how the price is displayed
      return $term . ' ('. $itemPrice .')';
    }
  }
}

字符串
基于此代码,我如何分配get-sku和视图在前端显示的变化选项区域旁边。如果有人能帮助我解决一个问题,我真的很感激。

zazmityj

zazmityj1#

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

    if( isset( $product ) ) {
        $query = sprintf( "SELECT postmeta.post_id AS product_id
            FROM {$wpdb->prefix}postmeta postmeta
            LEFT JOIN {$wpdb->prefix}posts products ON ( products.id = postmeta.post_id )
            WHERE postmeta.meta_key LIKE 'attribute_%%'
            AND postmeta.meta_value = '%s'
            AND products.post_parent = %d", $term, $product->get_id() );

        $results = $wpdb->get_results( $query );

        foreach ($results as $key => $result) {
            $variation_id = $result->product_id;
        }

        $variation_sku = get_post_meta( $variation_id , '_sku', TRUE );

        $term = $term . ' ('. $variation_sku .')';
    }

    return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name', 10, 1 );

字符串

相关问题