在WooCommerce中对延期付款的购物车项目进行存款。

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

我从另一个帖子中提取了这段代码,基本上这段代码是想用折扣来强制购物车价格。

我想做的是,只有在产品处于退订状态时才强制打折。因此,如果产品处于延期订单状态,客户可以订购这个商品,从而在购物车中计算出定金。

来自 按购物车总金额的百分比存款 第2个代码片段回答,我曾试过对代码进行修改,以获得购物车中后订商品的特定折扣。

原有的代码照样可以使用,但如何让它只适用于后订商品呢?

我试了几天了,比如用 $product->is_on_backorder( 1 ) 但我不能让它工作。如何获得购物车中的后订商品总金额?

我知道这是个简单的解决方案,但我试了好几种解决方案,都无法成功。

php wordpress woocommerce cart hook-woocommerce
1个回答
1
投票

更新了。 要做到这一点,只适用于延期订购的商品,你将使用以下代码。

add_action( 'woocommerce_cart_calculate_fees', 'calculated_deposit_discount_on_backorders' );
function calculated_deposit_discount_on_backorders( $cart ) {

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

    ## Set HERE your negative percentage (to remove an amount from cart total)
    $percent = -.80; // 80% off (negative)

    $backordered_amount = 0;

    foreach( $cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
            $backordered_amount = $cart_item['line_total'] + $cart_item['line_tax'];
        }
    }

    ## ## CALCULATION ## ##
    $calculated_amount = $backordered_amount * $percent;

    // Adding a negative fee to cart amount (Including taxes)
    $cart->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );

}

代码在function.php文件中的活动子主题(或活动主题)。它应该工作。

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