亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

電子商務:如何顯示可變產品選項下拉列表的 SKU

電子商務:如何顯示可變產品選項下拉列表的 SKU

PHP
慕桂英3389331 2022-09-25 20:26:34
如何在可變產品的下拉選項中查看 SKU 代碼?我試圖使用此代碼,但它只是為了在woocommerce中查看價格選項。原因最近我使用其他代碼,但 SKU 不是動態的,當我更改和變量選項。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 并在前端顯示的變體選項區域旁邊查看。如果有人能幫助我解決問題,我真的知道。
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

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 );


查看完整回答
反對 回復 2022-09-25
  • 1 回答
  • 0 關注
  • 89 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號