PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 1, 2022

[FIXED] How can i add Order Estimator to Shopify product details page

 July 01, 2022     javascript, shopify     No comments   

Issue

Basically, I want to show "FREE delivery {delivery-estimate}. Order within {time-counter}" above my add to cart button on my Shopify page. How can I do it?

Note:

  1. delivery estimate should be after 7 days as of the current date.
  2. time counter should refresh at 1:00 pm, and if the time is after 1:00 pm I want to add one more day to the estimated date.

Here is what I found before, but I cannot change it to the format I initially wanted.

<script language="JavaScript"> 
function day(a) {
    var date = new Date();
    var hours = date.getHours();

    // If after 3pm, add 1 day
    if(hours > 15) a++;

    var expectedDeliveryDate = addWeekdays(date, a);
    document.write(expectedDeliveryDate.toDateString() + ' with Standard Delivery');
}

function addWeekdays(fromDate, days) {
    var count = 0;
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }
    return fromDate;
}

</script>

<body>
<script language="JavaScript">
day(1)
</script>


Solution

Try something like this and you can filter this according to your needs:

const date = new Date()
const hours = date.getHours()

const newDate = new Date(date);
const deliveryEstimate = hours < 13 ? new Date(newDate.setDate(newDate.getDate() + 7)) : new Date(newDate.setDate(newDate.getDate() + 8))
const orderWithinValue = hours < 13 ? 13-hours : hours-13

console.log(`FREE delivery by ${deliveryEstimate.toISOString().split('T')[0]}. Order within ${orderWithinValue} hours`)

Your problem statement will have just a couple of "if condtions" and you need to handle them accordingly.



Answered By - Prateek Goyal
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing