简体   繁体   English

仅在 WooCommerce 单个产品页面上禁用可变产品价格范围

[英]Disable Variable Product Price Range on the WooCommerce Single Product Pages Only

I'm looking to hide variable products price range only on the single product pages for the product that is currently being viewed.我希望仅在当前正在查看的产品的单个产品页面上隐藏可变产品价格范围。 I do not want the price range to be disabled for all the products listed in the "Related Products" below and any products listed in the widgets in the footer as well.我不希望对下面“相关产品”中列出的所有产品以及页脚小部件中列出的任何产品禁用价格范围。

Everyone of the snippets I've found so far hides the price range for every product listed on the single products page.到目前为止,我发现的每个片段都隐藏了单个产品页面上列出的每个产品的价格范围。 It also hides the price completely from view from the variable products that have the exact same price for all of them since they do not display below the drop down when selected.它还从所有具有完全相同价格的可变产品中完全隐藏价格,因为它们在选择时不会显示在下拉列表下方。

Here is an example of a snippet I've found:这是我发现的一个片段示例:

/*
Disable Variable Product Price Range completely:
*/

add_filter( 'woocommerce_variable_sale_price_html', 'my_remove_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'my_remove_variation_price', 10, 2 );

function my_remove_variation_price( $price ) {
  $price = '';
  return $price;
}

Example of price issue below:以下价格问题示例:
在此处输入图片说明

Let's look on the Visual Hook Guide .让我们看看Visual Hook 指南 The price range is a part of the single product summary.价格区间是单品汇总的一部分。 The woocommerce_single_product_summary hook consists of: woocommerce_single_product_summary钩子包括:

 * @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

And the woocommerce_template_single_price function adds the single-product/price.php template-part. woocommerce_template_single_price函数添加了single-product/price.php模板部分。

So we have two ways:所以我们有两种方法:

1. Remove this template part for variable products only 1. 仅针对可变产品移除此模板部分

add_action( 'woocommerce_before_single_product', 'my_remove_variation_price' );
function my_remove_variation_price() {
  global $product;
  if ( $product->is_type( 'variable' ) ) {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' );
  }
}

2. Or edit the template part itself 2. 或者编辑模板部分本身

Copy wp-content/plugins/woocommerce/templates/single-product/price.php to wp-content/themes/YOUR_THEME/woocommerce/single-product/price.php and replace this code fragmentwp-content/plugins/woocommerce/templates/single-product/price.phpwp-content/themes/YOUR_THEME/woocommerce/single-product/price.php并替换此代码片段

  <p class="price"><?php echo $product->get_price_html(); ?></p>

with this one有了这个

<?php if ( ! $product->is_type( 'variable' ) ) : ?>
  <p class="price"><?php echo $product->get_price_html(); ?></p>
<?php endif; ?>
add_filter( 'woocommerce_variable_sale_price_html', 'ninja_remove_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'ninja_remove_variation_price', 10, 2 );

function ninja_remove_variation_price( $price ) {
if ( is_product() ) {
$price = '';
}
return $price;
}

This code will remove "From" text without removing the price completely for variable products.此代码将删除“From”文本,而不会完全删除可变产品的价格。

  add_filter( 'woocommerce_variable_price_html','variation_price_min', 9999, 2 );

  function variation_price_min( $price, $product ) {
  $prices = $product->get_variation_prices( true );
  $min_price = current( $prices['price'] );
  $price = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $min_price ) );
  return $price;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM