doubi3996
2013-08-31 14:05
浏览 34
已采纳

Magento - 在下拉列表中显示所有类别

I am creating a simple script for updating all prices to all products within a specific category in Magento. I have the action code working and it updates the prices. Now I am making a form that has a dropdown with all the available categories in it then a text field that a percent is inputted. The problem I am having is I can not get the dropdown to populate the categories and I am going insane. I have tried other similar code and hacked to pieces other code to try to get it to work and it is driving me insane. Below is my current hacked up version - I know it is ugly and wrong but eh what can you do. Thanks for looking.

    <?php
require 'app/Mage.php';
Mage::app();
class Mymodule_Pup_Helper_Data extends Mage_Core_Helper_Abstract {
public function getCategoriesDropdown() {
$categoriesArray = Mage::getModel(‘catalog/category’)
->getCollection()
->addAttributeToSelect(‘name’)
->addAttributeToSort(‘path’, ‘asc’)
->addFieldToFilter(‘is_active’, array(‘eq’=>’1′))
->load()
->toArray();
foreach ($categoriesArray as $categoryId => $category) {
if (isset($category['name'])) {
$categories[] = array(
‘label’ => $category['name'],
‘level’  =>$category['level'],
‘value’ => $categoryId
);
}
}
return $categories;
}
}
?>
<html>
<body>
Update Prices by Category
<form action="pupped.php" method="post">
<select id="category-changer" name="cat" style="width:150px;">
<option value="">--Select Category--</option>
<?php
$_CategoryHelper = Mage::helper("pup")->getCategoriesDropdown();
foreach($_CategoryHelper as $value){
foreach($value as $key => $val){
if($key=='label'){
$catNameIs = $val;
}
if($key=='value'){
$catIdIs = $val;
}
if($key=='level'){
$catLevelIs = $val;
$b ='';
for($i=1;$i<$catLevelIs;$i++){
$b = $b."-";
}
}
}
?>
<option value="<?php echo $catIdIs; ?>"><?php echo $b.$catNameIs ?></option>
<?php
}
?>
</select>
Percent: <input type="text" name="per">
<input type="Submit">
</form>
</body>
</html>

图片转代码服务由CSDN问答提供 功能建议

我正在创建一个简单的脚本,用于更新Magento中特定类别中所有产品的所有价格。 我有行动代码工作,它更新价格。 现在我正在创建一个表单,其中包含所有可用类别的下拉列表,然后是一个输入百分比的文本字段。 我遇到的问题是我无法获得下拉列表来填充类别,我会疯了。 我已经尝试过其他类似的代码并且破解了其他代码,试图让它工作,这让我疯了。 下面是我目前的黑客攻击版本 - 我知道这是丑陋和错误但是呃你能做什么。 谢谢你的期待。

 &lt;?php 
require'app / Mage.php'; 
 
Mage :: app(); 
class Mymodule_Pup_Helper_Data扩展Mage_Core_Helper_Abstract {
public function getCategoriesDropdown  (){
 $ categoriesArray = Mage :: getModel('catalog / category')
-&gt; getCollection()
-&gt; addAttributeToSelect('name')
-&gt; addAttributeToSort('path','  asc')
-&gt; addFieldToFilter('is_active',array('eq'=&gt;'1'))
-&gt; load()
-&gt; toArray(); 
foreach($ categoriesArray as  $ categoryId =&gt; $ category){
if(isset($ category ['name'])){
 $ categories [] = array(
'label'=&gt; $ category ['name'],\  n'level'=&gt; $ category ['level'],
'value'=&gt; $ categoryId 
); 
} 
} 
return $ categories; 
} 
} 
?
&gt;  ; 
&lt; html&gt; 
&lt; body&gt; 
按类别更新价格
&lt; form action =“pupped.php”method =“post”&gt; 
&lt; select id =“category-changer”name =“cat”  style =“width:150px;”&gt; 
&lt; option value =“”&gt;  - 选择类别 - &lt; /选项&gt; 
&lt;?php 
 $ _CategoryHelper =  Mage :: helper(“pup”) - &gt; getCategoriesDropdown(); 
foreach($ _ CategoryHelper as $ value){
foreach($ value as $ key =&gt;  $ val){
if($ key =='label'){
 $ catNameIs = $ val; 
} 
if($ key =='value'){
 $ catIdIs = $ val; 
}  
if($ key =='level'){
 $ catLevelIs = $ val; 
 $ b =''; 
for($ i = 1; $ i&lt; $ catLevelIs; $ i ++){
 $ b  = $ b。“ - ”; 
} 
} 
} 
?&gt; 
&lt; option value =“&lt;?php echo $ catIdIs;?&gt;”&gt;&lt;?php echo $ b  。$ catNameIs?&gt;&lt; / option&gt; 
&lt;?php 
} 
?&gt; 
&lt; / select&gt; 
Percent:&lt; input type =“text”name =“per”&gt; 
&lt;  ; input type =“Submit”&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmou1964 2013-09-01 07:15
    已采纳

    below are the code get subcategory in drop down

    <select id="category" class="myinput-text required-entry widthinput" name="category">
    <?php
      $parentid=5; // parent id which you want sub category
      $categories=explode(',',Mage::getModel('catalog/category')->load($parentid)->getChildren());
      foreach($categories as $cat){ 
         $category=Mage::getModel('catalog/category')->load($cat);
    ?>
       <option value="<?php echo $category->getId();?>"><?php echo $category->getName();?></option>
    <?php } ?>
    </select>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dougan6402 2016-10-07 05:03
    You can use the following code to easily display all categories in the dropdown till level 3:
    

                    <div align="right" class"all_categories_list" style="display:inline-block;width:50%;">      
                        <form>
                            <select class="select" id="option" name="option" onChange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO" style="width: 25%;">
                                 <option value="*">All Categories</option>
                                        <?php $_helper = Mage::helper('catalog/category') ?>
                                        <?php $_categories = $_helper->getStoreCategories() ?>
                                        <?php $currentCategory = Mage::registry('current_category') ?>
                                        <?php if (count($_categories) > 0): ?><!-----------Level 1 ----->
                                        <?php foreach($_categories as $_category): ?>
                                 <option value="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></option> 
                                        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>  
                                        <?php $_subcategories = $_category->getChildrenCategories()?>
                                        <?php if (count($_subcategories) > 0):?><!-----------Level 2 ----->
                                        <?php foreach($_subcategories as $_subcategory):?>
                                <option value="<?php echo $_helper->getCategoryUrl($_subcategory)?>"><?php echo $_subcategory->getName() ?></option>  
                                        <?php $_subcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>    
                                        <?php $_subcategoriesss = $_subcategory->getChildrenCategories()?>
                                        <?php if (count($_subcategoriesss) > 0):?><!-----------Level 3 ----->
                                        <?php foreach($_subcategoriesss as $_subcategoryy):?>
                                <option value="<?php echo $_helper->getCategoryUrl($_subcategoryy)?>"><?php echo $_subcategoryy->getName() ?></option>  
                                        <?php endforeach; ?>
                                        <?php endif; ?>
                                        <?php endforeach; ?>
                                        <?php endif; ?>
                                        <?php endforeach; ?>
                                        <?php endif; ?>
                            </select>
                        </form>
                    </div>
    <!------------------------All categories and Subcategories level 3 in dropdown ---------------------------->
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题