Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

woocommerce stock notification priority

I am trying to arrange the order of the product summary such that price comes after stock notification which comes after short description. I am aware of the priorities as defined in content-single-product.php

/**
         * woocommerce_single_product_summary hook.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         * @hooked WC_Structured_Data::generate_product_data() - 60
         */
        do_action( 'woocommerce_single_product_summary' );

However, I do not see where stock is hooked in. I cannot find it in the documentation or the code. If I knew the hook and priority for the stock template, I could solve my problem. I have tried

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_stock', 30 );

with no results. Can anyone help? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally figured it out. Stock status is hardcoded into the templates, so you must first remove it from simple.php and variation.php in your child theme. (And grouped.php too, but I don't use grouped products, so I didn't need to update that template.)

Delete these lines from variation.php

    <div class="woocommerce-variation-availability">
    {{{ data.variation.availability_html }}}
</div>

and this line from simple.php (line 28)

echo wc_get_stock_html( $product );

Then in your functions.php add the following code changing the priority to where ever you want the stock status to show up.

add_action( 'woocommerce_single_product_summary', 'mh_output_stock_status', 21 );

function mh_output_stock_status ( ) {
    global $product;

    echo wc_get_stock_html( $product );

}

I really wish stock was hooked in like the rest of the summary. It would be a lot more straight forward.

I hope this helps someone else.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...