简体   繁体   English

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

[英]Change WooCommerce cart item price based on a custom field and quantity threshold

I'm trying to change the cart item price from a product variations by a bulk price defined as a product custom field (product custom meta data) , when the cart item quantity reaches a specific threshold.当购物车项目数量达到特定阈值时,我正在尝试通过定义为产品自定义字段(产品自定义元数据)的批量价格从产品变体中更改购物车项目价格。

I'm Working from: WooCommerce: Get custom field from product variations and display it on the “additional information area” And WooCommerce: Bulk Dynamic Pricing Without a Plugin我的工作来自: WooCommerce:从产品变体中获取自定义字段并将其显示在“附加信息区域”WooCommerce:没有插件的批量动态定价

This is what I have:这就是我所拥有的:

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

But it doesn't work, as I can't get the custom field value for the bulk price.但它不起作用,因为我无法获得批量价格的自定义字段值。

In your code $variation_data['variation_id'] is not defined as $variation_data doesn't exist for woocommerce_before_calculate_totals hook… Try the following instead:在您的代码中$variation_data['variation_id']未定义为$variation_data不存在woocommerce_before_calculate_totals钩子......请尝试以下操作:

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

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

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

    // Define the quantity threshold
    $qty_threshold = 6;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Get the bulk price from product (variation) custom field
        $bulk_price = (float) $cart_item['data']->get_meta('bulk_price');

        // Check if  item quantity has reached the defined threshold
        if( $cart_item['quantity'] >= $qty_threshold && $bulk_price > 0 ) {
            // Set the bulk price
            $cart_item['data']->set_price( $bulk_price );
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 It should works.它应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 购物车商品价格计算,基于Woocommerce中选择的“天”自定义字段 - Cart item price calculation, based on chosen “days” custom field in Woocommerce 通过自定义字段更改购物车中的产品价格,Woocommerce - Change product price in Cart by custom field, Woocommerce 还可以根据产品自定义字段更改 WooCommerce Minicart 商品价格 - Also change WooCommerce Minicart item price based on product custom field 将定制产品价格乘以 WooCommerce 购物车中的数量 - Multiply custom product price by the quantity in WooCommerce cart 基于Woocommerce中购物车项目库存数量的自定义通知 - Custom notice based on cart item stock quantity in Woocommerce 根据 Woocommerce 中的购物车商品数量更改适用税率 - Change applied taxes rate based on cart item quantity in Woocommerce 基于Woocommerce中的维度自定义字段的自定义购物车商品价格计算 - Custom cart item price calculation based on dimentions custom fields in Woocommerce 在Woocommerce中根据购物车数量添加自定义结帐字段 - Add a custom checkout field based on cart items quantity in Woocommerce 在Woocommerce中将购物车项目的价格替换为自定义字段值 - Replace the price of the cart item with a custom field value in Woocommerce 使用产品自定义字段值覆盖 woocommerce 购物车项目价格 - Override woocommerce cart item price with product custom field value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM