Slaveworx
Slaveworx

Reputation: 611

Woocommerce Change Position of Product Variations

I am trying to change the position of the Product Variations inside the Single Product Page. Right now, they appear under the Short Description but I want them to appear before the Product's Short Description, right after the product Title and Price.

The code I have for the moment is the following:

function move_linked_variation_options()
{

    remove_action('woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
    add_action('woocommerce_single_product_summary', 'woocommerce_variable_add_to_cart', 10);
}

add_action('init', 'move_linked_variation_options');

It works great when products have variations, but when I navigate to a product that has no variations, it just throws an error which is the following:

the error

Do you have any idea why this is happening? Should I have some kind of conditional inside the function? Am I missing any action before the position change?

Any help will be appreciated.

Upvotes: 0

Views: 535

Answers (1)

Asiqur Rahman
Asiqur Rahman

Reputation: 431

Hope this helps

add_action( 'woocommerce_before_single_product_summary', 'move_linked_variation_options' );
function move_linked_variation_options() {
    global $product;
    
    if ( $product->is_type( 'variable' ) ) {
        remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
        add_action( 'woocommerce_single_product_summary', 'woocommerce_variable_add_to_cart', 10 );
    }
}

Upvotes: 1

Related Questions