Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 3 replies, has 3 voices.

Last updated by Andrey 1 year, 9 months ago.

Assisted by: Andrey.

Author Posts
August 30, 2023 at 7:45 pm #14315943

rigdenA

I have custom script added to woocommerce product pages for rental period. That calculates price for customer. Unfortunately It doesn't show up on translated version.

For example:

Original language:
hidden link

Translated language:
hidden link

August 31, 2023 at 9:25 am #14318675

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

1) In order to expedite handling your support requests, can you please share WPML support debug information from your site.

- To give debug information, login to your site and go to WPML → Support. From there click on the “debug information” link at the bottom of the page.
- Once on the “debug information” page, select the entire content of the text box and copy it.
- Now go back to our support forum, find debug information box and paste it.

More information on the link: https://wpml.org/faq/provide-debug-information-faster-support/

2) Can you please explain how are you adding his script to your default language page?

3) Please explain how it is missing, it is just not working on your 2nd language page or?

Thanks,
Drazen

August 31, 2023 at 7:53 pm #14323449

rigdenA

Hi

Thanks for reply

1) I did not find any box or place where to add debug information. Im pretty sure I added it when I opened this ticket. Check again please..

2) This script is added to functions.php

// Add a custom field before single add to cart
add_action('woocommerce_before_add_to_cart_button', 'display_single_product_custom_fields', 5);

function display_single_product_custom_fields() {
    if(!enable_rent_to_cat()) return;
	
    // Get the rental days data options
    $options = array(''  => __("Valitud periood", "woocommerce")) + get_rental_days_options();
	
    echo '<div class="custom-text text">
    <h5>'.__("Vali rendiperiood ja kuupäev", "woocommerce").'</h5>
    <label>'.__("Algus", "woocommerce").': </label>
    <input type="date" name="rental_date" value="" class="rental_date" required title="Vali rendi algus kuupäev" required />
    <label>Periood:</label>
    <select class="rental-days" id="rental-days" name="rental_days" required title="Vali rendiperiood" required>';
	

    foreach( $options as $key => $option ){
        echo '<option value="'.$key.'">'.$option.'</option>';
    }

    echo '</select>
    </div>';
}

// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3);

function add_custom_field_data($cart_item_data, $product_id, $variation_id) {
    // HERE set the percentage rate to be applied to get the new price
    $percentage  = 100;

    if (isset($_POST['rental_date']) && !empty($_POST['rental_date'])) {
        $cart_item_data['custom_data']['start_date'] = $_POST['rental_date'];
    }

    if (isset($_POST['rental_days']) && !empty($_POST['rental_days'])) {
        $cart_item_data['custom_data']['rental_days'] = esc_attr($_POST['rental_days']);

        $_product_id = $variation_id > 0 ? $variation_id : $product_id;

        $product     = wc_get_product($_product_id); // The WC_Product Object
        $base_price  = (float) $product->get_regular_price(); // Get the product regular price

        //$price_rate  = $cart_item_data['custom_data']['rental_days'] * $percentage / 100;
        $price_rate  = get_rental_rates($cart_item_data['custom_data']['rental_days']);

        $cart_item_data['custom_data']['base_price']  = $base_price;
        $cart_item_data['custom_data']['new_price']   = $base_price * $price_rate;
    }

    // Make each cart item unique
    if (isset($cart_item_data['custom_data']['rental_days']) || isset($cart_item_data['custom_data']['start_date'])) {
        $cart_item_data['custom_data']['unique_key'] = md5(microtime().rand());
    }

    return $cart_item_data;
}

// Set the new calculated cart item price
add_action('woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1);
function extra_price_add_custom_price($cart) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach($cart->get_cart() as $cart_item) {
        if (isset($cart_item['custom_data']['new_price']))
            $cart_item['data']->set_price((float) $cart_item['custom_data']['new_price']);
    }
}

// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 20, 3);

function display_cart_items_custom_price_details($product_price, $cart_item, $cart_item_key) {
    if (isset($cart_item['custom_data']['base_price'])) {
        $product = $cart_item['data'];
       /* echo "<pre>";
        print_r($cart_item);
        echo "</pre>";*/
        $base_price = $cart_item['custom_data']['base_price'];
        $price_rate  = get_rental_rates($cart_item['custom_data']['rental_days']);
        $product_price = wc_price(wc_get_price_to_display($product, array('price' => $base_price * $price_rate ))). '<br>';

        if (isset($cart_item['custom_data']['rental_days'])) {
            $rental_days    = get_rental_days_options();
            $product_price .= $rental_days[$cart_item['custom_data']['rental_days']];
        }
    }
    return $product_price;
}

// Display in cart item the selected date
add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);

function display_custom_item_data($cart_item_data, $cart_item) {
    if (isset($cart_item['custom_data']['start_date'])) {
        $cart_item_data[] = array(
            'name'  => __("Rendi algus", "woocommerce"),
            'value' => date('d.m.Y', strtotime($cart_item['custom_data']['start_date'])),
        );
    }

    if (isset($cart_item['custom_data']['rental_days'])) {
        $rental_days    = get_rental_days_options();
        $cart_item_data[] = array(
            'name'  => __("Rendiperiood", "woocommerce"),
            'value' => $rental_days[$cart_item['custom_data']['rental_days']],
        );
    }

    return $cart_item_data;
}


// Save and display custom field in orders and email notifications (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_fields_update_order_item_meta', 20, 4 );

function custom_fields_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset( $values['custom_data']['start_date'] ) ){
        $date = date( 'd.m.Y', strtotime( $values['custom_data']['start_date'] ) );
        $item->update_meta_data( __( 'Rendi algus', 'woocommerce' ), $date );
    }
    if ( isset( $values['custom_data']['rental_days'] ) ){
        $rental_days = get_rental_days_options();
        $item->update_meta_data( __( 'Rendiperiood', 'woocommerce' ), $rental_days[$values['custom_data']['rental_days']] );
    }
}

3) It is not showing up on 2nd language.

September 1, 2023 at 8:06 am #14324961

Andrey
WPML Supporter since 06/2013

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kyiv (GMT+03:00)

Thank you for sharing the information.

Your script needs to be debugged to say what is wrong. But we cannot debug it as this is out of the scope of our support. I suspect, at some point, your script fails in secondary languages. That could explain why it doesn't show.

You are getting product ID in your script, which could be different in secondary languages. I suggest adjusting this by using the 'wpml_object_id' filter.

Here's more information:
https://wpml.org/wpml-hook/wpml_object_id/

Please also refer to the WCML Hooks Reference page:
https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/

I hope this helps.

September 1, 2023 at 9:45 am #14325689

rigdenA

Hi,

Thanks, got it fixed. Seemed it messed up categories hierarchy, which caused script not to work. I synced categories again and now it is working.