How To Move Product Variation Descriptions in WooC

2020-02-13 05:31发布

问题:

I'd like to move my product's variation descriptions in Woocommerce beneath the add to cart button, and I can't find what hook I'm supposed to use. These are the variation's custom descriptions that load on selection in AJAX.

I'm able to hook another custom function beneath the add to cart button. So I think my problem is not knowing the name of the hook and/or if it's a hook versus a filter. I think it's either woocommerce_before_single_variation or woocommerce_before_add_to_cart_button.

Here's several attempts I've tried before with no luck in functions.php:

remove_action( 'woocommerce_after_single_variation','woocommerce_single_product_summary', 20 );
add_action( 'woocommerce_after_single_variation', 'woocommerce_single_product_summary',  9 );

//try #2
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
add_action('woocommerce_after_add_to_cart_button', 'woocommerce_single_variation', 35);

Thank you!

回答1:

The functionality I was looking for is included in WooCommerce 2.4 as a default but is not done on a hook. It's added by jQuery updating a div instead - which I found in woocommerce/js/assets/frontend/add-to-cart-variation.js. So I moved the div's location instead:

add_action ('woocommerce_after_single_variation', 'move_descriptions', 50);
function move_descriptions() {
    ?>
    <div class="woocommerce-variation-description" style="border: 1px solid transparent; height: auto;"></div>
    <?php
}


回答2:

I think this will do,

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20); 
add_action('woocommerce_after_add_to_cart_form', 'woocommerce_template_single_excerpt');

Only move Short description if product is variable

add_action('wp_head', 'move_short_desc_for_varition');
function move_short_desc_for_varition() {
    #
    global  $post;
    $product = get_product( $post->ID );
    if( $product->is_type( 'variable' ) ){ 
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20); 
        add_action('woocommerce_after_add_to_cart_form', 'woocommerce_template_single_excerpt');
    }
}


回答3:

The problem is in the priority of the remove hook.

I give you an example (this work for me):

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 15 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );

Pay attention and try with differents priorities.