Set-up
I've decided to try a little bit of PHP myself, I'm a total beginner.
I run a WooCommerce shop and want to dynamically display the expected delivery date range on each product page.
The delivery date range is today's date plus 3 and plus 4 days, e.g. today is the of 10th October so the delivery date range is 13 and 14 October. On each product page it should therefore mention,
Delivery between Oct 13 and Oct 14
I know where to write the code such that it displays where I want it to appear on the product pages.
I also know how to dynamically update the delivery dates.
Problem
Echo mixes the variables, instead of,
Delivery between Oct 13 and Oct 14
it states,
Delivery between and Oct 13Oct 14
My Code
function my_custom_action() {
$plus3 = strtotime("+3 day");
$plus4 = strtotime("+4 day");
$date_low = date('M d', $plus3);
$date_high = date('M d', $plus4);
$start_text = _e('Delivery between ','woodmart');
$end_text = _e(' and ','woodmart');
echo $start_text, $date_low, $end_text, $date_high ;
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );
I need $start_text
and $end_text
in functions so the strings are translatable.
Preferably I also have the months (e.g. Oct
) translatable, but for now I'd already be very happy to know how I get all variables echoed in the desired order.
This can be resolved using string interpolation.
for eg.
$name = "PHP"; echo "I am reading {$name}POT"; // output: I am reading PHPPOT
here's the link to a much more detailed reference : variable-interpolation-in-php