【发布时间】: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