简体   繁体   English

WooCommerce-更改购物车中的价格显示

[英]WooCommerce - Change price display in cart

First, thanks for your time. 首先,感谢您的宝贵时间。

We have certain products we offer a payment plan with, and it includes a $1 30day trial. 我们提供某些产品,我们提供付款计划,其中包括1美元的30天试用期。

When a user adds a product to the cart with the "payment plan" category ( id=41 below), I want the cart to display the price as $1 then X payments of $xx.xx . 当用户将产品添加到具有“付款计划”类别(下面的id=41 )的购物$xx.xx ,我希望购物车将价格显示为$ 1,然后显示$xx.xx X付款。 The back end only needs to pass the product SKU so this is purely for display reasons. 后端只需要通过产品SKU,因此这纯粹是出于显示原因。

This code works, but loops for however many items are in the cart. 该代码有效,但是购物车中有许多物品仍在循环。 Is there a way for me to stop the loop as soon as it senses the "payment plan" product category? 一旦我感觉到“付款计划”产品类别,有没有办法让我停止循环?

The Code: 编码:

        <td><?php 
                    function check_payment() {
                    //Check to see if user has product in cart
                    global $woocommerce;

                    //assigns a default negative value

                    $contains_special = false;

                    $wccart= WC()->cart->get_cart();

                    if ( !empty( $wccart ) ) {
                        foreach ( WC()->cart->get_cart() as $cart_item ) {

                            function is_item_special( $product_id ){
                                if( has_term( 'payment-plan', 'product_cat', $product_id ) ) {
                                    return TRUE;
                                } else {
                                    return false;
                                }
                            }//function

                            if ( is_item_special( $cart_item['product_id'] ) ) {

                                    $contains_special = true;
                                    $firstpayment = get_post_meta( $_product->id, 'firstpayment', true );
                                    $getsubtotal = WC()->cart->get_cart_subtotal(); //get cart subtotal
                                    $trimbefore = str_replace('<span class="amount">&#36;', "", $getsubtotal); //$getsubtotal returns <span class="amount>XX.XX</span> this trims off the opening span tag
                                    $intsubtotal = str_replace('</span>', "", $trimbefore); //trim closing span tag
                                    $formatsubtotal = number_format((float)$intsubtotal, 2, '.', '');//makes an integer

                                    $numberofpayments = get_post_meta( $_product->id, 'payments', true );

                                    $afterfirsttotal = $formatsubtotal - 1.00;
                                    $paymentamount = number_format((float)$afterfirsttotal, 2, '.', '') / $numberofpayments;


                                    echo '$' . $firstpayment . '<br/> then ' . $numberofpayments . ' payments of $' . number_format((float)$paymentamount, 2, '.', '');
                                    break;
                                } else {
                                wc_cart_totals_subtotal_html();
                                }//if/else statement
                        }//foreach loop
                    } //if cart isn't empty
                    } //function

?> ?>

I'm semi-new to PHP and still trying to understand some things, so sorry if this is just obvious! 我是PHP的新手,仍在尝试了解一些内容,如果很明显,对不起!

Here is something that I have used in the past (slightly modified from when I was checking for a product with a particular post meta field). 这是我过去使用过的东西(与我检查具有特定post meta字段的产品时稍作修改)。 From what I can tell from your question, it seems that you are looking for break which is how to quit a foreach loop once a particular condition has been satisfied. 从您的问题中可以看出,您似乎正在寻找break ,即一旦满足特定条件,如何退出foreach循环。

The first function loops through the cart items looking for any item that matches our criteria. 第一个功能遍历购物车中的物品,寻找与我们的条件匹配的任何物品。 Note the if ( ! empty( WC()->cart->get_cart() ) ) which prevents the code from running on an empty cart.... which was the cause of the foreach error you were seeing earlier. 请注意if ( ! empty( WC()->cart->get_cart() ) ) ,它会阻止代码在空购物车上运行。...这是您之前看到的foreach错误的原因。

The second function checks a product for a particular condition: in your case whether or not it has a specific product category assigned. 第二个功能检查产品的特定条件:在您的情况下,是否分配了特定的产品类别。 This could easily be handled inside the first function, but I refactored it out for readability. 这可以在第一个函数中轻松处理,但是出于可读性考虑,我对其进行了重构。

/*
 * loop through cart looking for specific item
 */
function kia_check_cart_for_specials() {

        $contains_special = false;

        if ( ! empty( WC()->cart->get_cart() ) ) {
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( kia_is_item_special( $cart_item['product_id'] ) ) {
                    $contains_special = true;
                    break; // found a special item so we can quit now
                }
            }
        }

        return $contains_special;
}

/*
 * check if an item has special category
 * change the term/condition to suit your needs
 */
function kia_is_item_special( $product_id ){
    if( has_term( 'special-category', 'product_cat', $product_id ) ) {
        return TRUE;
    } else {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM