Woocommerce条纹费未正确添加到条纹支付

时间:2020-07-13 19:46:23

标签: php woocommerce stripe-payments

我们有文章撰写服务,客户可以按字数订购文章。如果订购的总字数超过5000个(无论有多少篇文章),我们将以降低的比率收费。经过调整后的价格是经过计算的,当我在各个“产品”页面,“购物车”页面,“结帐”页面以及整个后端中都可以正常显示。

但是,当我在Woocommerce>“订单”>“编辑订单”下转到Wordpress后端的特定区域时,看到的是“条纹费”没有从产品总价中正确扣除。

例如:您可以在下面的图像中看到-$ 3.76应该从订单总额$ 80中减去。条纹支出应为$ 76.24,但显示为$ 115.43。

enter image description here

以下是我用来更新价格的功能。我已经从在线资源中将其拼凑起来,并且看起来运行良好(尽管可能效率不高),但我不确定为什么会错误地计算Stripe Payout值。

add_action( 'woocommerce_before_calculate_totals', 'articles_alter_price_cart', 9999 );
 
function articles_alter_price_cart( $cart ) {

    $grandTotalWords = 0;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    /* Loop through cart items and get grand total of words */
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $qty = $cart_item['quantity'];
        $wordsPerArticle = $cart_item['variation']['attribute_number-of-words'];
        $numWords = $wordsPerArticle * $qty;
        $grandTotalWords += $numWords;
    }

    /* Update cart item prices based on grand total of words */
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {

        $qty = $cart_item['quantity'];
        $numWordsInput = $cart_item['variation']['attribute_number-of-words'];
        $arr = explode(' Words', $numWordsInput);
        $wordsPerArticle = $arr[0];

        $numWords = $wordsPerArticle * $qty;

        if ( $grandTotalWords != '' && $grandTotalWords > 0 ) {
            if ( $grandTotalWords > 10000 ) {
                $totalCost = $numWords * 0.125;
            } elseif ( $grandTotalWords >= 5000 && $grandTotalWords <= 10000 ) {
                $totalCost = $numWords * 0.15;
            } else {
                $totalCost = $numWords * 0.2;
            }
        }

        $singleCost = $totalCost / $qty;
        /* Tried setting type as string because totals are saved as strings apparently  */
        settype($singleCost, 'string');

        $cart_item['data']->set_price( $singleCost );
    }
 
}

任何帮助都将不胜感激。谢谢!

0 个答案:

没有答案