根据自定义字段和数量阈值更改 WooCommerce 购物车商品价格

当购物车商品数量达到特定阈值时,我试图通过定义为产品自定义字段(产品自定义元数据)的批量价格从产品变体更改购物车商品价格。

我的工作来自: WooCommerce:从产品变体中获取自定义字段并将其显示在“附加信息区域” 和WooCommerce:无需插件的批量动态定价

这就是我所拥有的:

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );


function bbloomer_quantity_based_pricing( $cart, $variation_data ) {


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


    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;


    //get

    $bulk_price = get_post_meta( $variation_data[ 'variation_id' ], 'bulk_price', true);


    if ( $bulk_price ) {

        $threshold1 = 6; // Change price if items > 6


        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {

            if ( $cart_item['quantity'] >= $threshold1 ) {

                $price = $bulk_price;

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

            }

        }  

    }

}

但它不起作用,因为我无法获得批量价格的自定义字段值。



宝慕林4294392
浏览 51回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP