On the product details page, check the code of the next product!

Source: Internet
Author: User
Tags php foreach

 

<! -- For next & Prev link -->

<? PHP // Prev-/next-link...

// I don't quite understand why I need to assign a value: $ _ helper !!

$ _ Helper = $ this-> helper ('catalog/output ');

// Data in data [] -- use $ this-> get ();

$ _ Product = $ this-> getproduct ();

$ Prev_url = $ next_url = $ url = $ _ product-> getproducturl ();

// $ This-> helper ('catalog/data')-> getcategory (): Get the current category object. If yes, assign a value. If it does not exist, use another method to obtain

 

If ($ this-> helper ('catalog/data')-> getcategory ()){

$ Category = $ this-> helper ('catalog/data')-> getcategory ();

} Else {

// Obtain the current product and then the categoryids ("category" array). Here we only use the first one ($ _ ccats [0]), and then use Mage: GetModel () -> load () method to get the instantiated "Classification object"

$ _ Ccats = $ this-> helper ('catalog/data')-> getproduct ()-> getcategoryids ();

$ Category = Mage: GetModel ('catalog/Category ')-> load ($ _ ccats [0]);

}

 

$ Children = $ category-> getproductcollection ();

// Not very clear

$ _ COUNT = is_array ($ children )? Count ($ children): $ children-> count ();

If ($ _ count ){

Foreach ($ children as $ product ){

// Obtain the IDs of all products. Save to array $ plist.

$ Plist [] = $ product-> GETID ();

}

 

/**

* Determine the previous/next link and link to current category

*/

$ Current_pid = $ this-> helper ('catalog/data')-> getproduct ()-> GETID ();

// The array_search () function is used to find the key corresponding to a value in the array.

$ Curpos = array_search ($ current_pid, $ plist );

// Get link for Prev Product

//

$ Previd = isset ($ plist [$ curpos + 1])? $ Plist [$ curpos + 1]: $ current_pid;

$ Product = Mage: GetModel ('catalog/product')-> load ($ previd );

$ Prevpos = $ curpos;

While (! $ Product-> isvisibleincatalog ()){

$ Prevpos + = 1;

// If the next one exists, it is equal to the next one. If not, it is equal to its own

$ Nextid = isset ($ plist [$ prevpos])? $ Plist [$ prevpos]: $ current_pid;

$ Product = Mage: GetModel ('catalog/product')-> load ($ nextid );

}

$ Prev_url = $ product-> getproducturl ();

 

// Get link for next product

$ Nextid = isset ($ plist [$ curpos-1])? $ Plist [$ curpos-1]: $ current_pid;

$ Product = Mage: GetModel ('catalog/product')-> load ($ nextid );

$ Nextpos = $ curpos;

While (! $ Product-> isvisibleincatalog ()){

$ Nextpos-= 1;

//

$ Nextid = isset ($ plist [$ nextpos])? $ Plist [$ nextpos]: $ current_pid;

$ Product = Mage: GetModel ('catalog/product')-> load ($ nextid );

}

$ Next_url = $ product-> getproducturl ();

}

?>

 

 

<Div class = "prevnextcategoriesnavigation">

<Div class = "for-next">

<? PHP if ($ URL! = $ Prev_url):?>

<A class = "Next-product" href = "<? PHP echo $ prev_url;?> ">

<? PHP echo $ this->__ ('<-prev')?>

</A>

<? PHP endif;?>

</Div>

<Div class = "for-categories">

<Ul>

<Li> <? PHP echo $ this->__ ('back to categories: ')?> </LI>

<? PHP $ categories =$ _ product-> getcategoryids ();?>

<? PHP foreach ($ categories as $ K =>$ _ category_id):?>

<? PHP $ _ Category = Mage: GetModel ('catalog/Category ')-> load ($ _ category_id)?>

<Li> <a class = "back-to-catalog" href = "<? PHP echo $ _ category-> geturl ()?> "> <? PHP echo $ _ category-> getname ()?> </A> </LI>

<? PHP endforeach;?>

</Ul>

</Div>

<Div class = "for-Prev">

<? PHP if ($ URL! = $ Next_url):?>

<A class = "Prev-product" href = "<? PHP echo $ next_url;?> ">

<? PHP echo $ this->__ ('next-> ')?>

</A>

<? PHP endif;?>

</Div>

</Div>

<! -- For next & Prev link -->

 

 

// Obtain all products under the current category

$ Productcollection = $ this-> helper ('catalog/data')-> getcategory ()-> getproductcollection ();

// Obtain all categories corresponding to the current product

$ Category = $ this-> helper ('catalog/data')-> getproduct ()-> getcategoryids ();

Supplement:

1. Get store $ store = Mage: APP ()-> getstore ();

You can print $ store, which is a mage_core_model_store object. You can obtain data through the following code:

$ Store = Mage: APP ()-> getstore ()-> getdata ();

2. Get the configuration of the current store $ value = Mage: APP ()-> getstore ()-> getconfig ($ PATH)

 

 

 

First obtain the productid array, and then use this array to add and subtract keys to implement the previous and next products,

$ URL =$ _ product-> getproducturl ();

You can determine whether the URL is equal to $ prev_url or $ next_url. If the URL is equal, no next product exists and no previous product exists !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.