【问题标题】:Custom dynamic item pricing gets reset on Woocommerce Checkout自定义动态项目定价在 Woocommerce Checkout 上重置
【发布时间】:2018-01-18 00:16:24
【问题描述】:

我无法坚持我的价格更新。或多或少这是我用来更新价格的示例:

    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 price


【解决方案1】:

你的代码中有一些奇怪的东西:

  • $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_SessionsWC_Session_Handler 类中(所以你的条件总是正确的),无论如何这对这个钩子没有用。
  • 请注意,$cart_item['data'] 是此购物车商品的 WC_Product 对象的一个​​实例。
  • 您有 2 个购物车循环,一个接一个,您可以将所有东西合二为一(这样可以避免出现问题)。
  • $cart_item['key']$key (在购物车循环中) 总是相同的东西。在这种情况下,您实际上不需要使用它……
  • 您已在此问题的新编辑中完全删除了价格计算,并且定价不再是动态的(像以前一样基于产品数量)。

所以我尝试添加一种 动态价格计算 在更真实的条件下,但根据您的 $special_ids_array 数组键/值以不同的方式.

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

所以所有这些都简化并压缩了很多重新访问的代码:

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 文件中。

经过测试并且有效。

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

【讨论】:

  • 哦,天哪....看来我在写一些相当多余的爵士乐。我已经对此进行了测试,效果很好;也许我的只是循环并破坏了东西;但这很好。谢谢你的修改!
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多