Woocommerce 3中基于数量的购物车项目折扣

时间:2018-08-24 19:14:45

标签: php wordpress woocommerce cart discount

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

我找到了解决方案

Adding a discount by cart items conditionally based on the item quantity

但是这似乎改变了分贝中的产品价格,所以在打折说2件蓝色衬衫之后,因为在1行上订购了2件蓝色衬衫,如果我在单独的一行上添加第3件衬衫,它也会获得折扣,我不想。

1 个答案:

答案 0 :(得分: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文件中。经过测试,可以正常工作。

enter image description here


  

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

最新类似内容: Cart item quantity progressive percentage discount in Woocommerce 3