【问题标题】:Add custom text when product is on backorder using manage stock使用管理库存在产品缺货时添加自定义文本
【发布时间】:2021-05-15 20:06:37
【问题描述】:

我正在尝试在产品的状态为 BACKORDER 时显示自定义文本。我不想使用 woocommerce_get_availabilty_text,因为它会导致与显示“AVAILABLE ON BACKORDER”文本的插件发生冲突,我希望在插件文本中添加自定义文本,该文本将显示在所有具有“延期交货”状态的产品上 我尝试的代码:

   add_action( 'woocommerce_single_product_summary', 'custom_action_after_single_product_title', 25 );
    function custom_action_after_single_product_title() { 
    global $product; 


     if (! $product->managing_stock() && $product->is_on_backorder() ){
        $text = __('???????? ???????????? ???????????????????????????? ???????? ???????????? ???????? ????????????????????, ???????????? ???????????????????????????????? ???????????????? ???????? ???????????????????? 1 ???????????????? (???????????????????????????????? ???????????????? ???????????? ???????????????????????????? ???????? ???????? ???????????????????? ???????? ???????????? ????????????????????????????????)', 'woocommerce');
        $color = 'color:#bf0606;';
        $font  = 'itlaic';
    }
}

有什么办法可以做到吗?

【问题讨论】:

  • 您是否考虑过简单地将一些文本添加到模板挂钩中?例如,在摘要或标题或价格等之前或之后添加它。如果是这样,您可以直接使用它,这样它就不会与任何其他插件冲突。
  • 是的,但我只想在产品缺货时显示文本。因此,如果产品有库存或缺货,则不会显示文本。
  • 可以使用与上面相同的条件,只需将钩子名称更改为您希望文本显示的位置的名称即可。我将发布一个答案作为示例,您可以从那里进行开发。

标签: php wordpress woocommerce product status


【解决方案1】:

已更新。我设法让它适用于缺货但延期交货的产品。

查看此链接以了解您可以在产品页面上使用的其他挂钩。 https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/

function custom_action_after_single_product_title() {

    //Declare global product variable
    global $product; 

    //Check to see if product is on back order
    if ( $product->is_on_backorder() == true ){

        //Spit out some text
        echo '<p>Your text here</p>';

    }

}
add_action( 'woocommerce_before_single_product_summary', 'custom_action_after_single_product_title', 20 );

【讨论】:

  • 感谢您的努力,但不幸的是,代码不起作用。
  • 好的,我会在全新安装和更新时测试它
猜你喜欢
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 2017-09-27
  • 2018-03-12
相关资源
最近更新 更多