【问题标题】:How to manipulate Woocommerce product price on front end?如何在前端操纵 Woocommerce 产品价格?
【发布时间】:2020-07-02 09:34:22
【问题描述】:

伙计们,你们好吗?

我正在从 Shopify 迁移到 Woocommerce,在 Shopify 上我有一个自定义代码,显示产品价格除以 12 倍,这是我们接受的最大分期付款金额。

作为参考,我使用的代码是这样的:{{current_variant.price |times:1.1979| divided_by: 12| money }}

在这段代码中,我: 1) 获得了当前选择的变体的价格 ( current _variant.price); 2) 乘以分期付款税 (times:1.1979); 3) 并除以最多 12 个分期付款 (divided_by: 12)。

如何在 Woocommerce 中做同样的事情?我的意思是,我当然不能像在 Shopify 中那样使用 Liquid,所以我想我需要使用 PHP 或 JS,没关系。

而且我不能使用插件,因为稍后我有一个完整的表来计算每个分期付款(从 1x 到 12x)并向客户显示最终值,所以我有点需要通过这个更简单的例子来理解逻辑我粘贴了。

我试图执行的想法是price * interests / max. installments number。 PHP 真的不是我的强项,抱歉可能没有那么自信。

非常感谢!

【问题讨论】:

  • 这里不是很清楚你想做什么。当产品放入购物车时,您是否真的需要设置/操作 WC 用于产品的价格?还是您只需要向用户显示 一些东西,而不影响任何实际的购物车/价格计算逻辑?这些“分期付款”什么?这是用户将订购的产品的实际数量,还是……什么?

标签: php jquery woocommerce shopify liquid


【解决方案1】:

我假设您希望在简单产品和可变产品的产品价格下方显示分期付款价格。

您可以使用 jQuery 在产品价格下方添加一个元素。要使用正确的数据填充此元素,您可以使用 PHP 函数,该函数将根据产品价格计算分期付款价格。 (在这种情况下称为get_installment_price

由于每次变体更改时您都必须重新计算可变产品的分期付款价格,因此您可以在每次隐藏输入 variation_id 更改时通过 AJAX 调用该 PHP 函数。 WooCommerce 使用此隐藏输入来存储当前选择的变体。

下面的代码应该可以解决问题:

add_action( 'woocommerce_before_single_product', 'show_installment_price_underneath_product_price' );
function show_installment_price_underneath_product_price() {

    global $product;
    //Get the installment price for simple products
    $installment_price = $product->is_type('variable') == false ? get_installment_price( $product ) : 0;

    ?>
    <script>
    jQuery(document).ready(function($) {  

        // Check if product is variable
        if( $('input.variation_id').length ) {

            // If so, calculate the installment price each time the variation changes
            $('input.variation_id').change( function(){
                if( '' != $('input.variation_id').val() ) {

                    jQuery.ajax( {

                        url: '<?php echo admin_url( 'admin-ajax.php'); ?>',
                        type: 'post',
                        data: { 
                            action: 'get_installment_price', 
                            variation_id: $('input.variation_id').val()
                        },
                        success: function(data) {
                            $('.woocommerce-variation-price').siblings('.installment-price').remove();
                            $('.woocommerce-variation-price').after('<div class="installment-price"><p>Price per installment: ' + data + '</p></div>');
                        }
                    });
                }
            });

        // Else use the installement price we've calculated for simple product
        } else {
            let installmentPrice = '<?php echo $installment_price; ?>';
            $('.summary p.price').after('<div class="installment-price"><p>Price per installment: ' + installmentPrice + '</p></div>');
        }
    });
    </script>
    <?php
}
    
add_action('wp_ajax_get_installment_price' , 'get_installment_price');
add_action('wp_ajax_nopriv_get_installment_price','get_installment_price');
function get_installment_price( $product = null ) {

    $variation_id = isset( $_POST['variation_id'] ) ? intval( $_POST['variation_id'] ) : null;
    $product = $variation_id ? wc_get_product( $variation_id ) : $product;

    //Make your installment calculations here
    $installment_price = $product->get_price() * 1.1979 / 12;

    $formatted_installment_price = wc_price( $installment_price, array( 'currency' => get_woocommerce_currency() ) );

    if ( $variation_id ) { // This is the output for the AJAX call (variable product)
        echo $formatted_installment_price;
        wp_die();
    } else { // This is the output for the simple product
        return $formatted_installment_price;
    }
}

这应该添加到您的子主题的functions.php 中或通过像代码片段这样的插件。

【讨论】:

  • 如果此答案对您有用,您能否将答案标记为已接受并可能投票?提前谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 2015-02-25
相关资源
最近更新 更多