基于自定义字段的 WooCommerce 产品变化价格
WooCommerce product variation price based on custom fields
我有可变产品,我向其中添加了这样的自定义字段:
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Is Preordable
woocommerce_wp_checkbox(
array(
'id' => '_ab_preorder_checkbox[' . $variation->ID . ']',
'wrapper_class' => 'show_if_simple',
'label' => __(' Disponible à la précommande', 'woocommerce' ),
'description' => __( 'Disponible à la précommande', 'woocommerce' ),
'desc_tip' => true,
'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
)
);
// Custom Preorder Price
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_custom_price[' . $variation->ID . ']',
'label' => __( 'Prix à la précommande', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Prix à la précommande", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
)
);
// Date de livraison estimée
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_estimated_date[' . $variation->ID . ']',
'label' => __( 'Date de livraison estimé', 'woocommerce' ),
'placeholder' => '24/09/2021',
'desc_tip' => true,
'description' => __( "Date de livraison estimé", "woocommerce" ),
'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 ); // After all Variation fields
function ab_preorder_variation_fields_saving( $post_id ){
// Is Preordable
$woocommerce_text_field = $_POST['_ab_preorder_checkbox'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_checkbox', esc_attr( $woocommerce_text_field ) );
// Custom Preorder Price
$woocommerce_text_field = $_POST['_ab_preorder_custom_price'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_custom_price', esc_attr( $woocommerce_text_field ) );
// Date de livraison estimée
$woocommerce_text_field = $_POST['_ab_preorder_estimated_date'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_estimated_date', esc_attr( $woocommerce_text_field ) );
}
这是 WP Admin 中的结果:
这是我想要做的:
当客户选择变体时,我想:如果变体库存数量为 0 并且选中 _ab_preorder_checkbox
复选框,我想将产品价格设置为 100。
我试过下面的代码,但它不起作用:/
function action_woocommerce_before_calculate_totals( $cart_object) {
$cart_items = $cart_object->cart_contents;
if ( ! empty( $cart_items ) ) {
$price = 100;
foreach ( $cart_items as $key => $value ) {
if($value['data']['_ab_preorder_checkbox']=="yes")
$value['data']->set_price( $price );
}
}
};
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 2 );
谁能帮我解决这个问题?
此致,
由于 woocommerce 不允许购买零库存产品,您需要为相关产品设置“允许延期交货”选项。
我稍微重新审视了您的代码(特别是您的第二个函数)。
挂钩 woocommerce_before_calculate_totals
在您的情况下不方便。最好在产品上设置延期交货自定义价格。
以下将在启用复选框时在变体上设置延期交货价格,并将其显示在具有交货预计日期的相关变体上:
// Admin Variation custom fields
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 );
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Is Preordable
woocommerce_wp_checkbox(
array(
'id' => '_ab_preorder_checkbox['.$loop.']',
'wrapper_class' => 'show_if_simple',
'label' => __(' Disponible à la précommande', 'woocommerce' ),
'description' => __( 'Disponible à la précommande', 'woocommerce' ),
'desc_tip' => true,
'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
)
);
// Custom Preorder Price
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_custom_price['.$loop.']',
'label' => __( 'Prix à la précommande', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Prix à la précommande", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
)
);
// Date de livraison estimée
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_estimated_date['.$loop.']',
'label' => __( 'Date de livraison estimé', 'woocommerce' ),
'desc_tip' => true,
'description' => __( "Date de livraison estimé", "woocommerce" ),
'type' => 'date',
'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
)
);
echo '</div>';
}
// Save admin Variations custom fields values
add_action( 'woocommerce_admin_process_variation_object', 'ab_preorder_variation_fields_saving', 10, 2 );
function ab_preorder_variation_fields_saving( $variation, $loop ) {
if( isset($_POST['_ab_preorder_checkbox'][$loop]) ) {
$variation->update_meta_data( '_ab_preorder_checkbox', esc_attr($_POST['_ab_preorder_checkbox'][$loop]) );
}
if( isset($_POST['_ab_preorder_custom_price'][$loop]) ) {
$variation->update_meta_data( '_ab_preorder_custom_price', esc_attr($_POST['_ab_preorder_custom_price'][$loop]) );
}
if( isset($_POST['_ab_preorder_estimated_date'][$loop]) ) {
$variation->update_meta_data( '_ab_preorder_estimated_date', esc_attr($_POST['_ab_preorder_estimated_date'][$loop]) );
}
}
// Set the variation backorder price
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
if ( $product->get_stock_quantity() == 0 && $product->get_meta('_ab_preorder_checkbox') === 'yes' ) {
$backorder_price = $product->get_meta('_ab_preorder_custom_price');
if( $backorder_price > 0 ) {
$price = (float) $backorder_price;
}
}
return $price;
}
// Display prefixed backorder price and estimated delivery on single product pages
add_filter( 'woocommerce_available_variation', 'ab_available_variation_custom_field', 10, 3 );
function ab_available_variation_custom_field( $variation_data, $product, $variation ) {
if ( $variation->get_stock_quantity() == 0 && $variation->get_meta('_ab_preorder_checkbox') === 'yes' ) {
if ( $estimated_delivery_date = $variation->get_meta('_ab_preorder_estimated_date') ) {
// Display estimated delivery date
$variation_data['availability_html'] .= sprintf( '<p class="stock date-precommande">%s : %s</p>',
__("Date de livraison estimée (précommande)", "woocommerce"), $estimated_delivery_date );
}
// Displayed prefixed formatted price
$variation_data['price_html'] = '<span class="price-prefix">'.__("Prix à la précommande", "") .'<span> : ' . wc_price( $variation_data['display_price'] );
}
return $variation_data;
}
// Display on estimated delivery date on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
if ( $cart_item['variation_id'] > 0 && $cart_item['data']->get_stock_quantity() == 0
&& $cart_item['data']->get_meta('_ab_preorder_checkbox') === 'yes' ) {
if ( $estimated_delivery_date = $cart_item['data']->get_meta('_ab_preorder_estimated_date') ) {
$custom_items[] = array( "name" => __("Date de livraison estimée", "woocommerce"), "value" => $estimated_delivery_date );
}
}
return $custom_items;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我有可变产品,我向其中添加了这样的自定义字段:
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Is Preordable
woocommerce_wp_checkbox(
array(
'id' => '_ab_preorder_checkbox[' . $variation->ID . ']',
'wrapper_class' => 'show_if_simple',
'label' => __(' Disponible à la précommande', 'woocommerce' ),
'description' => __( 'Disponible à la précommande', 'woocommerce' ),
'desc_tip' => true,
'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
)
);
// Custom Preorder Price
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_custom_price[' . $variation->ID . ']',
'label' => __( 'Prix à la précommande', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Prix à la précommande", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
)
);
// Date de livraison estimée
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_estimated_date[' . $variation->ID . ']',
'label' => __( 'Date de livraison estimé', 'woocommerce' ),
'placeholder' => '24/09/2021',
'desc_tip' => true,
'description' => __( "Date de livraison estimé", "woocommerce" ),
'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 ); // After all Variation fields
function ab_preorder_variation_fields_saving( $post_id ){
// Is Preordable
$woocommerce_text_field = $_POST['_ab_preorder_checkbox'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_checkbox', esc_attr( $woocommerce_text_field ) );
// Custom Preorder Price
$woocommerce_text_field = $_POST['_ab_preorder_custom_price'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_custom_price', esc_attr( $woocommerce_text_field ) );
// Date de livraison estimée
$woocommerce_text_field = $_POST['_ab_preorder_estimated_date'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_estimated_date', esc_attr( $woocommerce_text_field ) );
}
这是 WP Admin 中的结果:
这是我想要做的:
当客户选择变体时,我想:如果变体库存数量为 0 并且选中 _ab_preorder_checkbox
复选框,我想将产品价格设置为 100。
我试过下面的代码,但它不起作用:/
function action_woocommerce_before_calculate_totals( $cart_object) {
$cart_items = $cart_object->cart_contents;
if ( ! empty( $cart_items ) ) {
$price = 100;
foreach ( $cart_items as $key => $value ) {
if($value['data']['_ab_preorder_checkbox']=="yes")
$value['data']->set_price( $price );
}
}
};
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 2 );
谁能帮我解决这个问题?
此致,
由于 woocommerce 不允许购买零库存产品,您需要为相关产品设置“允许延期交货”选项。
我稍微重新审视了您的代码(特别是您的第二个函数)。
挂钩 woocommerce_before_calculate_totals
在您的情况下不方便。最好在产品上设置延期交货自定义价格。
以下将在启用复选框时在变体上设置延期交货价格,并将其显示在具有交货预计日期的相关变体上:
// Admin Variation custom fields
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 );
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Is Preordable
woocommerce_wp_checkbox(
array(
'id' => '_ab_preorder_checkbox['.$loop.']',
'wrapper_class' => 'show_if_simple',
'label' => __(' Disponible à la précommande', 'woocommerce' ),
'description' => __( 'Disponible à la précommande', 'woocommerce' ),
'desc_tip' => true,
'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
)
);
// Custom Preorder Price
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_custom_price['.$loop.']',
'label' => __( 'Prix à la précommande', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Prix à la précommande", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
)
);
// Date de livraison estimée
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_estimated_date['.$loop.']',
'label' => __( 'Date de livraison estimé', 'woocommerce' ),
'desc_tip' => true,
'description' => __( "Date de livraison estimé", "woocommerce" ),
'type' => 'date',
'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
)
);
echo '</div>';
}
// Save admin Variations custom fields values
add_action( 'woocommerce_admin_process_variation_object', 'ab_preorder_variation_fields_saving', 10, 2 );
function ab_preorder_variation_fields_saving( $variation, $loop ) {
if( isset($_POST['_ab_preorder_checkbox'][$loop]) ) {
$variation->update_meta_data( '_ab_preorder_checkbox', esc_attr($_POST['_ab_preorder_checkbox'][$loop]) );
}
if( isset($_POST['_ab_preorder_custom_price'][$loop]) ) {
$variation->update_meta_data( '_ab_preorder_custom_price', esc_attr($_POST['_ab_preorder_custom_price'][$loop]) );
}
if( isset($_POST['_ab_preorder_estimated_date'][$loop]) ) {
$variation->update_meta_data( '_ab_preorder_estimated_date', esc_attr($_POST['_ab_preorder_estimated_date'][$loop]) );
}
}
// Set the variation backorder price
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
if ( $product->get_stock_quantity() == 0 && $product->get_meta('_ab_preorder_checkbox') === 'yes' ) {
$backorder_price = $product->get_meta('_ab_preorder_custom_price');
if( $backorder_price > 0 ) {
$price = (float) $backorder_price;
}
}
return $price;
}
// Display prefixed backorder price and estimated delivery on single product pages
add_filter( 'woocommerce_available_variation', 'ab_available_variation_custom_field', 10, 3 );
function ab_available_variation_custom_field( $variation_data, $product, $variation ) {
if ( $variation->get_stock_quantity() == 0 && $variation->get_meta('_ab_preorder_checkbox') === 'yes' ) {
if ( $estimated_delivery_date = $variation->get_meta('_ab_preorder_estimated_date') ) {
// Display estimated delivery date
$variation_data['availability_html'] .= sprintf( '<p class="stock date-precommande">%s : %s</p>',
__("Date de livraison estimée (précommande)", "woocommerce"), $estimated_delivery_date );
}
// Displayed prefixed formatted price
$variation_data['price_html'] = '<span class="price-prefix">'.__("Prix à la précommande", "") .'<span> : ' . wc_price( $variation_data['display_price'] );
}
return $variation_data;
}
// Display on estimated delivery date on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
if ( $cart_item['variation_id'] > 0 && $cart_item['data']->get_stock_quantity() == 0
&& $cart_item['data']->get_meta('_ab_preorder_checkbox') === 'yes' ) {
if ( $estimated_delivery_date = $cart_item['data']->get_meta('_ab_preorder_estimated_date') ) {
$custom_items[] = array( "name" => __("Date de livraison estimée", "woocommerce"), "value" => $estimated_delivery_date );
}
}
return $custom_items;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。