且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加入购物车按钮内的 Woocommerce 变体产品价格

更新时间:2023-10-25 14:28:10

也许对某人有用!结果.可变产品的加入购物车按钮

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 );//商店和其他档案页面add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2);//单品页面函数 custom_add_to_cart_price( $button_text, $product ) {//变量产品if( $product->is_type('variable') ) {//商店和档案如果(!is_product()){$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) );返回 $button_text .' - 从 ' .strip_tags( $product_price );}//单品页面别的 {$variations_data =[];//初始化//遍历变体数据foreach($product->get_available_variations() 作为 $variation ) {//在数据数组中为每个变体 ID 设置相应的价格(在 jQuery 中使用)$variations_data[$variation['variation_id']] = $variation['display_price'];}?><脚本>jQuery(函数($){var jsonData = <?php echo json_encode($variations_data);?>,inputVID = 'input.variation_id';$('输入').change(函数(){if( '' != $(inputVID).val() ) {var vid = $(inputVID).val(),//变量 ID价格 = '';//初始化//遍历变体 ID/价格对$.each( jsonData, function( index, price ) {if( index == $(inputVID).val() ) {vprice = Math.round(price);//正确的变化价格}});//更改选项时动态更改价格$("button.single_add_to_cart_button.button.alt span").remove();$(".single_add_to_cart_button").append("" + " " + vprice + " ₴" + "</span>");}});});</script><?php返回 $button_text;}}//所有其他产品类型别的 {$product_price = wc_price( wc_get_price_to_display( $product ) );返回 $button_text .' ' .strip_tags( $product_price );}}

I use "Display price on add to cart button from the functions.php file in Woocommerce" answer code, that add price inside simple product.

But I want to improve this function and dynamically display chosen variation price inside button. With this code on variation product page it display only the lowest price. Is there any solution?

How add to cart button looks now (no options chosen):

How I want to be displayed:

Maybe it will be useful for somebody! Result. Add to cart button of variable product

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
    // Variable products
    if( $product->is_type('variable') ) {
        // shop and archives
        if( ! is_product() ){

            $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
            return $button_text . ' - From ' . strip_tags( $product_price );
        } 
        // Single product pages
        else {
            $variations_data =[]; // Initializing

        // Loop through variations data
        foreach($product->get_available_variations() as $variation ) {
            // Set for each variation ID the corresponding price in the data array (to be used in jQuery)
            $variations_data[$variation['variation_id']] = $variation['display_price'];
        }
        ?>
        <script>
        jQuery(function($) {
            var jsonData = <?php echo json_encode($variations_data); ?>,
                inputVID = 'input.variation_id';

            $('input').change( function(){
                if( '' != $(inputVID).val() ) {
                    var vid      = $(inputVID).val(), // VARIATION ID
                       vprice   = ''; // Initilizing

                    // Loop through variation IDs / Prices pairs
                    $.each( jsonData, function( index, price ) {
                        if( index == $(inputVID).val() ) {
                            vprice = Math.round(price); // The right variation price
                        }
                    });
                    // Change price dynamically when changing options
                    $( "button.single_add_to_cart_button.button.alt span" ).remove();
                    $(".single_add_to_cart_button").append("<span>" + " " + vprice + " ₴" + "</span>");
                }
            });
        });
        </script><?php
            return $button_text;
        }
    } 
    // All other product types
    else {
        $product_price = wc_price( wc_get_price_to_display( $product ) );
        return $button_text . '  ' . strip_tags( $product_price );
    }
}