我在保持价格更新时遇到问题。或多或少这是我用来更新价格的示例:
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
钩子似乎没有在结帐页面上进行永久更改。
我需要在哪里挂钩我的函数,以便无论结帐时发生什么负载,更改都会持续存在,从而覆盖最初成功的价格更改?
您的代码中有一些奇怪的事情:
$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
对象的实例。$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 文件中。
已测试且有效。
即使多次重新加载页面,基于购物车商品数量和自定义计算的动态计算定价仍会保留在结帐中。