The javascript file woocommerce/assets/js/frontend/add-to-cart-variation.js
, responsible for displaying variable product prices, basically uses $('form').find('.single_variation');
to update the price, so if the .single_variation
is outside of the form, it will not work.
So things like this do not work:
function move_variation_price() {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_single_variation', 5 );
}
add_action( 'woocommerce_before_add_to_cart_form', 'move_variation_price' );
That said, your best bet if you want to use it on top of your product, is to use the woocommerce_before_variations_form
hook.
woocommerce_single_variation
hook will bring up the add to cart button too, so you will have to hide that out with CSS:
form.variations_form.cart .single_variation_wrap:nth-child(1) .quantity {
display: none !important; /* important is necessary */
}
form.variations_form.cart .single_variation_wrap:nth-child(1) button {
display: none;
}
I know it hurts. But it's the "only" way.
Method 2
Ok, I was pissed with Method 1 and came up with this new method, that doesn't require any changes in PHP, only Javascript and CSS.
Javascript that will check if the variable product price changed, and will update the actual price div with the new value. This new price div can be anywhere, not only inside the form.
Javascript:
// Update price according to variable price
if (jQuery('form.variations_form').length !== 0) {
var form = jQuery('form.variations_form');
var variable_product_price = '';
setInterval(function() {
if (jQuery('.single_variation_wrap span.price span.amount').length !== 0) {
if (jQuery('.single_variation_wrap span.price span.amount').text() !== variable_product_price) {
variable_product_price = jQuery('.single_variation_wrap span.price span.amount').text();
jQuery('.single-product-summary p.price span.amount').text(variable_product_price);
}
}
}, 500);
}
CSS:
.single_variation_wrap .price {
display: none;
}
Final result:
