代码之家  ›  专栏  ›  技术社区  ›  iMMuNiTy

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

  •  2
  • iMMuNiTy  · 技术社区  · 8 年前

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

    因此,现在我有了尽可能多的价格设置:

    • 正常价格(例如 100$ ),则,
    • 销售价格(未设定)
    • (和) 这个 bestprice (例如 90$ )

    我想添加一个按钮 “单击以获得更好的价格” 在产品页中。通常,产品页面中的价格为 正常价格

    • 如果用户按add to cart,则1个产品 100个$ 将添加到购物车中。
    • 如果用户单击“单击以获得更好的价格”按钮(价格将更改为 最佳价格 90$ )当他将按add来运送 90$ 将被添加到购物车中。

    在此方面的任何帮助都将不胜感激。

    1 回复  |  直到 8 年前
        1
  •  3
  •   LoicTheAztec    7 年前

    这是一个完整的解决方案代码,我已将您的自定义字段slug替换为 _bestprice 因为它应该以下划线开头,如 _price _sale_price

    在简单产品的后端中,附加的“最佳价格”自定义字段:

    enter image description here

    // 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'] ) );
        }
    }
    

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

    enter image description here

    当客户点击“获得更好的价格按钮”时,显示的价格在某种价格范围内变化,就像产品在销售时一样 (当产品在销售时,它将取代销售价格) :

    enter image description here

    此处(示例)产品正在销售,销售价格 $32.00 被bestprice取代 $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”时更改购物车项目价格:

    enter image description here

    // 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'] );
            }
        }
    }
    

    这是可选代码 通过产品链接更改“添加到购物车”链接 当出现以下情况时,在商店和归档页面中 最佳价格 在产品设置选项中设置:

    // 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;
    }
    

    代码进入功能。活动子主题(或活动主题)的php文件 。已测试并正常工作。