如何在 WooCommerce 中根据数量提供折扣

已发表: 2020-01-27

通常,作为店主,如果客户购买超过一个数量的特定产品,您有时可能希望提供折扣。 这可能是为了促进销售或完成多余的库存。 在这篇文章中,您将学习如何使用代码片段和插件在 WooCommerce 中根据数量提供折扣。

如何使用代码片段在 WooCommerce 中根据数量提供折扣

让我们探索如何使用代码片段来执行此操作。 使用此代码段,如果客户购买超过 2 个数量的相同产品,我们将为他们提供 5% 的折扣。

为此,您需要将以下代码添加到子主题的 functions.php 文件中:

 add_filter('woocommerce_add_cart_item_data', 'ts_add_default_price_as_custom', 20, 3 );

功能 ts_add_default_price_as_custom( $cart_item_data, $product_id, $variation_id ){

    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- 在这里设置折扣 ----- ##
    $discount_percentage = 5; // 折扣 (5%)

    // WC_Product 对象
    $product = wc_get_product($product_id);

    $price = (float) $product->get_price();

    // 将产品默认基价设置为自定义购物车商品数据
    $cart_item_data['base_price'] = $price;

    // 将产品折扣价设置为自定义购物车商品数据
    $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

    // 将百分比设置为自定义购物车商品数据
    $cart_item_data['percentage'] = $discount_percentage;

    返回 $cart_item_data;
}

// 显示产品原价
add_filter('woocommerce_cart_item_price', 'ts_display_cart_items_default_price', 20, 3);

功能 ts_display_cart_items_default_price($product_price, $cart_item, $cart_item_key){
    if( isset($cart_item['base_price']) ) {
        $product = $cart_item['data'];
        $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    返回 $product_price;
}

// 显示带有折扣百分比的产品名称
add_filter('woocommerce_cart_item_name', 'ts_add_percentage_to_item_name', 20, 3);
函数 ts_add_percentage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    返回 $product_name;
}

add_action('woocommerce_before_calculate_totals', 'ts_custom_discounted_cart_item_price', 20, 1);
功能 ts_custom_discounted_cart_item_price($cart) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        返回;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        返回;

    ## ----- 在此处设置数量 ----- ##
    $targeted_qty = 2; // 目标数量

    // 遍历购物车项目
    foreach ( $cart->get_cart() as $cart_item ) {

        // 对于 2 个或更多的项目数量
        if($cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

            // 设置购物车商品折扣价
            $cart_item['data']->set_price($cart_item['new_price']);
        }
    }
}

如果产品的数量超过 2,上面的代码将添加 5% 的折扣。您可以通过更改$discount_percentage变量来设置所需的折扣和$targeted_qty来设置数量,从而满足您的要求。您希望应用折扣。

offer discounts based on quantity in WooCommerce - Updated Cart contents

如果您在购物车中添加 2 个或更多数量的相同产品,您现在将能够看到折扣价。

我们在上面的代码中使用了四个主要的钩子:

woocommerce_add_cart_item_data :此过滤器挂钩用于更改购物车的内容/元数据。
woocommerce_cart_item_price :此过滤器挂钩用于更改价格在购物车中的显示方式。
woocommerce_cart_item_name :此过滤器挂钩可用于更改购物车中的产品名称。 在我们的示例中,我们使用它将折扣百分比附加到产品名称中。
woocommerce_before_calculate_totals :此操作挂钩用于覆盖购物车中的产品价格。

如何仅针对非特价商品提供基于数量的折扣:

如果您想将其限制为仅那些不销售的产品,您只需编辑上面的代码片段以在添加到钩子woocommerce_add_cart_item_data 的函数内添加一个条件:

 add_filter('woocommerce_add_cart_item_data', 'ts_add_default_price_as_custom', 20, 3 );
功能 ts_add_default_price_as_custom( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- 在这里设置折扣 ----- ##
    $discount_percentage = 5; // 折扣 (5%)

    // WC_Product 对象
    $product = wc_get_product($product_id);

    // 在此处添加条件以仅针对非在售产品提供折扣
    如果(!$product->is_on_sale()){
        $price = (float) $product->get_price();

        // 将产品默认基价设置为自定义购物车商品数据
        $cart_item_data['base_price'] = $price;

        // 将产品折扣价设置为自定义购物车商品数据
        $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

        // 将百分比设置为自定义购物车商品数据
        $cart_item_data['percentage'] = $discount_percentage;
    }

    返回 $cart_item_data;
}

// 显示产品原价
add_filter('woocommerce_cart_item_price', 'ts_display_cart_items_default_price', 20, 3);
功能 ts_display_cart_items_default_price($product_price, $cart_item, $cart_item_key){
    if( isset($cart_item['base_price']) ) {
        $product = $cart_item['data'];
        $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    返回 $product_price;
}

// 显示带有折扣百分比的产品名称
add_filter('woocommerce_cart_item_name', 'ts_add_percentage_to_item_name', 20, 3);
函数 ts_add_percentage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    返回 $product_name;
}

add_action('woocommerce_before_calculate_totals', 'ts_custom_discounted_cart_item_price', 20, 1);
功能 ts_custom_discounted_cart_item_price($cart) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        返回;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        返回;

    ## ----- 在此处设置数量 ----- ##
    $targeted_qty = 2; // 目标数量

    // 遍历购物车项目
    foreach ( $cart->get_cart() as $cart_item ) {

        // 对于 2 个或更多的项目数量
        if($cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

            // 设置购物车商品折扣价
            $cart_item['data']->set_price($cart_item['new_price']);
        }
    }
} 

通过这种方式,您可以通过在添加到过滤钩woocommerce_add_cart_item_data的函数内声明适当的条件,使用代码片段为非销售、销售或所有产品(即全店范围)提供基于数量的折扣。

如何使用插件在 WooCommerce 中根据数量提供折扣

如果您不习惯在网站文件中添加代码片段,您也可以使用插件来执行此操作:

  1. WooCommerce 的折扣规则:这是一个免费插件,可让您不仅根据产品数量添加百分比折扣,还可以在您的客户超过特定价格限制时添加折扣。 您还可以在一定数量的项目(不一定是同一产品)上添加百分比折扣。 此插件的 PRO 版本允许您添加类别范围的折扣、BOGO(买一送一)优惠、固定价格折扣而不是百分比折扣等等。
  2. WooCommerce 的有条件折扣:这是另一个插件,在其免费版本中提供几乎所有重要功能 - 您可以根据数量、购物车商品数量、特定类别、特定产品、可变功能(蓝色衬衫、中号尺寸)添加折扣等),对于某些商店角色甚至特定客户。 它的 PRO 版本具有一些附加功能,其中之一是在购买特定产品时将产品免费添加到购物车中。
  3. 智能优惠券:除了礼品卡、商店积分、URL优惠券和高级限制外,您还可以根据购物车中特定产品的数量(例如10件衬衫)以及购物车中多个产品的总产品数量来限制优惠券购物车(例如——5 件衬衫 + 2 条裤子 + 3 条腰带)。

通过这种方式,您可以使用代码片段或插件根据产品数量向您的客户提供折扣。