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

时间:2015-10-21 13:25:58

标签: php wordpress woocommerce

首先,谢谢你的时间。

我们提供某些产品,我们提供付款计划,并包含1美元30天的试用期。

当用户将产品添加到具有“付款计划”类别(下面为id=41)的购物车时,我希望购物车将价格显示为1美元,然后是$xx.xx的X付款。后端只需要通过产品SKU,因此这纯粹是出于显示原因。

此代码有效,但许多项目的循环都在购物车中。有没有办法让我在感知到“付款计划”产品类别后立即停止循环?

守则:

        <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

&GT?;

我是PHP的半新手,仍在尝试理解一些事情,很抱歉,如果这很明显!

1 个答案:

答案 0 :(得分:0)

这是我过去使用过的东西(从我检查具有特定帖子元字段的产品时略有修改)。从我的问题可以看出,似乎你正在寻找break,这是在满足特定条件后如何退出foreach循环。

第一个功能循环浏览购物车项目,寻找符合我们条件的任何商品。请注意if ( ! empty( WC()->cart->get_cart() ) ),它会阻止代码在空的购物车上运行....这是您之前看到的foreach错误的原因。

第二个功能检查产品是否符合特定条件:在您的情况下是否分配了特定的产品类别。这可以很容易地在第一个函数内部处理,但我为了可读性而重构了它。

/*
 * 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;
    }
}
相关问题