根据数量阈值逐步折扣 Woocommerce 产品价格

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

可以:

  1. 将特定类别的产品步骤数量设置为 10 的倍数,从至少 50 个开始[已解决]
  2. 仅在特定产品数量达到 100 和 200 时更改价格?例如:
    1. 50 至 90 个(数量)每件产品收费 1 美元;
    2. 每件产品从 100 到 190(数量)收费 0.7$;
    3. 每件产品收费 0.5 美元,最多 200 个(数量)。

编辑:

我用这些答案解决了第一部分:

php wordpress woocommerce price product-quantity
1个回答
0
投票

特殊情况下,我回答您的问题是因为我们希望您在问题中提供真实的代码尝试。还有下次,当时就问一个问题,这是 Stack OverFlow 上的规则。

所以,下面的代码将回答你的第二个问题。对于特定产品,它将根据数量阈值更改产品价格。

在第一个函数中,您将在数组中定义目标产品 ID。

在第二个功能中,您将设置数量阈值和价格。

此代码适用于简单产品(不适用于可变产品/产品变体)

// Conditional function - Settings: Define your product IDS HERE
function is_product_threshold_qty_pricing_enabled( $product ){
    // Here set in the array your product IDS
    $targeted_product_ids = array(15, 16, 17, 18, 24);

    return in_array($product->get_id(), $targeted_product_ids);
}

// Utility function - Settings: Define your threshold quantity and price rates (for pricing)
function get_quantity_threshold_price_rate( $quantity ) {
    if ( $quantity < 100 ) {
        $rate = 1; // No discount (less than 100)
    } elseif ( $quantity < 200 ) {
        $rate = 0.7; // - 30% discount (between 100 and 199 )
    } else {
        $rate = 0.5; // - 50% discount (up to 200)
    }
    return $rate;
}

// Utility function: Get threshold quantity pricing for display
function get_displayed_qty_threshold_pricing( $product, $quantity ) {
    $price_rate = get_quantity_threshold_price_rate( $quantity );
    if ( $price_rate < 1 ) {
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $active_price  = wc_get_price_to_display( $product, array( 'price' => ($product->get_price() * $price_rate) ) );

        $price = wc_format_sale_price( $regular_price, $active_price ) . $product->get_price_suffix();
    } else {
        $price = $product->get_price_html(); // No discount
    }
    error_log('[ '.$price_rate.' ] quantity: '.$quantity . ' | price: ' . wp_strip_all_tags($price));
    return $price;
}

// PHP Admin Ajax receiver: Return formatted prices
add_action('wp_ajax_qty_threshold_pricing', 'get_qty_threshold_pricing');
add_action('wp_ajax_nopriv_qty_threshold_pricing', 'get_qty_threshold_pricing');
function get_qty_threshold_pricing() {
    if (isset($_POST['product_id']) && isset($_POST['quantity'])) {
        $product = wc_get_product(intval($_POST['product_id'])); // GET THE PRODUCT OBJECT

        echo get_displayed_qty_threshold_pricing( $product, intval($_POST['quantity']) );
    }
    wp_die(); // Exit silently (to avoid an Error 500)
}

// Jquery Ajax: Display on single product pages the price based on quantity
add_action('wp_footer', 'display_single_product_threshold_pricing', 10);
function display_single_product_threshold_pricing() {
    global $product;

    if( is_product() && is_product_threshold_qty_pricing_enabled( $product ) ) :
    ?>
    <script>
    jQuery(function($){
        const blockWhite = {message: null, overlayCSS: {background: '#fff', opacity: 0.6}};
        $('form.cart').on('change input', '[name=quantity]', function() {
            $('form.cart').block(blockWhite);
            $.ajax({
                url:  '<?php echo admin_url( 'admin-ajax.php' ); ?>',
                type: 'POST',
                data: {
                    'action':     'qty_threshold_pricing',
                    'product_id': '<?php echo $product->get_id(); ?>',
                    'quantity':   $(this).val()
                },
                success: function(response) {
                    $('form.cart').unblock();
                    $('.price').html(response);
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

// Display cart item updated price in Cart and Mini Cart
add_action('woocommerce_cart_item_price', 'filter_minicart_displayed_price', 10, 2);
function filter_minicart_displayed_price($price, $cart_item) {
    $product = wc_get_product( $cart_item['data']->get_id() );
    if ( is_product_threshold_qty_pricing_enabled( $product ) ) {
        $price = get_displayed_qty_threshold_pricing( $product, $cart_item['quantity'] );
    }
    return $price;
}

add_action('woocommerce_before_calculate_totals', 'set_cart_item_updated_price');
function set_cart_item_updated_price($cart) {
    if ((is_admin() && !defined('DOING_AJAX')))
        return;

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

    // Loop through cart items and set the updated price
    foreach ($cart->get_cart() as $cart_item) {
        if ( is_product_threshold_qty_pricing_enabled( $cart_item['data'] ) ) {
            $price_rate = get_quantity_threshold_price_rate( $cart_item['quantity'] );
            if ( $price_rate < 1 ) {
                $product = wc_get_product($cart_item['data']->get_id());

                $cart_item['data']->set_price( $product->get_price() * $price_rate );
                $cart_item['data']->set_sale_price( $product->get_sale_price() * $price_rate );
            }

        }
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。


补充:

要定位 产品类别 (或产品标签) 而不是产品 ID,请将第一个函数替换为:

function is_product_threshold_qty_pricing_enabled( $product ){
    // Here set in the array the term slugs
    $term_slugs = array('hoodies', 't-shirts');
    $taxonomy   = 'product_cat'; // Product category taxonomy

    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    return has_term( $term_slugs, $taxonomy, $product_id );
}

对于产品标签,请将

'product_cat'
替换为
'product_tag'

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