简体   繁体   English

如何在 WooCommerce 中获得“添加到购物车”以使用替代价格?

[英]How to get "Add to cart" in WooCommerce to use alternate prices?

I have a WordPress site.我有一个 WordPress 网站。 I need 2 prices (retail price and wholesale price).我需要 2 个价格(零售价和批发价)。

I used custom fields to enter wholesale prices.我使用自定义字段输入批发价格。 Then used product table plugin and changed it to show whole sale price.然后使用产品表插件并将其更改为显示整个销售价格。

I want the "Add to Cart" simple use item price instead of wholesale price.我想要“添加到购物车”简单使用的商品价格而不是批发价格。

How can I change this?我怎样才能改变这个?

$add_to_cart_text_final = ( $product_type == 'grouped' || $product_type == 'external' || $add_to_cart_text == ' ' ? $product->add_to_cart_text() : $add_to_cart_text );//'?add-to-cart=' .  $data['id']; //home_url() . 
                $wptf_single_action .= apply_filters('woocommerce_loop_add_to_cart_link', 
                        sprintf('<a rel="nofollow" data-add_to_cart_url="%s" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', 
                                esc_attr( $add_to_cart_url ),
                                //'http://localhost/practice-wp/product-table/?add-to-cart=' . $data['id'] . '&attribute_borno=ETC&quantity=10', 
                                esc_url( $add_to_cart_url ), 
                                //esc_url( $product->add_to_cart_url() ), 
                                esc_attr( $default_quantity ), //1 here was 1 before 2.8
                                esc_attr($product->get_id()), 
                                esc_attr($product->get_sku()), 
                                esc_attr( $ajax_action_final . ( $row_class ? 'wptf_variation_product single_add_to_cart_button button alt disabled wc-variation-selection-needed wptf_woo_add_cart_button' : 'button wptf_woo_add_cart_button ' . $stock_status_class ) ), //ajax_add_to_cart  //|| !$data['price']
                                esc_html( $add_to_cart_text_final )
                                //esc_html($product->add_to_cart_text())
                        ), $product);

We have to set New price before total calculation in the cart page.我们必须在购物车页面中的总计算之前设置新价格。 please try this code请试试这个代码

        add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

    function add_custom_price( $cart_object ) {
        foreach ( $cart_object->cart_contents as $key => $value ) {
          $product = $value['data'];
          $product_id = $product->get_id(); // get the product ID
          $custom_price = get_post_meta( $product->get_id(), 'room', true );  //Add whole sale price custom field
            $value['data']->set_price($custom_price); 
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM