Search HTML page for URL value & replace based on Product

Discussion in 'Customization & add-ons' started by karyng01, Jun 4, 2018.

  1. karyng01

    karyng01 aMember Pro Customer

    Joined:
    Jul 30, 2008
    Messages:
    71
    Hi Everyone,

    Manual Breadcrumb HTML
    We have a manually built breadcrumb menu across hundreds of aMember pages for one product called "{ Synergy }" / product id=34

    Here is an example of the breadcrumb menu on one page.

    Code:
      <ul class="crumbs"> 
        <li class="first"><a href="/members/member/" style="z-index:9;"><span></span>My Account</a></li>
        <li><a href="/members/content/p/id/179/" style="z-index:8;">{ Synergy }</a></li>
        <li><a href="/members/content/Lessons.8" style="z-index:7;">Lessons</a></li>
        <li><a href="/members/content/Facilitation+Skills+Lessons.11" style="z-index:6;">Facilitation Skills Lessons</a></li>
        <li><a href="/members/content/p/id/230/" style="z-index:5;">Facilitation &#8211; Active Listening</a></li>
      </ul>
    New Product Shares (some) Pages
    We are now sharing a sub-set of these pages for a second product called "{ Synergy - Core 1 }" / product id=80.
    As the breadcrumb menu is manually coded in HTML, we need to modify one value in it depending on product the customer has.

    The customer can NOT have both products active at the same time.

    Question
    How can I search every page as they render for a customer and replace one URL with a different URL, based on the product that customer has purchased?

    Current URL: "/members/content/p/id/179/"
    Replace with URL: "/members/content/p/id/406/"

    Can this be done in some global way, for example in site.php so I don't have to insert PHP code on every page?

    DOMDocument / preg_match / preg_replace
    My research has come up with these three likely PHP functions but I am not sure how to test for a product id then depending on which product a customer has access to, replace one URL value with another or leave the original value alone.

    Thanks if anyone has a solution, my PHP coding knowledge is somewhat basic.

    Aly
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello Aly,

    I suggest the following solution:

    1. First of all replace literal value of 179 with abstract placeholder %user_pid%
    You can do it for all your pages in database directly with the following query:

    Code:
    UPDATE `am_page` SET html = REPLACE(html, '"/members/content/p/id/179/" style="z-index:8;"', '"/members/content/p/id/%user_pid%/" style="z-index:8;"');

    2. Then replace this placeholder with ID based on user active subscriptions.
    You can do it with the following code in site.php file:
    http://www.amember.com/docs/Site.php_file

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::PAGE_BEFORE_RENDER, function(Am_Event $e) {
     
        
    $t $e->getTemplate();
        
    /* @var $u User */
        
    $u $e->getUser();
      
        
    $t->setUser_pid(in_array(34$u->getActiveProductIds()) ? 179 406);
      
    });
    I hope it help you.

    Best Regards.
    karyng01 and relapse like this.
  3. karyng01

    karyng01 aMember Pro Customer

    Joined:
    Jul 30, 2008
    Messages:
    71
    Hi Caesar,

    This looks like a great solution, thank you so much. I will try it out and report back on the success.

    Your help is much appreciated for the coding was beyond me.

    Aly
  4. karyng01

    karyng01 aMember Pro Customer

    Joined:
    Jul 30, 2008
    Messages:
    71
    Hi Caesar,

    Your solution fixed the problem. Everything worked perfectly first time, code & SQL syntax without error.

    Thank you so much.

    Aly
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I am glad to hear that. You are welcome!

Share This Page