【问题标题】:WooCommerce wc_price( $price ) & variations & headacheWooCommerce wc_price($price) & 变化 & 头痛
【发布时间】:2020-07-28 13:11:36
【问题描述】:

我遇到了一些 PHP 初学者的问题...

我想根据产品变体是出租还是出售来更改可变产品的价格标签。每个产品都有两种选择。

我发现产品的帖子标题包含文字“租”或“购买”,具体取决于变体。

因此,我已经在此处找到了一些代码,以尝试根据产品帖子标题的结果显示不同的标签。目前看起来像这样(但不起作用!),感觉这完全是错误的方法,有人可以帮忙吗?

function sv_change_product_html( $price_html, $product ) {

$is_rental = get_post_meta( $product->id, 'post_title' );
$price = $product->get_regular_price();

if (strpos($is_rental, "Rent") !== false) {
    $price_html = '<span class="amount">' . wc_price( $price ) . ' per week</span>';    
}
 else
 {
$price_html = '<span class="amount">' . wc_price( $price ) . ' buy today</span>';
}

return $price_html;
}    

【问题讨论】:

  • 更新,变体文本实际上似乎在 post_excerpt,而不是 post_title.. 仍然不起作用

标签: php wordpress woocommerce product price


【解决方案1】:

可能更好的方法是检查随附的产品属性,该属性根据您的变体(“租”/“购买”)存储值,然后从那里开展业务,因为这种方法看起来很老套,而且如果店主决定将来改变他们命名产品的方式,可能会失败。

此外,您还可以使用 WooCommerce 类 (Product/Product Variation) 中提供的方法获取名称 ($product-&gt;get_name())、属性 ($product-&gt;get_attribute(&lt;attr_name&gt;)) 等产品属性

另外,您的函数是否与正确的过滤器挂钩?

【讨论】:

  • Hacky 可能是正确的描述! :-) 它与 > add_filter( 'woocommerce_get_price_html', 'sv_change_product_html', 10, 2 ); 挂钩
  • 如果函数被正确钩住但你仍然没有看到任何变化,那么这意味着你的条件块没有工作。尝试在代码末尾返回一个静态值(如return '100';),看看它是否反映在前端。另外,由于我之前所说的方法存在问题,我建议您使用我建议的方法。
  • 我放弃了,改用了这个>businessbloomer.com/…
猜你喜欢
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 2010-11-08
  • 2012-09-07
相关资源
最近更新 更多