在 Woocommerce 中,我在后端为我的产品添加了一个额外的价格字段
"bestprice"
。
所以现在我有尽可能的价格设置:
100$
),bestprice
(例如 90$
)我想在产品页面添加一个按钮“点击以获得更好的价格”。一般情况下产品页面的价格是正常价格。
100$
的 1 个产品将添加到购物车中。bestprice
90$),当他按下“添加到购物车”时,90$
的产品将被添加到购物车中.对此的任何帮助将不胜感激。
这是完整的解决方案代码...我已将您的自定义字段段替换为
_bestprice
,因为它应该以下划线开头,例如 _price
或 _sale_price
。
在简单产品的后端,附加的“最佳价格”自定义字段:
// Add a custom field to product in backend (for testing)
add_action( 'woocommerce_product_options_pricing', 'add_field_product_options_pricing' );
function add_field_product_options_pricing() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_bestprice',
'label' => __('Best price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
'placeholder' => __('Set the best price…', 'woocommerce'),
'description' => __('Enter the custom value here.', 'woocommerce'),
'desc_tip' => 'true',
));
echo '</div>';
}
// Save product custom field to database when submitted in Backend (for testing)
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
// Saving custom field value
if( isset( $_POST['_bestprice'] ) ){
update_post_meta( $post_id, '_bestprice', sanitize_text_field( $_POST['_bestprice'] ) );
}
}
在单个产品页面中,我们添加一个自定义按钮(和一个隐藏字段):
当顾客点击“获取更好的价格按钮”时,显示的价格会在某种价格范围内变化,就像产品促销时一样(当产品促销时,它会取代促销价):
此处(示例)产品正在促销,促销价
$32.00
替换为最优惠价格 $30.00
。
// Add a 'Get better price' additional button and a hidden field below single add to cart button
add_action( 'woocommerce_after_add_to_cart_button', 'before_add_to_cart_button' );
function before_add_to_cart_button() {
global $product;
// Get your product 'bestpprice' custom field
$bestprice = get_post_meta( $product->get_id(), '_bestprice', true);
if( ! empty($bestprice) ):
$bestprice = wc_get_price_to_display( $product, array( 'price' => $bestprice ) );
$reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$range = wc_format_sale_price( $reg_price, $bestprice );
?>
<!-- The button and hidden field -->
<div class="bestprice-wrapper"><br>
<a href="" class="get_bestprice button alt" id="get_bestprice"><?php _e('Get better price'); ?></a>
<input type="hidden" name="bestprice" id="bestprice" class="bestprice" value="" />
</div>
<!-- The jQuery code -->
<script type="text/javascript">
(function($){
var b = '<?php echo $bestprice; ?>',
i = 'input[name=bestprice]',
p = 'p.price',
r = '<?php echo $range; ?>',
t = 'a#get_bestprice'
u = true;
$(t).click( function(e){
e.preventDefault();
if(u){
$(p).html(r); // Replacing price with the range
$(i).val(b); // Set the best price in hidden input field
u = false; // Disable button
$(t).text('Better Price active'); // change button text
$(t).removeClass('alt'); // Remove button 'alt' class for styling
}
});
})(jQuery);
</script>
<?php
endif;
}
启用“bestprice”后,数据将添加到购物车对象并更改购物车商品价格:
// Add custom fields data to cart items
add_filter( 'woocommerce_add_cart_item_data', 'custom_add_cart_item_data', 20, 2 );
function custom_add_cart_item_data( $cart_item, $product_id ){
if( ! isset( $_POST['bestprice'] ) )
return $cart_item;
if( ! empty( $_POST['bestprice'] ) )
$cart_item['custom_data']['bestprice'] = (float) esc_attr( $_POST['bestprice'] );
return $cart_item;
}
// Replacing cart item price with 'bestprice'
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_bestprice', 20, 1 );
function set_cart_item_bestprice( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( isset( $cart_item['custom_data']['bestprice'] ) ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( (float) $cart_item['custom_data']['bestprice'] );
}
}
}
这是一个可选代码 当在产品设置选项中设置 bestprice 时,可在商店和存档页面中通过产品链接更改添加到购物车链接:
// Change add to cart link by a link to the product in Shop and archives pages for bestprice enabled option
add_filter( 'woocommerce_loop_add_to_cart_link', 'bestprice_loop_add_to_cart_button', 10, 2 );
function bestprice_loop_add_to_cart_button( $button, $product ) {
// Get your product 'bestpprice' custom field
$bestprice = get_post_meta( $product->get_id(), '_bestprice', true);
// Only for enabled "bestprice" option price.
if( ! empty( $bestprice ) ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。