WooCommerce存款和购物车总计算

问题描述 投票:1回答:1

我想要达到的目的是根据客户添加到购物车中的商品向客户收取押金。只有特定物品需要押金。对于这些项目中的每一项,都会添加1x存款。例如:Item1,Item2和Item3都需要存款10.00,而Item4,Item5和Item6不需要存款。所以,如果我在购物车中有以下内容

Product       Qty     Unit Price     Subtotal
Item1          x1     £50.00           £50.00
Item2          x2     £30.00           £60.00
Item4          x1     £5.00             £5.00
Item6          x1     £2.99             £2.99
---------------------------------------------
                              Total:  £117.99
                              Deposit: £30.00
                              Balance: £87.99

我需要收取30英镑的押金,因为有3个存款项目。然后,这留下了客户在收集物品时支付的剩余余额。

我尝试了各种插件(WooCommerce Deposits,Yith Deposits / Part Payments),但没有一个能够实现我的需求。

我已经把Hooks添加到我的孩子主题的functions.php文件中了,但我不知道如何让它完全正常运行。

现行守则

add_action( 'woocommerce_cart_calculate_fees', 'tcg_calculate_deposit' );
function tcg_calculate_deposit( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Check if these products are in the cart
    $deposit_items = array( '4359', '4336', '4331', '4320' );

    $deposit = 10.00;

    $matching = false;

    foreach ( $cart_object->get_cart() as $item_values ) {

        if( in_array( $item_values['product_id'], $deposit_items )) {

         // for each item in array deposit + 1
         // so if 3 deposit items deposit = 30.00

         // set cart total to be the deposit amount so I can process this amount as payment through woocommerce
        $cart_subtotal = $cart_object->subtotal_ex_tax;
        $balance = $cart_subtotal - $deposit;

        $cart_object->add_fee( __( 'Balance', 'woocommerce'), $balance, true );

        break;
        }

    }

}

在这一刻,这使总计为小计+余额的总和。我需要它存款。我不确定是否需要创建单独的函数来实现?

php wordpress woocommerce
1个回答
1
投票

这是一种方法:

首先:将存款价格的自定义字段添加到产品常规选项卡。

    function add_custom_fields() {

        global $woocommerce, $post;

        woocommerce_wp_text_input( 
            array( 
                'id'          => '_deposit_price', 
                'label'       => __( 'Deposit price(€)', 'woocommerce' ), 
                'placeholder' => '10.20',
                'desc_tip'    => 'true',
                'description' => __( 'Enter the custom deposit price here.', 'woocommerce' ) 
            )
        );

    }

    add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fields');

其次:将自定义字段的值作为元数据保存到产品中。

    function custom_fields_save( $post_id ){

        $woocommerce_text_field = sanitize_text_field( $_POST['_deposit_price'] );

        if( is_numeric( $woocommerce_text_field ) || empty( $woocommerce_text_field ) ){
            update_post_meta( $post_id, '_deposit_price', esc_attr( $woocommerce_text_field ) );
        }

    }

    add_action( 'woocommerce_process_product_meta', 'custom_fields_save' );

第三:过滤woocommerce_get_price,如果产品设定了存款价格,我们将返回而不是正常的产品价格。如果没有设定存款价格,我们返回0。

    function filter_woocommerce_get_price( $price, $product ){

        $product_id = $product->get_id();
        $deposit_price = get_post_meta( $product_id, '_deposit_price', true );

        if( ! empty( $deposit_price )  ) {

            return $deposit_price;
        }

        return 0; 

    }

    add_filter( 'woocommerce_get_price', 'filter_woocommerce_get_price', 10, 2 );

最后:过滤多个值,以便在产品页面和结帐页面中将价格设置为此€34.50 Deposit: €10.00

    function filter_woocommerce_get_price_html( $price, $product ){

        $product_id = $product->get_id();
        $deposit_price = get_post_meta( $product_id, '_deposit_price', true );
        $product_price = get_post_meta( $product_id, '_price', true );

        if( ! empty( $deposit_price )  ) {

            return wc_price($product_price) . ' <i>Deposit: ' . wc_price($deposit_price) . '</i>';
        }

        return wc_price( $product_price ); 

    }

    add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
    add_filter( 'woocommerce_cart_product_price', 'filter_woocommerce_get_price_html', 10, 2 );



    function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity ){

        $product_id = $product->get_id();
        $deposit_price = get_post_meta( $product_id, '_deposit_price', true );
        $product_price = get_post_meta( $product_id, '_price', true );

        if( ! empty( $deposit_price )  ) {

            return wc_price( $product_price * $quantity ) . ' <i>Deposit: ' . wc_price( $deposit_price * $quantity ) . '</i>';
        }

        return wc_price( $product_price * $quantity ); 

    }

    add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 3 );

现在,您只需将剩余余额作为元数据添加到订单中,或者将其作为费用添加到购物车。

附:请记住在产品常规选项卡中设置存款价格。

© www.soinside.com 2019 - 2024. All rights reserved.