2016-07-18 13 views
0

管理パネルの商品エディタにカスタムタブを追加し、「最近の商品」または「上取り」のタブのように、 。view.phtml(Magento)で選択した商品のID-Sを取得

その後、view.phtmlで選択した製品のID-sを取得したいと考えています。そこから私のコードを作業します。

このコードに基づいてこのチュートリアルに従うとします。

インストーラスクリプトアプリ/コード/コミュニティ/ Inchoo/CustomLinkedProducts/SQL/inchoo_customlinkedproducts_setup /インストール-0.0.1.phpを

<?php 

$installer = $this; 

$data = array(
    array(
     'link_type_id' => Inchoo_CustomLinkedProducts_Model_Catalog_Product_Link::LINK_TYPE_CUSTOM, 
     'code'   => 'custom' 
    ) 
); 

foreach ($data as $bind) { 
    $installer->getConnection()->insertForce($installer->getTable('catalog/product_link_type'), $bind); 
} 

$data = array(
    array(
     'link_type_id'     => Inchoo_CustomLinkedProducts_Model_Catalog_Product_Link::LINK_TYPE_CUSTOM, 
     'product_link_attribute_code' => 'position', 
     'data_type'      => 'int' 
    ) 
); 

$installer->getConnection()->insertMultiple($installer->getTable('catalog/product_link_attribute'), $data); 

管理インターフェースアプリ/コード/コミュニティ/ Inchoo/CustomLinkedProducts /ブロック/ Adminhtml /カタログ/製品/編集/ Tab.php

<?php 

class Inchoo_CustomLinkedProducts_Block_Adminhtml_Catalog_Product_Edit_Tab 
extends Mage_Adminhtml_Block_Widget 
implements Mage_Adminhtml_Block_Widget_Tab_Interface 
{ 
    public function canShowTab() 
    { 
     return true; 
    } 

    public function getTabLabel() 
    { 
     return $this->__('Custom Linked Products'); 
    } 

    public function getTabTitle()   
    { 
     return $this->__('Custom Linked Products'); 
    } 

    public function isHidden() 
    { 
     return false; 
    } 

    public function getTabUrl() 
    { 
     return $this->getUrl('*/*/custom', array('_current' => true)); 
    } 

    public function getTabClass() 
    { 
     return 'ajax'; 
    }  
} 

コントローラアプリ/コード/コミュニティ/ Incho O/CustomLinkedProducts /コントローラ/ Adminhtml /カタログ/ ProductController.php

<?php 

require_once(Mage::getModuleDir('controllers','Mage_Adminhtml').DS.'Catalog'.DS.'ProductController.php'); 

class Inchoo_CustomLinkedProducts_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController 
{ 
    public function customAction() 
    { 
     $this->_initProduct(); 
     $this->loadLayout(); 
     $this->getLayout()->getBlock('catalog.product.edit.tab.custom') 
      ->setProductsCustom($this->getRequest()->getPost('products_custom', null)); 
     $this->renderLayout(); 
    } 

    public function customGridAction() 
    { 
     $this->_initProduct(); 
     $this->loadLayout(); 
     $this->getLayout()->getBlock('catalog.product.edit.tab.custom') 
      ->setProductsRelated($this->getRequest()->getPost('products_custom', null)); 
     $this->renderLayout(); 
    }  
} 

管理レイアウトアプリ/設計/ adminhtml /デフォルト/デフォルト/レイアウト/ inchoo_customlinkedproducts.xml

<?xml version="1.0" encoding="UTF-8"?> 

<layout> 

    <adminhtml_catalog_product_edit> 
     <reference name="product_tabs"> 
      <action method="addTab"> 
       <name>custom</name> 
       <block>inchoo_customlinkedproducts/adminhtml_catalog_product_edit_tab</block> 
      </action> 
     </reference> 
    </adminhtml_catalog_product_edit> 

    <adminhtml_catalog_product_custom> 
     <block type="core/text_list" name="root" output="toHtml"> 
      <block type="inchoo_customlinkedproducts/adminhtml_catalog_product_edit_tab_custom" name="catalog.product.edit.tab.custom"/> 
      <block type="adminhtml/widget_grid_serializer" name="custom_grid_serializer"> 
       <reference name="custom_grid_serializer"> 
        <action method="initSerializerBlock"> 
         <grid_block_name>catalog.product.edit.tab.custom</grid_block_name> 
         <data_callback>getSelectedCustomProducts</data_callback> 
         <hidden_input_name>links[custom]</hidden_input_name> 
         <reload_param_name>products_custom</reload_param_name> 
        </action> 
        <action method="addColumnInputName"> 
         <input_name>position</input_name> 
        </action> 
       </reference> 
      </block> 
     </block> 
    </adminhtml_catalog_product_custom> 

    <adminhtml_catalog_product_customgrid> 
     <block type="core/text_list" name="root" output="toHtml"> 
      <block type="inchoo_customlinkedproducts/adminhtml_catalog_product_edit_tab_custom" name="catalog.product.edit.tab.custom"/> 
     </block> 
    </adminhtml_catalog_product_customgrid> 

</layout> 

カスタムタブ app/code/community/Inchoo/CustomLinkedProducts/Block/Adminhtml/Catalog/Product/Edit/Tab/Custom.php

class Inchoo_CustomLinkedProducts_Block_Adminhtml_Catalog_Product_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Grid 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->setId('custom_product_grid'); 
     $this->setDefaultSort('entity_id'); 
     $this->setUseAjax(true); 
     if ($this->_getProduct()->getId()) { 
      $this->setDefaultFilter(array('in_products' => 1)); 
     } 
     if ($this->isReadonly()) { 
      $this->setFilterVisibility(false); 
     } 
    } 

    protected function _getProduct() 
    { 
     return Mage::registry('current_product'); 
    } 

    protected function _addColumnFilterToCollection($column) 
    { 
     // Set custom filter for in product flag 
     if ($column->getId() == 'in_products') { 
      $productIds = $this->_getSelectedProducts(); 
      if (empty($productIds)) { 
       $productIds = 0; 
      } 
      if ($column->getFilter()->getValue()) { 
       $this->getCollection()->addFieldToFilter('entity_id', array('in' => $productIds)); 
      } else { 
       if($productIds) { 
        $this->getCollection()->addFieldToFilter('entity_id', array('nin' => $productIds)); 
       } 
      } 
     } else { 
      parent::_addColumnFilterToCollection($column); 
     } 
     return $this; 
    } 

    protected function _prepareCollection() 
    { 
     $collection = Mage::getModel('catalog/product_link')->useCustomLinks() 
      ->getProductCollection() 
      ->setProduct($this->_getProduct()) 
      ->addAttributeToSelect('*'); 

     if ($this->isReadonly()) { 
      $productIds = $this->_getSelectedProducts(); 
      if (empty($productIds)) { 
       $productIds = array(0); 
      } 
      $collection->addFieldToFilter('entity_id', array('in' => $productIds)); 
     } 

     $this->setCollection($collection); 
     return parent::_prepareCollection(); 
    } 

    public function isReadonly() 
    { 
     return $this->_getProduct()->getCustomReadonly(); 
    } 

    protected function _prepareColumns() 
    { 
     if (!$this->isReadonly()) { 
      $this->addColumn('in_products', array(
       'header_css_class' => 'a-center', 
       'type'    => 'checkbox', 
       'name'    => 'in_products', 
       'values'   => $this->_getSelectedProducts(), 
       'align'    => 'center', 
       'index'    => 'entity_id' 
      )); 
     } 

     $this->addColumn('entity_id', array(
      'header' => Mage::helper('catalog')->__('ID'), 
      'sortable' => true, 
      'width'  => 60, 
      'index'  => 'entity_id' 
     )); 

     $this->addColumn('name', array(
      'header' => Mage::helper('catalog')->__('Name'), 
      'index'  => 'name' 
     )); 

     $this->addColumn('type', array(
      'header' => Mage::helper('catalog')->__('Type'), 
      'width'  => 100, 
      'index'  => 'type_id', 
      'type'  => 'options', 
      'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(), 
     )); 

     $sets = Mage::getResourceModel('eav/entity_attribute_set_collection') 
      ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId()) 
      ->load() 
      ->toOptionHash(); 

     $this->addColumn('set_name', array(
      'header' => Mage::helper('catalog')->__('Attrib. Set Name'), 
      'width'  => 130, 
      'index'  => 'attribute_set_id', 
      'type'  => 'options', 
      'options' => $sets, 
     )); 

     $this->addColumn('status', array(
      'header' => Mage::helper('catalog')->__('Status'), 
      'width'  => 90, 
      'index'  => 'status', 
      'type'  => 'options', 
      'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(), 
     )); 

     $this->addColumn('visibility', array(
      'header' => Mage::helper('catalog')->__('Visibility'), 
      'width'  => 90, 
      'index'  => 'visibility', 
      'type'  => 'options', 
      'options' => Mage::getSingleton('catalog/product_visibility')->getOptionArray(), 
     )); 

     $this->addColumn('sku', array(
      'header' => Mage::helper('catalog')->__('SKU'), 
      'width'  => 80, 
      'index'  => 'sku' 
     )); 

     $this->addColumn('price', array(
      'header'  => Mage::helper('catalog')->__('Price'), 
      'type'   => 'currency', 
      'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE), 
      'index'   => 'price' 
     )); 

     $this->addColumn('position', array(
      'header'   => Mage::helper('catalog')->__('Position'), 
      'name'    => 'position', 
      'type'    => 'number', 
      'validate_class' => 'validate-number', 
      'index'    => 'position', 
      'width'    => 60, 
      'editable'   => !$this->_getProduct()->getCustomReadonly(), 
      'edit_only'   => !$this->_getProduct()->getId() 
     )); 

     return parent::_prepareColumns(); 
    } 

    public function getGridUrl() 
    { 
     return $this->getData('grid_url') 
      ? $this->getData('grid_url') 
      : $this->getUrl('*/*/customGrid', array('_current' => true)); 
    } 

    protected function _getSelectedProducts() 
    { 
     $products = $this->getProductsCustom(); 
     if (!is_array($products)) { 
      $products = array_keys($this->getSelectedCustomProducts()); 
     } 
     return $products; 
    } 

    public function getSelectedCustomProducts() 
    { 
     $products = array(); 
     foreach (Mage::registry('current_product')->getCustomProducts() as $product) { 
      $products[$product->getId()] = array('position' => $product->getPosition()); 
     } 
     return $products; 
    }  
} 

アプリ/コード/コミュニティ/ Inchoo/CustomLinkedProducts /モデル/カタログ/製品/ Link.php

<?php 

class Inchoo_CustomLinkedProducts_Model_Catalog_Product_Link extends Mage_Catalog_Model_Product_Link 
{ 
    const LINK_TYPE_CUSTOM = 6; 

    public function useCustomLinks() 
    { 
     $this->setLinkTypeId(self::LINK_TYPE_CUSTOM); 
     return $this; 
    } 

    public function saveProductRelations($product) 
    { 
     parent::saveProductRelations($product); 

     $data = $product->getCustomLinkData(); 
     if (!is_null($data)) { 
      $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_CUSTOM); 
     } 
    } 
} 

アプリ/コード/コミュニティ/ Inchoo/CustomLinkedProducts /モデル/カタログ/ Product.php

<?php 

class Inchoo_CustomLinkedProducts_Model_Catalog_Product extends Mage_Catalog_Model_Product 
{ 
    public function getCustomProducts() 
    { 
     if (!$this->hasCustomProducts()) { 
      $products = array(); 
      $collection = $this->getCustomProductCollection(); 
      foreach ($collection as $product) { 
       $products[] = $product; 
      } 
      $this->setCustomProducts($products); 
     } 
     return $this->getData('custom_products'); 
    } 

    public function getCustomProductIds() 
    { 
     if (!$this->hasCustomProductIds()) { 
      $ids = array(); 
      foreach ($this->getCustomProducts() as $product) { 
       $ids[] = $product->getId(); 
      } 
      $this->setCustomProductIds($ids); 
     } 
     return $this->getData('custom_product_ids'); 
    } 

    public function getCustomProductCollection() 
    { 
     $collection = $this->getLinkInstance()->useCustomLinks() 
      ->getProductCollection() 
      ->setIsStrongMode(); 
     $collection->setProduct($this); 
     return $collection; 
    } 

    public function getCustomLinkCollection() 
    { 
     $collection = $this->getLinkInstance()->useCustomLinks() 
      ->getLinkCollection(); 
     $collection->setProduct($this); 
     $collection->addLinkTypeIdFilter(); 
     $collection->addProductIdFilter(); 
     $collection->joinAttributes(); 
     return $collection; 
    } 
} 

Fontendレイアウトアプリの\設計\フロントエンド\ RWD \デフォルト\

<?xml version="1.0" encoding="UTF-8"?> 

<layout> 

    <catalog_product_view translate="label"> 
     <reference name="right"> 
      <block type="inchoo_customlinkedproducts/catalog_product_list_custom" name="catalog.product.custom" before="-" template="inchoo/catalog/product/list/custom.phtml"/> 
     </reference> 
    </catalog_product_view>  

</layout> 

カスタムPHT layout.xml mlのアプリの\設計\フロントエンド\ RWDの\デフォルト\テンプレート\ inchooの\カタログ\製品\

<?php if($this->getItems()->getSize()): ?> 
<div class="block block-custom"> 
    <div class="block-title"> 
     <strong><span><?php echo $this->__('Custom Linked Products') ?></span></strong> 
    </div> 
    <div class="block-content"> 
     <p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or') ?>&nbsp;<a href="#" onclick="selectAllCustom(this); return false;"><?php echo $this->__('select all') ?></a></p> 
     <ol class="mini-products-list" id="block-custom"> 
     <?php foreach($this->getItems() as $_item): ?> 
      <li class="item"> 
       <?php if(!$_item->isComposite() && $_item->isSaleable()): ?> 
        <?php if (!$_item->getRequiredOptions()): ?> 
         <input type="checkbox" class="checkbox custom-checkbox" id="custom-checkbox<?php echo $_item->getId() ?>" name="custom_products[]" value="<?php echo $_item->getId() ?>" /> 
        <?php endif; ?> 
       <?php endif; ?> 
       <div class="product"> 
        <a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a> 
        <div class="product-details"> 
         <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p> 
         <?php echo $this->getPriceHtml($_item, true, '-custom') ?> 
         <?php if ($this->helper('wishlist')->isAllow()) : ?> 
          <a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a> 
         <?php endif; ?> 
        </div> 
       </div> 
      </li> 
     <?php endforeach ?> 
     </ol> 
     <script type="text/javascript">decorateList('block-custom', 'none-recursive')</script> 
    </div> 
    <script type="text/javascript"> 
    //<![CDATA[ 
    $$('.custom-checkbox').each(function(elem){ 
     Event.observe(elem, 'click', addCustomToProduct) 
    }); 

    var customProductsCheckFlag = false; 
    function selectAllCustom(txt){ 
     if (customProductsCheckFlag == false) { 
      $$('.custom-checkbox').each(function(elem){ 
       elem.checked = true; 
      }); 
      customProductsCheckFlag = true; 
      txt.innerHTML="<?php echo $this->__('unselect all') ?>"; 
     } else { 
      $$('.custom-checkbox').each(function(elem){ 
       elem.checked = false; 
      }); 
      customProductsCheckFlag = false; 
      txt.innerHTML="<?php echo $this->__('select all') ?>"; 
     } 
     addCustomToProduct(); 
    } 

    function addCustomToProduct(){ 
     var checkboxes = $$('.custom-checkbox'); 
     var values = []; 
     for(var i=0;i<checkboxes.length;i++){ 
      if(checkboxes[i].checked) values.push(checkboxes[i].value); 
     } 
     if($('custom-products-field')){ 
      $('custom-products-field').value = values.join(','); 
     } 
    } 
    //]]> 
    </script> 
</div> 
<?php endif ?> 
list.php

など\モジュール\ Inchoo_CustomLinkedProducts.xml

<?xml version="1.0" encoding="UTF-8"?> 

<config> 
    <modules> 
     <Inchoo_CustomLinkedProducts> 
      <active>true</active> 
      <codePool>community</codePool> 
     </Inchoo_CustomLinkedProducts> 
    </modules> 
</config> 

どのように非常に問題がありますview.phtmlで製品のID-sを取得します。私はすべてを試してみるが、うまくいかなかった。その後、私はMagentoの関連製品を、チュートリアルショーのようにinchoo_customrelatedproducts/catalog_product_list_custom内にクローンしましたが、動作しますが、関連製品は機能しませんでした。

このコードからview.phtmlで選択した製品のID-sを取得する方法はありますか?

+0

Stack Overflowは[プログラミング関連](http://stackoverflow.com/help/on-topic)のQ&Aサイトであるため、このトピックをオフトピックとして閉じることにしました。あなたの質問はプログラミングに関するものではありません。おそらくあなたは代わりにhttp://magento.stackexchange.comに投稿する必要がありますか? – Enigmativity

+0

商品リストでカスタムタブをプログラミングする最も簡単な方法があるかどうかを知りたいのですが。しかし、私はmagento.stackexchange.comでも尋ねる。 – googlar

+0

あなたはそれを聞かなかった。あなたは「カスタムタブを追加する」ように頼んだだけです。これがプログラミング関連の質問であれば、それを1にしてください。どのコードを変更する必要があるのか​​を理解し、どこにいるのかを示すことを示します。 – Enigmativity

答えて

0

新しい属性グループを作成してみてください。

+0

私は試してみましたが、商品リストは見つかりませんでした。 – googlar

関連する問題