简体   繁体   中英

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

I have a WordPress site. I need 2 prices (retail price and wholesale price).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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