18910140161

PHP-仅当WooCommerce单一产品页面上的价格超过10$时才显示-堆栈溢出

顺晟科技

2022-10-18 12:57:27

157

我使用这个代码来向客户显示,如果客户想要支付几个月的分期付款,那么几个月的费用是多少。

但该代码应该只适用于10美元以上的项目,而对10美元以下的项目无效。

有人有什么建议吗?

add_action( 'woocommerce_single_product_summary', 'show_emi', 1 );
function show_emi() {
       global $product; 
       $id = $product->get_id();
        $product = wc_get_product( $id );
        $a=$product->get_price();
        $b  = 20;
        $c = 100;
        $d = $a / $b;
        $total = $a / $b;
      echo '<div class="afbetaling">
    <b>  Eller kun '.$total.' kr./md i 20 mdr. </b>
0% i rente og gebyrer. <br> Få mere info i butikken</div>';
}

顺晟科技:

您的代码包含一些小错误或使用了一些优化

  • 使用global $product;获取该对象的产品ID,然后使用该产品ID获取产品对象。这是一个不必要的步骤,因为您已经可以访问$product
  • 使用明确的变量来说明$a$b$c
  • 使用__( string $text, string $domain = 'default' )函数可以使文本可翻译
  • 为了满足您的需求,您必须将产品价格置于IF条件中,并且只有在满足
  • 此条件时才能继续

所以你得到:

function action_woocommerce_single_product_summary() {
    // Get the global product object
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get price
        $price = $product->get_price();
        
        // Above 10
        if ( $price > 10 ) {
            // Settings
            $months = 20;
            $total = $price / $months;
            
            // Message
            $message = sprintf( __( '<strong>Or only DKK %s / month for %s months</strong>, 0%% in interest and fees. Get more info in the store', 'woocommerce' ), $total, $months );
            
            // Output
            echo '<div class="afbetaling">' . $message . '</div>';
        }
    }
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 10, 0 );
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航