自定义动态商品定价在 Woocommerce Checkout 上重置

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

我在保持价格更新时遇到问题。或多或少这是我用来更新价格的示例:

    add_action( 'woocommerce_before_calculate_totals', 'cst_product_quantity_discounter', 10 );
function cst_product_quantity_discounter( $cart_object ) {

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

    if( ! isset( WC()->session->reload_checkout ) ) {

        // declare woocommerce global
        global $woocommerce;      

        // array of ID's to match that represent each item

        $special_ids_array = [
            0 => array( "id" => 15372, "product" => "TAF", "single_bag_price" => 42.99, "3_multiplier" => 0.82181902768 ),
            1 => array( "id" => 14285, "product" => "POW", "single_bag_price" => 29.99, "3_multiplier" => 0.8890741358 )
        ];

        // gather cart object
        $wc_cart = WC()->cart->get_cart();

        foreach( $wc_cart as $cart_item ){

            // gather relevant data for testing and comparisons
            $product_id = $cart_item['product_id'];
            $product_key = $cart_item['key'];
            $pack_size_attribute = $cart_item['variation']['attribute_pa_sample-check']; // returns: "full-pack" || "sample" || null
            $item_quantity = $cart_item['quantity'];

            foreach ( $special_ids_array as $id_test_2 ) {
                if ( $product_id === $id_test_2["id"] && $pack_size_attribute !== "sample" ){
                    foreach ( $cart_object->cart_contents as $key => $value ) {
                        // if the key matches we will set the price of the correct product
                        if ( $key === $product_key ){
                            $discounted_price = 10;
                            $value['data']->set_price($discounted_price);
                        }
                    }
                }
            } 
        }
    }
}

还有一些其他的东西可以让值看到,但有效。这将在我的测试购物车页面上更新,一切看起来都很好。

我的问题是,当我进入测试结帐页面时,价格会短暂更新,然后被原始价格覆盖。我缺少一些在结帐时运行的更新,并且

woocommerce_before_calculate_totals
钩子似乎没有在结帐页面上进行永久更改。

我需要在哪里挂钩我的函数,以便无论结帐时发生什么负载,更改都会持续存在,从而覆盖最初成功的价格更改?

php woocommerce cart checkout product-price
1个回答
2
投票

您的代码中有一些奇怪的事情:

  • $cart_object
    (即
    WC_Cart
    对象)已经是挂钩函数中的参数(它取代了
    WC()->cart
    或过时的
    $woocommerce->cart
  • 所以
    WC()->cart->get_cart()
    $cart_object->cart_contents
    是完全相同的东西。最好的方法是在 foreach 循环中使用
    $cart_object->get_cart()
  • 您不需要
    global $woocommerce;
    ,因为它已包含在
    WC()
    中(全局 Woocommerce 对象已包含在
    $cart_object
    中)
  • reload_checkout
    不存在于
    WC_Sessions
    WC_Session_Handler
    (所以你的条件始终为真) 无论如何,这对于这个钩子没有用。
  • 有关信息
    $cart_item['data']
    是此购物车商品的
    WC_Product
    对象的实例。
  • 您有 2 个购物车循环,一个循环,您可以将所有东西合而为一(这样可以避免出现问题)。
  • $cart_item['key']
    $key
    (在购物车循环中) 始终是同一件事。在这种情况下你真的不需要使用它......
  • 您已在问题的新编辑中完全删除了价格计算,并且定价不再是动态的(像以前一样基于产品数量)。

因此,我尝试添加一种动态价格计算在更真实的条件下,但根据您的

$special_ids_array
数组键/值以不同的方式。

计算的动态价格折扣从符合条件的商品数量 3 开始(产品 ID 和“包装尺寸”属性)。

因此,所有这些都大大简化和紧凑了重新审视的代码:

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

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

    // array of ID's to match that represent each item
    $special_ids = array(
        0 => array( "id" => 15372, "product" => "TAF", "single_bag_price" => 42.99, "3_multiplier" => 0.82181902768 ),
        1 => array( "id" => 14285, "product" => "POW", "single_bag_price" => 29.99, "3_multiplier" => 0.8890741358 ),
    );

    // Loop through the cart items
    foreach( $cart->get_cart() as $cart_item ){
        $product_id  = $cart_item['product_id'];
        $quantity    = $cart_item['quantity'];
        $pack_size   = $cart_item['variation']['attribute_pa_sample-check']; // returns "full-pack", "sample" or null
        foreach ( $special_ids as $special_id ){
            if( $special_id['id'] == $product_id && $pack_size !== 'sample' && $quantity >= 3 ){
                $discounted_price = $special_id['single_bag_price'] * $special_id['3_multiplier']; // calculation (fake and custom)
                $cart_item['data']->set_price($discounted_price); // set the calculated price
            }
        }
    }
}

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

已测试且有效。

即使多次重新加载页面,基于购物车商品数量和自定义计算的动态计算定价仍会保留在结帐中。

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