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

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

從 WooCommerce 中的變體產品獲取自定義字段值和最高價格

從 WooCommerce 中的變體產品獲取自定義字段值和最高價格

PHP
蠱毒傳說 2023-06-18 17:45:45
我正在嘗試顯示來自 XX 的變體自定義價格跨度。最低價格來自(多個)自定義字段值,我需要使用最低值。最高價格應該是變化最高價格。如果變體具有bulk_price價值,我只想要這個,并且只在檔案頁面中顯示它。我需要獲取自定義字段值和最高價格。這就是我所擁有的:function change_product_price_display( $price) {    $bulk_price = get_post_meta([ 'variation_id' ], 'bulk_price', true);    $priceMax        = $product->get_variation_price('max'); // Max price    //only show in archives     if (is_product_category()) {        //only if there is a bulk price        if ( $bulk_price ) {            return ' <span class="price-suffix">' . ' From ' . get_woocommerce_currency_symbol() .__( $bulk_price , "woocommerce") . ' - ' . $priceMax   . '</span>';           }    }    //don't affect other products    else {        return $price;    }   }add_filter( 'woocommerce_get_price_html', 'change_product_price_display');add_filter( 'woocommerce_cart_item_price', 'change_product_price_display');
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

在產品類別存檔中顯示最低值(自定義字段)到最高價格。注釋并在代碼中添加解釋


// Display on product category archive lowest value to max price

function change_product_price_display( $price, $product ) {

    // Returns true when viewing a product category archive.

    if ( is_product_category() ) {

        // Set array

        $bulk_prices = array();


        // Loop for variations IDs

        foreach( $product->get_children() as $variation_id ) {

            // Get post meta

            $bulk_price = get_post_meta($variation_id, 'bulk_price', true);


            // True

            if( $bulk_price ) {

                // Push

                $bulk_prices[] = $bulk_price;   

            }

        }


        // NOT empty

        if( sizeof($bulk_prices) > 0 ) {

            // Sort: low to high

            sort($bulk_prices);


            // First value

            $lowest_value = reset( $bulk_prices );


            // Get max price

            $price_max = $product->get_variation_price('max');


            // Output

            $price = '<span class="price-suffix">From ' . get_woocommerce_currency_symbol() . $lowest_value . ' - ' . wc_price($price_max) . '</span>';

        }

    }


    return $price;

}

add_filter( 'woocommerce_variable_price_html', 'change_product_price_display', 10, 2 );

為清楚起見,創建和保存自定義字段的代碼


// Add custom field input product variations

function action_woocommerce_variation_options_pricing( $loop, $variation_data, $variation ) {  

    woocommerce_wp_text_input( array(

        'id'          => 'bulk_price[' . $loop . ']',

        'desc_tip'    => 'true',

        'description' => __( 'Enter the Bulk price here.', 'woocommerce' ),

        'label'       => __( 'Custom Field', 'woocommerce' ),

        'value'       => get_post_meta( $variation->ID, 'bulk_price', true )

    ));

}

add_action( 'woocommerce_variation_options_pricing', 'action_woocommerce_variation_options_pricing', 10, 3 );


// Save custom field on product variation save

function action_woocommerce_save_product_variation( $variation_id, $i ) {

    $bulk_price = $_POST['bulk_price'][$i];

    if ( isset( $bulk_price ) ) {

        update_post_meta( $variation_id, 'bulk_price', esc_attr( $bulk_price ) );

    }

}

add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );



查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 189 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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