繁体   English   中英

购物商品折扣基于Woocommerce 3中的数量

[英]Cart item discount based on quantity in Woocommerce 3

我的WP网站销售定制的T恤。 自定义插件使每个定制衬衫成为woocommerce购物车中的一个项目。 如果订购了一件设计的2件衬衫(该订单项的数量为2件),我想打折。 但如果每行只有1个项目,我不想打折。

我找到了这个解决方案

根据商品数量有条件地按购物车商品添加折扣

但是这似乎改变了数据库中的产品价格,所以在折扣开始之后说2个蓝色衬衫,因为在1行上订购了2个,如果我在另一条线上添加第3件衬衫它也会得到折扣,我不想要。

由于woocommerce版本3+ 链接的答案代码不起作用 它需要不同的东西,甚至可以以更好的方式完成

该代码将根据购物车商品数量应用购物车商品折扣。 在此代码示例中,当数量等于或大于2(两)时,它将对购物车项目应用5%的折扣。

显示的购物车商品单价始终是实际商品价格。 折扣将生效并显示在购物车项目小计上。

此外,产品名称将附加折扣提及。

编码:

add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- YOUR SETTING ----- ##
    $discount_percentage = 5; // Discount (5%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    // Only for non on sale products
    if( ! $product->is_on_sale() ){
        $price = (float) $product->get_price();

        // Set the Product default base price as custom cart item data
        $cart_item_data['base_price'] = $price;

        // Set the Product discounted price as custom cart item data
        $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

        // Set the percentage as custom cart item data
        $cart_item_data['percentage'] = $discount_percentage;
    }

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function 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'] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_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>';
    }
    return $product_name;
}

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

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

    ## ----- YOUR SETTING ----- ##
    $targeted_qty = 2; // Targeted quantity

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // For item quantity of 2 or more
        if( $cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

            // Set cart item discounted price
            $cart_item['data']->set_price($cart_item['new_price']);
        }
    }
}

代码位于活动子主题(或活动主题)的functions.php文件中。 经过测试和工作。

在此输入图像描述


要显示折扣产品价格而不是原始产品价格,只需删除woocommerce_cart_item_price()函数(和挂钩) ...

最新类似: Woocommerce 3中的购物车商品数量累进百分比折扣

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM