WooCommerce存款和购物车总计计算

时间:2018-10-12 22:36:12

标签: php wordpress woocommerce

我要实现的目标是根据客户添加到购物车中的物品向客户收取定金。仅特定项目需要支付押金。对于这些项目中的每一项,都会添加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存款,Yith存款/部分付款),但没有一个插件可以满足我的需求。

我已经通过在我的子主题的functions.php文件中添加了Hooks来解决问题,但是我对如何使其完全发挥作用感到困惑。

当前代码

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;
        }

    }

}

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

1 个答案:

答案 0 :(得分: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 );


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

P.S。请记住在“产品”的“常规”标签中设置入金价格。

相关问题