设置反映在 Woocommerce 3 中的订单上的折扣购物车商品价格

尤里·雷弗洛

我在我的functions.php 中使用此代码对我的产品应用 10% 的折扣,从购物车中的第二个开始:

function add_discount_price_percent( $cart_object ) {

    global $woocommerce;

    $pdtcnt=0;

    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        $pdtcnt++;

        $oldprice = 0;
        $newprice = 0;


        if($pdtcnt>1) { // from second product

            $oldprice = $cart_item['data']->price; //original product price      

            // echo "$oldprice<br />";

            $newprice = $oldprice*0.9; //discounted price
            $cart_item['data']->set_sale_price($newprice);
            $cart_item['data']->set_price($newprice);
            $cart_item['data']->set_regular_price($oldprice);

        }     
    }

    WC()->cart->calculate_totals();

}


add_action( 'woocommerce_before_cart', 'add_discount_price_percent', 1);

add_action( 'woocommerce_before_checkout_form', 'add_discount_price_percent', 99 );

价格在购物车和结帐页面中都正确显示,但是当我使用 PayPal 沙盒测试我的付款时,我看到并且必须支付全价,因为折扣被忽略。

如果我在提交按钮之前回显折扣价格,我会得到正确的价格:

function echo_discount_before_checkout_submit() {

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

    foreach ( WC()->cart->get_cart() as $key => $value ) {
        echo $value['data']->price . "<br />";
    }

}
add_action( 'woocommerce_review_order_before_submit', 'echo_discount_before_checkout_submit', 99 );

我如何向 PayPal 发送正确的折扣价?

编辑:@LoisTheAtzec 的回复非常好,但如果数量超过 2,即使是第一个产品,我也需要获得 10% 的折扣:我正在尝试此代码,但无法获得正确的值。

// If it is the first product and quantity is over 1
if ($count === 1 && $cart_item['quantity'] >= 2) {

        // get unit price
        $unit_price = $cart_item['data']->get_price();

        // get quantity to discount (total - 1)
        $discounted_quantity = $cart_item['quantity'] - 1;

        // get total discount amount (on total quantity - 1) 
        $discounted_amount = ($unit_price * $discounted_quantity) * 0.9;

        // add first non discounted price to total discount amount
        $total_discounted_price = $unit_price + $discounted_amount;

        // distribute discount over total quantity and get new unit price 
        $distributed_unit_discount = $total_discounted_price / $cart_item['quantity'];

        // set new unit price
        $cart_item['data']->set_price($distributed_unit_discount);
    }

更新 09-06-2018

我对登录用户有一个奇怪的行为,可能取决于插件之间的某些冲突或我使用的主题(Avada):折扣应用了两次,所以我不得不防止将此代码添加到我的函数中:

// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 999, 1);  

function add_discount_percentage_on_2nd_item($cart) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;

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

希望能帮助到你。

LoicTheAztec

在购物车对象中,您唯一可以真正改变并产生影响的是活动价格
更改购物车项目中的常规价格或销售价格无效。

尝试以下操作,更改第 2 个购物车及以后的价格,数据将正确传递到 Paypal:

// Calculate and save as custom cart item data the discounted price
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 3);

function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
    // HERE set the percentage rate to be applied to get the new price
    $percentage = 10; // 10%

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

    $product = wc_get_product($_product_id); // The WC_Product Object
    $base_price = (float) $product->get_price(); // Get the product active price

    // Save the calculated discounted price as custom cart item data
    $cart_item_data['discounted_price'] = $base_price * ( 100 - $percentage ) / 100;

    return $cart_item_data;
}

// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    $count = 0;

    // Loop through cart items
    foreach($cart->get_cart() as $cart_item) {
        $count++; // Increasing

        // On 2nd cart item or more set the calculated discounted price
        if ($count >= 2 && isset($cart_item['discounted_price']))
            $cart_item['data']->set_price($cart_item['discounted_price']);
    }
}

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


添加 - 如果购物车内容数超过 2,则所有商品均可享受折扣。

您将使用与上面相同的第一个挂钩函数代码。
您将用以下内容替换第二个挂钩函数:

// Set a discounted price on cart items when cart content count is over 2
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);
function add_discount_percentage_on_2nd_item($cart) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Get the total items count
    $total_count = $cart->get_cart_contents_count();

    // if total count is below 2 we exit
    if( $total_count < 2 ) 
        return; // Exit

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

        // Set the calculated discounted price
        if (isset($cart_item['discounted_price']))
            $cart_item['data']->set_price($cart_item['discounted_price']);
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中。它应该有效。


附加2

  • 只有第一个购物车项目是全价,下一个数量的第一个项目打折)
  • 所有纽特购物车商品都打折。

编码:

// Calculate and save as custom cart item data the discounted price
add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 3);

function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
    // HERE set the percentage rate to be applied to get the new price
    $percentage = 10; // 10%

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

    $product = wc_get_product($_product_id); // The WC_Product Object
    $base_price = (float) $product->get_price(); // Get the product active price

    // Save the normal active product price as custom cart item data
    $cart_item_data['normal_price'] = $base_price;

    // Save the calculated discounted price as custom cart item data
    $cart_item_data['discounted_price'] = $base_price * ( 100 - $percentage ) / 100;

    return $cart_item_data;
}

// Set the discounted price on 2nd item and
add_action('woocommerce_before_calculate_totals', 'add_discount_percentage_on_2nd_item', 20, 1);

function add_discount_percentage_on_2nd_item($cart) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;

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

    // Initializing variables
    $count = 0;
    $first_item = true

    // Loop through cart items
    foreach($cart->get_cart() as $cart_item) {
        // 1. First cart item
        if ( isset($cart_item['discounted_price']) && isset($cart_item['normal_price']) && $first_item ){
            if( $cart_item['quantity'] > 1 ){
                $normal_price   = (float) $cart_item['normal_price'];
                $discount_price = (float) $cart_item['discounted_price'];
                $quantity       = (int) $cart_item['quantity'];

                // The first item is at full price and others at discounted price
                $cart_item['data']->set_price( $normal_price + ( $discount_price * ($quantity - 1) ) );
            }
            $first_item = false; // We switch it to false as it is the first cart item
        }
        // 2. All next items (at discounted price
        elseif ( isset($cart_item['discounted_price']) && ! $first_item ){
            // Set the discounted price
            $cart_item['data']->set_price($cart_item['discounted_price']);
        }
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中。它应该有效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据 Woocommerce 中的购物车商品价格有条件地设置不同的税率

来自分类Dev

从 WooCommerce 中的自定义字段值动态更改购物车商品价格

来自分类Dev

将Woocommerce购物车商品自定义数据另存为订单商品元数据,以在订单和电子邮件上显示

来自分类Dev

避免选择的变化计算价格反映在Woocommerce相关产品中

来自分类Dev

在Woocommerce购物车商品名称和订单商品名称中添加换行符

来自分类Dev

如何在woocommerce的购物车页面中同时显示总订单销售价格和常规价格(或总折扣价格)

来自分类Dev

根据WooCommerce中的购物车小计,将特定的应税运费价格成本设置为0

来自分类Dev

基于商品数量计数的WooCommerce购物车中的折扣

来自分类Dev

折扣购物车项目价格,不包括WooCommerce中的类别及其子项

来自分类Dev

ejabberd-在ejabberd上设置昵称不会反映在Pidgin / Empathy上,除非我注销

来自分类Dev

在ERPNext GUI中未反映在Frappe应用程序中设置值

来自分类Dev

在另一个表格中设置表格宽度不会反映在创建的pdf中

来自分类Dev

通过Mininet python API设置的带宽不会反映在Opendaylight中

来自分类Dev

通过Mininet python API设置的带宽不会反映在Opendaylight中

来自分类Dev

APi 网关:提供给方法的 SDK 设置名称未反映在 SDK 生成中

来自分类Dev

为什么我的布局更改没有反映在 Sinatra/Puma/Nginx 生产设置中?

来自分类Dev

Paraview:来自 python 脚本的一些管道设置未反映在 UI 中

来自分类Dev

即使在成功构建后,更改也不会反映在 Ionic 3 应用程序中

来自分类Dev

根据 WooCommerce 中的订单数量设置折扣

来自分类Dev

允许客户设置产品价格并通过WooCommerce中的某些验证将其添加到购物车

来自分类Dev

WooCommerce-在产品页面之外列出购物车商品名称

来自分类Dev

获取购物车商品名称,数量所有详细信息woocommerce

来自分类Dev

如何在WooCommerce结帐页面上显示购物车商品加价?

来自分类Dev

获取WooCommerce购物车商品计数(产品类别除外)

来自分类Dev

Woocommerce 中成本较低的产品的购物车折扣

来自分类Dev

Woocommerce设置类别最低购物车

来自分类Dev

$ rootScope更改不会反映在模板上

来自分类Dev

当在Wordpress中使用Woocommerce时,应用于购物车的购物车折扣未在“订单总额”中显示正确的金额

来自分类Dev

反映在matlab中的随机游走?

Related 相关文章

  1. 1

    根据 Woocommerce 中的购物车商品价格有条件地设置不同的税率

  2. 2

    从 WooCommerce 中的自定义字段值动态更改购物车商品价格

  3. 3

    将Woocommerce购物车商品自定义数据另存为订单商品元数据,以在订单和电子邮件上显示

  4. 4

    避免选择的变化计算价格反映在Woocommerce相关产品中

  5. 5

    在Woocommerce购物车商品名称和订单商品名称中添加换行符

  6. 6

    如何在woocommerce的购物车页面中同时显示总订单销售价格和常规价格(或总折扣价格)

  7. 7

    根据WooCommerce中的购物车小计,将特定的应税运费价格成本设置为0

  8. 8

    基于商品数量计数的WooCommerce购物车中的折扣

  9. 9

    折扣购物车项目价格,不包括WooCommerce中的类别及其子项

  10. 10

    ejabberd-在ejabberd上设置昵称不会反映在Pidgin / Empathy上,除非我注销

  11. 11

    在ERPNext GUI中未反映在Frappe应用程序中设置值

  12. 12

    在另一个表格中设置表格宽度不会反映在创建的pdf中

  13. 13

    通过Mininet python API设置的带宽不会反映在Opendaylight中

  14. 14

    通过Mininet python API设置的带宽不会反映在Opendaylight中

  15. 15

    APi 网关:提供给方法的 SDK 设置名称未反映在 SDK 生成中

  16. 16

    为什么我的布局更改没有反映在 Sinatra/Puma/Nginx 生产设置中?

  17. 17

    Paraview:来自 python 脚本的一些管道设置未反映在 UI 中

  18. 18

    即使在成功构建后,更改也不会反映在 Ionic 3 应用程序中

  19. 19

    根据 WooCommerce 中的订单数量设置折扣

  20. 20

    允许客户设置产品价格并通过WooCommerce中的某些验证将其添加到购物车

  21. 21

    WooCommerce-在产品页面之外列出购物车商品名称

  22. 22

    获取购物车商品名称,数量所有详细信息woocommerce

  23. 23

    如何在WooCommerce结帐页面上显示购物车商品加价?

  24. 24

    获取WooCommerce购物车商品计数(产品类别除外)

  25. 25

    Woocommerce 中成本较低的产品的购物车折扣

  26. 26

    Woocommerce设置类别最低购物车

  27. 27

    $ rootScope更改不会反映在模板上

  28. 28

    当在Wordpress中使用Woocommerce时,应用于购物车的购物车折扣未在“订单总额”中显示正确的金额

  29. 29

    反映在matlab中的随机游走?

热门标签

归档