在变体产品 Woocommerce 中获得价值的新自定义字段?

再会!我在变体产品 Woocommerce 中创建了新的自定义文件!如何获取该字段的值以在其他功能中使用他?


一般来说,我需要将一个值从一个函数传递给另一个函数,是否有可能做到这一点?


add_action( 'woocommerce_product_after_variable_attributes', 'num_cubicmeters_fields', 10, 3 );

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

    woocommerce_wp_text_input( array(

        'id'                => '_num_cubic_var[' . $variation->ID . ']', // id поля

        'label'             => 'Количество кубометров', // Надпись над полем

        'description'       => 'Укажи количество кубометров',// Описание поля

        'desc_tip'          => 'true', // Всплывающая подсказка

        'placeholder'       => 'Количество кубометров, м3', // Надпись внутри поля

        'type'              => 'number', // Тип поля

        'custom_attributes' => array( // Произвольные аттрибуты

            'step' => 'any', // Шаг значений

            'min'  => '0', // Минимальное значение

        ),

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

    ) );

}



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

function art_save_variation_settings_fields( $post_id ) {

    $woocommerce__num_cubic_var = $_POST['_num_cubic_var'][ $post_id ];

    if (isset($woocommerce__num_cubic_var) && ! empty( $woocommerce__num_cubic_var ) ) {

        update_post_meta( $post_id, '_num_cubic_var', esc_attr( $woocommerce__num_cubic_var ) );

    }

}

}

}

函数创建字段


富国沪深
浏览 66回答 1
1回答

万千封印

你是这个意思吗?function ut_before_calculate_totals( $cart_obj ) {&nbsp; &nbsp; foreach( $cart_obj->get_cart() as $key => $value ) {&nbsp; &nbsp; &nbsp; &nbsp; //echo '<pre>', print_r($value, 1), '</pre>';&nbsp; &nbsp; &nbsp; &nbsp; // The variation ID&nbsp; &nbsp; &nbsp; &nbsp; $variation_id = $value['data']->get_id();&nbsp; &nbsp; &nbsp; &nbsp; // output&nbsp; &nbsp; &nbsp; &nbsp; echo $variation_id . '<br>';&nbsp; &nbsp; &nbsp; &nbsp; // Get post meta&nbsp; &nbsp; &nbsp; &nbsp; $variations_num_cubic = get_post_meta( $variation_id, '_num_cubic_var', true );&nbsp; &nbsp; &nbsp; &nbsp; // output&nbsp; &nbsp; &nbsp; &nbsp; echo $variations_num_cubic . '<br>';&nbsp; &nbsp; }}add_action( 'woocommerce_before_calculate_totals', 'ut_before_calculate_totals', 10, 1 );
打开App,查看更多内容
随时随地看视频慕课网APP