将变体描述移动到WooCommerce中单个产品页面上的标题下方

时间:2017-12-28 05:50:27

标签: php wordpress woocommerce

我正在尝试将变体描述从产品摘要移到标题下方和价格上方。

我试过这段代码:

add_action('plugins_loaded', 'move_variation_description', 50);

function move_variation_description(){ 
  // Remove the hook from the original location
  remove_action( 'woocommerce_single_product_summary', array( 
WC_Variation_Description::get_instance()->frontend, 
'add_variation_description' ), 25 );
  // Re-add the hook to where you want it to be
  add_action( 'woocommerce_before_single_product_summary',  array( 
WC_Variation_Description::get_instance()->frontend, 
'add_variation_description' ), 25 );
}

但没有任何成功。来自Here的代码似乎也无效。

非常感谢任何帮助。

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以覆盖woocommerce模板。在您的主题中创建一个名为woocommerce的文件夹,并在woocommmerce插件中添加相应文件的相应文件夹。例如woocommerce / single-product / layouts /其中添加了product.php。 在该重写文件中,您可以通过更改HTML将变体描述从product-summary移动到标题下方和价格上方。

答案 1 :(得分:1)

使用functions.php

中的以下代码实现了它
//Move Variation Description
add_action( 'wp_footer', 'ec_child_modify_wc_variation_desc_position' );
function ec_child_modify_wc_variation_desc_position() {
    ?>
    <script>
    (function($) {
        $(document).on( 'found_variation', function() {
            var desc = $( '.woocommerce-variation.single_variation' ).find( 
'.woocommerce-variation-description' ).html();
            var $entry_summary = $( '.product_title' ), $wc_var_desc = 
$entry_summary.find( '.woocommerce-variation-description' );

            if ( $wc_var_desc.length == 0 ) {
                $entry_summary.append( '<div class="woocommerce-variation-
description"></div>' );
            }
            $entry_summary.find( '.woocommerce-variation-description' 
).html( desc );
        });
    })( jQuery );

    </script>
    <style>form.variations_form .woocommerce-variation-description { 
display: none; }</style>
    <?php
}

答案 2 :(得分:1)

要实现此目标有些棘手,因为版本说明似乎与“添加到购物车”按钮捆绑在一起。我使用以下方法(最后说明)实现了此目的:

  1. 您需要删除产品简短说明,因为它总是超出版本说明的上方。只需添加:

    add_action( 'woocommerce_single_product_summary', 'single_product_summary_action', 1 );
    function single_product_summary_action() { // remove the single_excerpt //(we will manually add it back in the add-to-cart/variation-add-to-cart-button) remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 ); }

  2. 您需要覆盖WooCommerce模板以使用变体“添加到购物车”按钮。为此,您可以在主题中复制WooCommerce文件templates/single-product/add-to-cart/variation-add-to-cart-button.php。 在该新文件中搜索该行

    do_action('woocommerce_after_add_to_cart_quantity');

并在其后添加此内容:

woocommerce_template_single_excerpt();

这将重新添加产品简短说明。

通过这种方式,版本说明将上移。

请注意: -此解决方案移动简短描述,而不是变化描述。但是最终结果是版本说明也将向上移动。 -查看WooCommerce文件templates/content-single-product.php。这指定了WC通过woocommerce_single_product_summary操作加载模板各个部分的顺序。那是我获得20的删除操作的地方。

相关问题