我已经从2.6.14更新到WC 3.0.1.
我原来的代码如下:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Run Code Online (Sandbox Code Playgroud)
它不再更新购物车或迷你车的价格.
我尝试动态更改产品的价格。我已经安装了插件WC_fields_Factory,并添加了一些字段来插入价格。
这是我的更改价格代码:
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
add_filter( 'woocommerce_add_cart_item', array( $this, 'simone_add_cart_item' ), 10, 1 );
add_action( 'woocommerce_before_calculate_totals', array( $this, 'simone_update_price'), 99 );
Run Code Online (Sandbox Code Playgroud)
[...]
private function _getPrice($product_id, $price) {
$all_fields = apply_filters( 'wcff/load/all_fields', $product_id, 'wccpf' );
$prezzoCalcolato = 0;
foreach ( $all_fields as $title => $fields )
{
foreach ( $fields as $field )
{
if( isset($field['field_valore']) && $field['field_grouprice'] == 'price-var' )
{
$quantita = isset($_REQUEST[ $field["name"] ]) ? intval($_REQUEST[ $field["name"] ]) : 0; …Run Code Online (Sandbox Code Playgroud) 在 Woocommerce 中,我曾经jQuery在单个产品页面上计算自定义价格,现在需要将此值传递给购物车。
所需的行为是将从隐藏字段检索到的新价格传递给购物车项目价格。
这是我的实际代码:
// Hidden input field in single product page
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11, 0 );
function custom_hidden_product_field() {
echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="">';
}
// The code to pass this data to the cart:
add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty( $_REQUEST['custom_price'] ) ) {
// Set the custom data in the cart item
$cart_item_data['custom_data']['custom_price'] = $_REQUEST['custom_price'];
$data = array( 'custom_price' => $_REQUEST['custom_price'] ); …Run Code Online (Sandbox Code Playgroud) 我需要在单个产品中添加一个带有自定义价格的选择,如下面的屏幕截图所示;

这个想法是,通过从 (select) 中选择一个选项,您可以将其增加到基本价格。
不使用变体,因为我的想法是做更多的事情,但我需要关于如何做到这一点的帮助。
我见过这样做的插件被称为“附加组件”,但我不想使用插件。