通过phtml在主页上显示随机产品

时间:2017-01-05 10:41:16

标签: php magento-1.9

我的主页上显示了一个块,其中显示了我们指定其ID的产品。主页上的代码(静态块)如下所示:

 protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.registration);

                //initializing firebase auth object
                firebaseAuth = FirebaseAuth.getInstance();

                //initializing views
                editTextEmail = (EditText) findViewById(R.id.editTextEmail);
                editTextPassword = (EditText) findViewById(R.id.editTextPassword);

                buttonSignup = (Button) findViewById(R.id.buttonSignup);

                progressDialog = new ProgressDialog(this);

                //attaching listener to button
                buttonSignup.setOnClickListener(this);
            }

            private void registerUser(){

                //getting email and password from edit texts
                String email = editTextEmail.getText().toString().trim();
                String password  = editTextPassword.getText().toString().trim();

                //checking if email and passwords are empty
                if(TextUtils.isEmpty(email)){
                    Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
                    return;
                }

                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
                    return;
                }

                //if the email and password are not empty
                //displaying a progress dialog

                progressDialog.setMessage("Registering Please Wait...");
                progressDialog.show();

                //creating a new user
                firebaseAuth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                //checking if success
                                if(task.isSuccessful()){
                                    //display some message here
                                    Toast.makeText(registration.this,"Successfully registered",Toast.LENGTH_LONG).show();
         Intent i=new Intent(registration.this, MainActivity.class);
         startActivity(i);
                                }else{
                                    //display some message here
                                    Toast.makeText(registration.this,"Registration Error",Toast.LENGTH_LONG).show();

                                }
                                progressDialog.dismiss();
                            }
                        });

            }

            @Override
            public void onClick(View view) {
                //calling register method on click
                registerUser();
            }
        }

one_product.phtml文件包含以下代码:

{{block type="core/template" product_id="2559" template="catalog/product/one_product.phtml"}}

所以基本上我想用逗号分隔我指定的随机产品。例如:我在静态块中指定product_id =&#34; 2559,2661,2857,9293&#34;它应该随机显示这4种产品中的任何一种。

这样做的方法是什么?

还有什么方法可以让它从SKU拉出产品吗?由于我们记得所有SKU,但我们每次在此处更改产品时都必须检查每个产品ID。

如果问题太基础,请原谅我,我不是开发人员。 您可以在www.giftzila.com上看到它的实际效果

谢谢!

1 个答案:

答案 0 :(得分:1)

在app / design / frontend / default / Your_Theme / template / catalog / random-product.phtml创建一个名为random-product.phtml的新文件,然后在该文件中添加以下代码

    <?php
$chosen_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$_productCollection = $this->getLoadedProductCollection();
$number_of_products = $this->getNumProducts();
if (sizeof($_productCollection) < $number_of_products) {
    $number_of_products = sizeof($_productCollection);
}
$displayed_products = array();
foreach ($_productCollection as $_product) {
    $displayed_products[] = $_product;
}
$random_products = array();
if (sizeof($_productCollection) > 1) {
    $random_products = array_rand($displayed_products, $number_of_products);
} else {
    $random_products = array('0');
}
?>
<?php if(!$_productCollection->getSize()):?>
<div class="note-msg">
    <?=$this->__('There are no products matching the selection.')?>
</div>
<?php else:?>
<div class="main-binder">
  <div class="cms-box">
  <div class="category-title">
      <h2>Random Products</h2>
  </div>
  <div class="category-products">
    <table id="products-grid-table" class="products-grid">
    <?php
    $k=0;
    for ($i=0; $i < $number_of_products; $i++): ?>
    <?php if ($k == 0) { ?>
            <tr class="first odd">
        <?php } if($k==3)   { $k=0;  ?>
            </tr><tr class="first odd even">
        <?php } ?>
            <td id="td_<?php echo ($k+1); ?>" <?php if($k==3){ ?>class="last"<? } ?> >
        <div class="cms-box">
              <div id="cont_<?php echo ($k+1); ?>">
              <div class="product-name-block">
                  <?php
                  $pname=$this->htmlEscape($displayed_products[$random_products[$i]]->getName());
                  ?>
                  <h3 class="product-name">
                  <a href="<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>" title="<?php echo $pname; ?>">
                    <?php if(strlen($pname) > 28) {echo substr($pname,0,25)."...";}else {echo $pname;}?>
                    </a></h3>
                </div>
                <div class="image-box">
                <a class="product-image" href="<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>"> <img src="<?php echo $this->helper('catalog/image')->init($displayed_products[$random_products[$i]], 'small_image')->resize(140);?>" alt="<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>" title="<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>"/> </a>
                </div>
                <div class="cms-price-box" style=" text-align:center;">
                    <span class="regular-price" id="product-price-37">
                        <span class="price" ><?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->
     getCurrentCurrencyCode())->getSymbol().$displayed_products[$random_products[$i]]->getPrice(); ?></span>
                    </span>
                </div>
                <div class="button-row" style="text-align:center;">
                            <button onclick="setLocation('<?php echo $displayed_products[$random_products[$i]]->getProductUrl();?>')" class="button" type="button"><span><span><span>Details</span></span></span></button>
                    <button onclick="setLocation('<?php echo $this->getUrl('')."/checkout/cart/add?product=".$displayed_products[$random_products[$i]]->getId()."&qty=1"  ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
                </div>
              </div>
        </div></td>
<?php $k++; ?>
<?php endfor;?>
</tr>
      </table>
    </div>
  </div>
</div>
<?php endif;?>

现在通过添加以下代码在您的cms主页中调用阻止: -

{{block type="catalog/product_list" category_id="3" num_products="8" template="catalog/random-product.phtml"}}