【问题标题】:How to target an array of Product IDs in WooCommerce?如何在 WooCommerce 中定位一系列产品 ID?
【发布时间】:2021-07-11 09:12:33
【问题描述】:

当价格字段为空时,我正在使用下面的代码将产品价格更改为文本(联系定价):

add_filter( 'woocommerce_get_price_html', 'change_special_product_price', 10, 2 );
function change_special_product_price( $price_html, $product ) {
    if ( $product->id == 6917 ) {
        $price_html = '<span class="woocommerce-Price-amount amount">Contact for Pricing</span>';
    }

    return $price_html;
}

add_filter('woocommerce_empty_price_html', 'custom_call_for_empty_price_html');
function custom_call_for_empty_price_html() {
    return 'TBC';
}

function dequeue_woocommerce_styles_scripts() {
    if ( function_exists( 'is_woocommerce' ) ) {
        if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
        }
    }
}

它适用于一种产品,但我无法让它适用于多种产品。

如何定位一系列产品 ID?

【问题讨论】:

    标签: php arrays wordpress woocommerce product


    【解决方案1】:

    对于产品 ID 数组,请使用in_array() 条件函数,如下所示:

    function change_special_product_price( $price_html, $product ) {
        $product_ids = array(6917, 6918, 6921);
    
        if ( in_array($product->get_id(), $product_ids ) ) {
            $price_html = '<span class="woocommerce-Price-amount amount">' . __("Contact for Pricing") . '</span>';
        } 
        return $price_html;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2015-12-31
      • 2023-03-31
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      相关资源
      最近更新 更多