Woocommerce shows the price of both options

I create a store for a customer. Now on the product page there is a drop-down list where you can select "Standard" or "Express".

When this is selected by the customer, you can choose the amount you want to receive from this product.

now I found a piece of code in stackoverflow ( Woocommerce get the price of a product change ) to display the correct price immediately after the amount in the drop-down list. it works great.

But now the price of the first change in the amount (100 (€ 22)) is also displayed in the delivery drop-down list (2 - 3 days (€ 22). And I would like to remove the price from the delivery variable / drop-down menu.

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;


    $query = "SELECT postmeta.post_id AS product_id
                FROM {$wpdb->prefix}postmeta AS postmeta
                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $product->id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
        $_product = new WC_Product_Variation( $variation_id[0] );
        return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
    }
    return $term;

}

$variant_id [0]; 1, 2, 3, 4 5, , , .

Thnx :)

0
1

, display_price_in_variation_option_name(), , . , , , - , . , "" " " " ". , , , , , , , , .

, 48

/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php

woocommerce_variation_option_name, 48 54 ( 2.3.0). , . , , . , , $name. , . -, $name, :

apply_filters( 'woocommerce_variation_option_name', $term->name )

apply_filters( 'woocommerce_variation_option_name', $term->name, $name )

, . , :

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' )

:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name', 10, 2 )

, . :

function display_price_in_variation_option_name( $term ) {

:

function display_price_in_variation_option_name( $term, $name = null ) {

, , , . , , , . , . - WP Admin " β†’ " , . URL- ? = -, "pa_" - , "pa_" ( ). , "pa_shipping". :

function display_price_in_variation_option_name( $term, $name = null ) {

if($name == 'pa_shipping') {
  return $term;
}

global $wpdb, $product;

// the rest of your original code

, , , . JavaScript, , ( ).

0

Source: https://habr.com/ru/post/1665471/


All Articles