【发布时间】:2019-12-25 08:09:31
【问题描述】:
我在网站加载时有一个自定义弹出窗口并将所选选项存储在 cookie 中,我想根据所选选项更改产品价格。
我已经根据产品中的那些选项添加了价格元框,用于简单和可变的产品。
并在前端检索该价格,请检查以下代码:
add_filter( 'woocommerce_product_get_price', 'return_price_by_option', 10, 2 );
function return_price_by_option( $price, $product ) {
// options can be digits like 1, 2, 3, ...
$option = isset( $_COOKIE['option'] ) ? $_COOKIE['option'] : '';
if( $option ) {
$regMKey = '_sale_price_option_' . $option;
$optPrice = get_post_meta( $product->get_id(), $regMKey, true );
if( empty($optPrice) ) {
$regMKey = '_regular_price_option_' . $option;
$optPrice = get_post_meta( $product->get_id(), $regMKey, true );
}
return $optPrice;
}
return $price;
}
// Also used two more filter for regular and sale price,
// Function have same code, but just return the price for same meta key
add_filter( 'woocommerce_product_get_regular_price', 'return_regular_price_by_option', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'return_sale_price_by_option'), 10, 2 );
它适用于简单的产品, 但是,不适用于可变产品,任何人都可以帮助我处理可变产品吗?请提出任何简单的方法。
【问题讨论】:
标签: wordpress dynamic woocommerce price