【问题标题】:How can I remove price range for variable products with only one variation?如何删除只有一种变体的可变产品的价格范围?
【发布时间】:2020-08-18 16:29:54
【问题描述】:

我有一个基于 wordpress 和 woocommerce 的家具网站。为了保持一致性,我希望所有床架都有变化,即使它们只有一种尺寸(特大号床、大号床、双床)。但是,woocommerce 上的变体产品自然具有描述变体之间的最低价格到最高价格的价格范围。如果我只有一个变体,它有一个空白的最低价格,并且仍然有连字符,然后是最高价格,如下所示,使它看起来像是负数。如何删除具有一种变体的产品的连字符(包括禁用所有其他变体的位置)。

这个网站是 khemlanimart.com

编辑:我不希望它只替换为最低价格,因为当有一个变化时,最低价格为空。如果我想在只有一种变化的情况下将其替换为最高价格,并且在有多种变化时仍保持价格范围

【问题讨论】:

  • 我向 Loic 道歉。未来我会做得更好。我只是不熟悉 php,但这不是借口
  • 好的……我刚刚更新了我的答案,有一个错误。

标签: php wordpress woocommerce


【解决方案1】:

已更新 (将不正确的$this 替换为$product

尝试以下操作,这应该会删除具有一种变体的可变产品的价格范围:

add_filter( 'woocommerce_variable_price_html', 'variable_products_with_one_variation', 10, 2 );
function variable_products_with_one_variation( $price_html, $product ) {
    $prices = $product->get_variation_prices( true );
    
    if( count($prices['price']) == 1 ) {
        $active_price = end( $prices['price'] );
        $reg_price    = end( $prices['regular_price'] );
        
        if ( $this->is_on_sale() ) {
            $price = wc_format_sale_price( wc_price( $reg_price ), wc_price( $active_price ) );
        } else {
            $price = wc_price( $active_price );
        }
    }
    return $price;
}

它应该工作。无论如何,如果它不起作用,请使用this is the right hook

【讨论】:

  • 嗯。代码不起作用。我收到了这个致命错误:未捕获的错误:不在 /var/www/html/wp-content/themes/storefront-child/functions.php:25 的对象上下文中使用 $this 堆栈跟踪:#0 。现在正在尝试调试。不过谢谢你并尝试你提到的链接
  • 我也尝试了您的其他代码:stackoverflow.com/questions/48832078/… 并得到:警告:call_user_func_array() 期望参数 1 是有效的回调,未找到函数“custom_variation_price_html”或 /var/ 中的函数名称无效www/html/wp-includes/class-wp-hook.php 在第 287 行。似乎没有任何改变。
  • @VikramKhemlani 我已经更新了Custom Woocommerce variable product price range for more than one active variation ...这只是一个额外的空白。
  • @VikramKhemlani 在我的回答中,连字符可能在格式化的销售价格上,但不再有任何价格范围......
  • 这行得通。我的插件设置有问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2015-07-23
相关资源
最近更新 更多