从 WooCommerce 中的变体产品获取自定义字段值和最高价格

我正在尝试显示来自 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');


蛊毒传说
浏览 170回答 1
1回答

白板的微信

在产品类别存档中显示最低值(自定义字段)到最高价格。注释并在代码中添加解释// Display on product category archive lowest value to max pricefunction change_product_price_display( $price, $product ) {&nbsp; &nbsp; // Returns true when viewing a product category archive.&nbsp; &nbsp; if ( is_product_category() ) {&nbsp; &nbsp; &nbsp; &nbsp; // Set array&nbsp; &nbsp; &nbsp; &nbsp; $bulk_prices = array();&nbsp; &nbsp; &nbsp; &nbsp; // Loop for variations IDs&nbsp; &nbsp; &nbsp; &nbsp; foreach( $product->get_children() as $variation_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get post meta&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bulk_price = get_post_meta($variation_id, 'bulk_price', true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $bulk_price ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Push&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bulk_prices[] = $bulk_price;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // NOT empty&nbsp; &nbsp; &nbsp; &nbsp; if( sizeof($bulk_prices) > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Sort: low to high&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort($bulk_prices);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // First value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $lowest_value = reset( $bulk_prices );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get max price&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price_max = $product->get_variation_price('max');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = '<span class="price-suffix">From ' . get_woocommerce_currency_symbol() . $lowest_value . ' - ' . wc_price($price_max) . '</span>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $price;}add_filter( 'woocommerce_variable_price_html', 'change_product_price_display', 10, 2 );为清楚起见,创建和保存自定义字段的代码// Add custom field input product variationsfunction action_woocommerce_variation_options_pricing( $loop, $variation_data, $variation ) {&nbsp;&nbsp;&nbsp; &nbsp; woocommerce_wp_text_input( array(&nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'bulk_price[' . $loop . ']',&nbsp; &nbsp; &nbsp; &nbsp; 'desc_tip'&nbsp; &nbsp; => 'true',&nbsp; &nbsp; &nbsp; &nbsp; 'description' => __( 'Enter the Bulk price here.', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp;=> __( 'Custom Field', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; 'value'&nbsp; &nbsp; &nbsp; &nbsp;=> get_post_meta( $variation->ID, 'bulk_price', true )&nbsp; &nbsp; ));}add_action( 'woocommerce_variation_options_pricing', 'action_woocommerce_variation_options_pricing', 10, 3 );// Save custom field on product variation savefunction action_woocommerce_save_product_variation( $variation_id, $i ) {&nbsp; &nbsp; $bulk_price = $_POST['bulk_price'][$i];&nbsp; &nbsp; if ( isset( $bulk_price ) ) {&nbsp; &nbsp; &nbsp; &nbsp; update_post_meta( $variation_id, 'bulk_price', esc_attr( $bulk_price ) );&nbsp; &nbsp; }}add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );
打开App,查看更多内容
随时随地看视频慕课网APP