2 回答
TA贡献1784条经验 获得超2个赞
使用的正确钩子是woocommerce_cart_item_price这样的:
add_filter( 'woocommerce_cart_item_price', 'giftcard_cart_item_price', 10, 3 );
function giftcard_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
$giftcard_key = 'giftcard_product_price';
if( isset( $cart_item[$giftcard_key] ) ) {
$args = array( 'price' => floatval( $cart_item[$giftcard_key] ) );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
现在自定义价格将正确显示在迷你车中......
TA贡献1828条经验 获得超3个赞
可变产品篮子中的完整变体免费。
/**
* Initialize Session cart
*/
add_action('init', 'init_cart_session', 1);
function init_cart_session() {
if (!session_id()) {
session_start();
}
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
}
/**
* Custom size correct
*/
add_action('woocommerce_before_calculate_totals', 'woo_size_correct', 10, 6);
function woo_size_correct() {
global $woocommerce;
if (isset($_POST['attribute_pa_size']) && $_POST['attribute_pa_size'] == 'custom')
{
$_SESSION['cart'][$_POST['variation_id']] = $_POST['custom'];
}
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if (array_key_exists($cart_item['data']->variation_id, $_SESSION['cart'])) {
$cart_item['data']->set_price($_SESSION['cart'][$cart_item['data']->variation_id]);
}
}
}
add_filter('woocommerce_cart_item_price', 'woo_size_mini_cart_correct', 10, 6);
function woo_size_mini_cart_correct($price_html, $cart_item, $cart_item_key) {
global $woocommerce;
if (array_key_exists($cart_item['data']->variation_id, $_SESSION['cart'])) {
$args = array('price' => floatval($_SESSION['cart'][$cart_item['data']->variation_id]));
if (WC()->cart->display_prices_including_tax()) {
$product_price = wc_get_price_including_tax($cart_item['data'], $args);
} else {
$product_price = wc_get_price_excluding_tax($cart_item['data'], $args);
}
return wc_price($product_price);
}
return $price_html;
}
- 2 回答
- 0 关注
- 118 浏览
添加回答
举报