在WooCommerce中为付款方式添加乘法折扣

时间:2018-06-12 21:00:34

标签: woocommerce discount fee

以下代码段可让我获得付款方式的折扣:



add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 1, 1 );
function shipping_method_discount( $cart_object ) {

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

	// HERE Define your targeted shipping method ID
    $payment_method = 'cod';

    // The percent to apply
    $percent = 30; // 30%

    $cart_total = $cart_object->subtotal_ex_tax;
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    if( $payment_method == $chosen_payment_method ){
        $label_regular = __( "Total regular" );
        $label_sale = __( "Total" );
        $label_text = __( "30% de descuento" );
        // Calculation
        $regular = number_format(($cart_total * 100) / (100 - $percent), 1);
        $sale = number_format(($regular / 100) * (100 - $percent), 1);
        $discount = number_format(($regular / 100) * $percent, 1);
        // Add the discount
        $cart_object->add_fee( $label_regular, $regular, false );
        $cart_object->add_fee( $label_sale, -$sale, false );
        $cart_object->add_fee( $label_text, -$discount, false );
    }
}

add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
    // jQuery code
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}
&#13;
&#13;
&#13;

问题1:如何为更多方法添加折扣?例如,上面的代码是针对1种付款方式的折扣,但我也需要它用于其他方法。

问题2:如何隐藏输出?到目前为止,我的输出是常规总数,折扣和小计,但我想隐藏输出中的小计(计算仍然需要)。

这个想法是从正常价格而不是从销售价格中获得折扣。 提前谢谢。

0 个答案:

没有答案