【问题标题】:Magento 1.9 change custom option value depending on stock numberMagento 1.9 根据股票编号更改自定义选项值
【发布时间】:2019-08-16 08:12:20
【问题描述】:

我有自定义代码,用于确定是否存在自定义代码值以及是否存在选择此选项而不是产品配置中的默认排序顺序选项。

我想要做的是进一步添加逻辑,如果产品 sku 以“F”开头,那么它将显示该产品的默认排序顺序值。

自定义代码在这个文件中..

app\code\local\Mage\Catalog\Block\Product\View\Options\Type\Select.php

这是有问题的代码

            // Set Default for the following cases:
            // Ring Size for Men = T, Women = M
            // Price for Custom Option is $0 and is the last, unless defaulted by Men or Women.
            // if ($_value->getTitle() == 'M' && $isFemale) {
            //     $select->setValue($_value->getOptionTypeId());  
            // }
            // else if ($_value->getTitle() == 'T' && $isMale) {
            //     $select->setValue($_value->getOptionTypeId());  
            // } 
            // else if ($_value->getPrice() == 0.0000 && $hasNoPrice && !$isFemale && !$isMale) {
            //     $select->setValue($_value->getOptionTypeId());  
            // } 

            if ($_value->getTitle() == 'M') {
               $select->setValue($_value->getOptionTypeId());  
            }
            else if ($_value->getTitle() == 'T') {
                $select->setValue($_value->getOptionTypeId());  
            } 
        }

此代码原样正常。但我要添加到语句中的是,如果 Stock 代码以“F”开头,则分配默认值。

类似

if ($sku_code == 'F') { $select->setValue($configValue); }

这将显示选项的默认排序顺序值..

我似乎无法正常工作。

任何帮助编写此代码,您都会立即获得荣誉和良好的业力!

干杯

更新这是来自 Select.php 的完整代码

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Product options text type block
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Mage_Catalog_Block_Product_View_Options_Type_Select
    extends Mage_Catalog_Block_Product_View_Options_Abstract
{
    /**
     * Return html for control element
     *
     * @return string
     */
    public function getValuesHtml()
    {
        $_option = $this->getOption();
        $configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
        $store = $this->getProduct()->getStore();

        if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN
            || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE) {
            $require = ($_option->getIsRequire()) ? ' required-entry' : '';
            $extraParams = '';
            $select = $this->getLayout()->createBlock('core/html_select')
                ->setData(array(
                    'id' => 'select_'.$_option->getId(),
                    'class' => $require.' product-custom-option'
                ));
            if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN) {
                $select->setName('options['.$_option->getid().']');
                    // ->addOption('', $this->__('Please Select'));
            } else {
                $select->setName('options['.$_option->getid().'][]');
                $select->setClass('multiselect'.$require.' product-custom-option');
            }

            // This is a very specific case for Ring Size
            // $isMale = $isFemale = $hasNoPrice = false;
            // foreach ($_option->getValues() as $_value) {
            //     if ($_value->getTitle() == 'I' && $_value->getPrice() == 0.0000) {
            //         $isFemale = true;
            //     } 
            //     else if ($_value->getTitle() == 'M' && $_value->getPrice() == 0.0000) {
            //         $isMale = true;
            //     }

            //     if ($_value->getPrice() == 0.0000) {
            //         $hasNoPrice = true;
            //     }
            // } 

            foreach ($_option->getValues() as $_value) {
                $priceStr = $this->_formatPrice(array(
                    'is_percent'    => ($_value->getPriceType() == 'percent'),
                    'pricing_value' => $_value->getPrice(($_value->getPriceType() == 'percent'))
                ), false);
                $select->addOption(
                    $_value->getOptionTypeId(),
                    $_value->getTitle() . ' ' . $priceStr . '',
                    array('price' => $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false))
                );

                // Set Default for the following cases:
                // Ring Size for Men = T, Women = M
                // Price for Custom Option is $0 and is the last, unless defaulted by Men or Women.
                // if ($_value->getTitle() == 'M' && $isFemale) {
                //     $select->setValue($_value->getOptionTypeId());  
                // }
                // else if ($_value->getTitle() == 'T' && $isMale) {
                //     $select->setValue($_value->getOptionTypeId());  
                // } 
                // else if ($_value->getPrice() == 0.0000 && $hasNoPrice && !$isFemale && !$isMale) {
                //     $select->setValue($_value->getOptionTypeId());  
                // }

                //not sure about this line

                if ($sku_code[0] == 'F') {
                    $select->setValue($configValue);
                }

                else if ($_value->getTitle() == 'M') {
                   $select->setValue($_value->getOptionTypeId());  
                }
                else if ($_value->getTitle() == 'T') {
                    $select->setValue($_value->getOptionTypeId());  
                } 
            }

            if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE) {
                $extraParams = ' multiple="multiple"';
            }
            if (!$this->getSkipJsReloadPrice()) {
                $extraParams .= ' onchange="opConfig.reloadPrice()"';
            }
            $select->setExtraParams($extraParams);

            if ($configValue) {
                $select->setValue($configValue);
            } 

            return $select->getHtml();
        }

        if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO
            || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX
            ) {
            $selectHtml = '<ul id="options-'.$_option->getId().'-list" class="options-list">';
            $require = ($_option->getIsRequire()) ? ' validate-one-required-by-name' : '';
            $arraySign = '';
            switch ($_option->getType()) {
                case Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO:
                    $type = 'radio';
                    $class = 'radio';
                    if (!$_option->getIsRequire()) {
                        $selectHtml .= '<li><input type="radio" id="options_' . $_option->getId() . '" class="'
                            . $class . ' product-custom-option" name="options[' . $_option->getId() . ']"'
                            . ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"')
                            . ' value="" checked="checked" /><span class="label"><label for="options_'
                            . $_option->getId() . '">' . $this->__('None') . '</label></span></li>';
                    }
                    break;
                case Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX:
                    $type = 'checkbox';
                    $class = 'checkbox';
                    $arraySign = '[]';
                    break;
            }
            $count = 1;
            foreach ($_option->getValues() as $_value) {
                $count++;

                $priceStr = $this->_formatPrice(array(
                    'is_percent'    => ($_value->getPriceType() == 'percent'),
                    'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent')
                ));

                $htmlValue = $_value->getOptionTypeId();
                if ($arraySign) {
                    $checked = (is_array($configValue) && in_array($htmlValue, $configValue)) ? 'checked' : '';
                } else {
                    $checked = $configValue == $htmlValue ? 'checked' : '';
                }

                $selectHtml .= '<li>' . '<input type="' . $type . '" class="' . $class . ' ' . $require
                    . ' product-custom-option"'
                    . ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"')
                    . ' name="options[' . $_option->getId() . ']' . $arraySign . '" id="options_' . $_option->getId()
                    . '_' . $count . '" value="' . $htmlValue . '" ' . $checked . ' price="'
                    . $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false) . '" />'
                    . '<span class="label"><label for="options_' . $_option->getId() . '_' . $count . '">'
                    . $_value->getTitle() . ' ' . $priceStr . '</label></span>';
                if ($_option->getIsRequire()) {
                    $selectHtml .= '<script type="text/javascript">' . '$(\'options_' . $_option->getId() . '_'
                    . $count . '\').advaiceContainer = \'options-' . $_option->getId() . '-container\';'
                    . '$(\'options_' . $_option->getId() . '_' . $count
                    . '\').callbackFunction = \'validateOptionsCallback\';' . '</script>';
                }
                $selectHtml .= '</li>';
            }
            $selectHtml .= '</ul>';

            return $selectHtml;
        }
    }

}

【问题讨论】:

  • 你能把你的完整模板文件贴在你想要这样做的地方吗?
  • @giollianosulit 添加了来自 Select.php 的代码。这是添加默认值的地方。希望这会有所帮助。

标签: magento product magento-1.9 option


【解决方案1】:

可能我没理解对,但你想看看 sku_code 是否以“f”开头?

编辑:

if ($sku_code == 'F') { 
    $select->setValue($configValue); 
}

试试:

if (strpos($sku_code, 'F') === 0) { 
    $select->setValue($configValue); 
}

或许:

if (substr($sku_code, 0, 1) === 'F') { $select->setValue($configValue); }

希望对你有帮助:)

【讨论】:

  • 是的,正确..想看看是否以 f 开头,在这种情况下显示默认值。你在正确的轨道上,它确实可以工作,但它现在正在为所有 sku 代码应用默认值。不仅仅是以 F.. 开头的那些。
猜你喜欢
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
相关资源
最近更新 更多