当前位置: 代码迷 >> Web前端 >> Magento Custom Options Templates (完善)
  详细解决方案

Magento Custom Options Templates (完善)

热度:1043   发布时间:2013-09-18 14:17:40.0
Magento Custom Options Templates (完美)

Step 1: change the app/design/adminhtml/default/default/template/page/head.phtml with the attached head.phtml

Step 2: app/design/adminhtml/default/default/template/catalog/product/edit/options.phtml 
before: 

<div class="entry-edit-head">

insert: 
<div id="coSamples" style="margin-bottom:15px;"></div>

and

after: 

<script type="text/javascript">
insert: 
initOptionsSamples();

Step 3: app/design/adminhtml/default/default/template/catalog/product/edit/options/type/select.phtml 
find: 

OptionTemplateSelectRow = ...

replace with: 

OptionTemplateSelectRow = '<tr id="product_option_{{id}}_select_{{select_id}}">'+
            '<td>'+
            '<input type="hidden" name="product[options][{{id}}][values][{{select_id}}][option_type_id]" value="{{option_type_id}}">'+
            '<input type="hidden" id="product_option_{{id}}_select_{{select_id}}_is_delete" name="product[options][{{id}}][values][{{select_id}}][is_delete]" value="">'+
            '<input type="text" class="required-entry input-text select-type-title" id="product_option_{{id}}_select_{{select_id}}_title" name="product[options][{{id}}][values][{{select_id}}][title]" value="{{title}}">{{checkboxScopeTitle}}</td>'+
            '<td><input type="text" class="input-text validate-number product-option-price" id="product_option_{{id}}_select_{{select_id}}_price" name="product[options][{{id}}][values][{{select_id}}][price]" value="{{price}}"></td>'+
            '<td><?php echo $this->getPriceTypeSelectHtml() ?>{{checkboxScopePrice}}</td>'+
            '<td><input type="text" class="input-text sku" name="product[options][{{id}}][values][{{select_id}}][sku]" value="{{sku}}"></td>'+
            '<td><input type="text" class="validate-zero-or-greater input-text sort-order" name="product[options][{{id}}][values][{{select_id}}][sort_order]" value="{{sort_order}}"></td>'+
            '<td class="last"><span title="Delete row"><?php echo $this->getDeleteButtonHtml() ?></span></td>'+
        '</tr>';

in the head.phtml, you can find the customOptionsSamples variable where you can set up your own custom options templates

Image Attachments
Screen shot 2010-01-13 at 1.25.52 PM.pngScreen shot 2010-01-13 at 1.26.23 PM.png
File Attachments
 head.phtml  (File Size: 9KB - Downloads: 320)



方法二

function setCustomOption($productId,   
                $title,   
                array $optionData,   
                array $values = array()) {  
                  
    Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);  
    if (!$product = Mage::getModel('catalog/product')->load($productId)) {  
        throw new Exception('Can not find product: ' . $productId);  
    }  
      
    $defaultData = array(  
        'type'          => 'field',  
        'is_require'    => 0,  
        'price'         => 0,  
        'price_type'    => 'fixed',  
    );  
      
    $data = array_merge($defaultData,   
                        $optionData,   
                        array('product_id' => (int)$productId,  
                        'title' => $title,  
                        'values' => $values));  
      
    $product->setHasOptions(1)->save();                                       
    $option = Mage::getModel('catalog/product_option')->setData($data)  
                    ->setProduct($product)->save();  
    return $option;  
}  



$productop = Mage::registry('current_product');
$_product = Mage::getModel('catalog/product')->load($productop->getId());
$optionexist=count($_product->getOptions());
$casetemplate=$_product->getResource()->getAttribute('pattern_template')->getFrontend()->getValue($_product);
if($optionexist==0){
	
	
	if($casetemplate=='Small'){
	$sku=$_product->getResource()->getAttribute('sku')->getFrontend()->getValue($_product);

	$options =  array('type' => 'drop_down',  
            'is_require' => 1,  
            'price' => 0,  
            'price_type' => 'fixed');  
			
		$values = array(  
            array(  
                'title' => 'Black',  
                'price' =>0.00,  
                'price_type' => 'fixed',  
                'sku' => $sku,
                'sort_order' => 1  
            ),  
   
            array(  
                'title' => 'Brown',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 2  
            ),  
   
             array(  
                'title' => 'Red',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 3  
            ),
			array(  
                'title' => 'Petal Pink',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 4  
            ),
			array(  
                'title' => 'White',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 5  
            ),
			array(  
                'title' => 'Aqua',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 6  
            ),
			array(  
                'title' => 'Purple',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 7  
            ),
			array(  
                'title' => 'Black/Red Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 8  
            ),
			array(  
                'title' => 'Black/Orange Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 9  
            ),
			array(  
                'title' => 'Black/Green Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 10  
            ),
			array(  
                'title' => 'Black/Purple Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 11  
            )
         );  
		
		setCustomOption($productop->getId(), 'Pattern', $options, $values); 
		echo "Generate Small Case Successful";
	}else if($casetemplate=='Big'){

	$sku=$_product->getResource()->getAttribute('sku')->getFrontend()->getValue($_product);

	$options =  array('type' => 'drop_down',  
            'is_require' => 1,  
            'price' => 0,  
            'price_type' => 'fixed');  
		$values = array(  
            array(  
                'title' => 'Black',  
                'price' =>0.00,  
                'price_type' => 'fixed',  
                'sku' => $sku,
                'sort_order' => 1  
            ),  
   
            array(  
                'title' => 'Brown',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 2  
            ),  
   
             array(  
                'title' => 'Red',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 3  
            ),
			array(  
                'title' => 'Purple',  
                'price' => 0.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 4  
            ),
			array(  
                'title' => 'Black/Red Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 5  
            ),
			array(  
                'title' => 'Black/Orange Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' =>6  
            ),
			array(  
                'title' => 'Black/Green Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 7  
            ),
			array(  
                'title' => 'Black/Purple Stitch',  
                'price' => 2.00,  
                'price_type' => 'fixed',  
                'sku' => '',  
                'sort_order' => 8  
            )
         );  
   
	
		setCustomOption($productop->getId(), 'Pattern', $options, $values); 
		echo "Generate Big Case Successful";
	}else{
		echo "Pattern is Empty, Nothing to do";
	}
}else{
	if($casetemplate!='Big' || $casetemplate!='Small'){
		foreach ($productop->getOptions() as $opt) {
			$opt->delete(); 
		}
		$productop->setHasOptions(0)->save();
		echo "Delete options";
	}else{
		echo "Nothing to do";
	}
}



  相关解决方案