在 Woocommerce 中添加到购物车之前更改产品价格的附加自定义按钮

Additional custom button to change product price before adding to cart in Wovcommerce

在 Woocommerce 中,我在后端为我的产品添加了一个额外的价格字段 "bestprice"

所以现在我有尽可能多的价格设置:

我想在产品页面添加一个按钮 "click for better price"。通常情况下,产品页面中的价格将是正常价格

如有任何帮助,我们将不胜感激。

这是一个完整的解决方案代码……我已经用 _bestprice 替换了您的自定义字段 slug,因为它应该以下划线开头,例如 _price_sale_price.

在简单产品的后端,额外的 "Best 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'] ) );
    }
}

在单个产品页面中我们添加一个自定义按钮(和一个隐藏字段):

当客户点击"Get better price button"时,显示的价格在一种价格范围内变化,就像产品打折时一样(产品打折时,它取代销售价) :

此处(示例)产品正在促销,促销价 .00 替换为最佳价格 .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;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.