如何在Magento产品页面上显示自定义选项?

时间:2013-03-18 14:44:37

标签: php magento magento-1.7

我有一个定制的Magento产品页面显示,因此没有颜色,尺寸等的选择器。

在产品详情中 - >自定义选项,我已经制作了颜色选项,现在如果我尝试购买该产品,则需要选择颜色,但现在没有下拉。 我也看不到产品页面中没有选择该选项的错误,我只能在我访问购物车时看到它,但是它显示错误。

所以我需要的是:

1.如何从产品本身的自定义选项[下拉列表]中显示自定义选项“颜色”。 2.在产品页面上显示错误,例如“产品缺货或未选择颜色选项”。

提前致谢!

更新:

我得到它使用此代码但是当我想添加到购物车仍然说选项未被选中,它有什么问题?

    $productSku = $_product->getSku();
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );

$product = Mage::getModel("catalog/product")->load($productId);

$attVal = $product->getOptions();

$optStr = "";

// loop through the options
foreach($attVal as $optionKey => $optionVal) {

//$optStr .= "<br/>";

//$optStr .= $optionVal->getTitle().": ";

$optStr .= "<select style='display:block; clear:both;' name='options[".$optionVal->getId()."]'>";

foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
$optStr .= "<option value='".$valuesVal->getId()."'>".$valuesVal->getTitle()."</option>";
}

$optStr .= "</select>";

}

echo($optStr );

更新Amit: var_dump($attVal);

array(1) {
  [1]=>
  object(Mage_Catalog_Model_Product_Option)#469 (19) {
    ["_product":protected]=>
    object(Mage_Catalog_Model_Product)#74 (32) {
      ["_cacheTag":protected]=>
      string(15) "catalog_product"
      ["_eventPrefix":protected]=>
      string(15) "catalog_product"
      ["_eventObject":protected]=>
      string(7) "product"
      ["_canAffectOptions":protected]=>
      bool(false)
      ["_typeInstance":protected]=>
      NULL
      ["_typeInstanceSingleton":protected]=>
      NULL
      ["_linkInstance":protected]=>
      NULL
      ["_customOptions":protected]=>
      array(0) {
      }
      ["_urlModel":protected]=>
      NULL
      ["_errors":protected]=>
      array(0) {
      }
      ["_optionInstance":protected]=>
      object(Mage_Catalog_Model_Product_Option)#286 (19) {
        ["_product":protected]=>
        NULL
        ["_options":protected]=>
        array(0) {
        }
        ["_valueInstance":protected]=>
        NULL
        ["_values":protected]=>
        array(0) {
        }
        ["_eventPrefix":protected]=>
        string(13) "core_abstract"
        ["_eventObject":protected]=>
        string(6) "object"
        ["_resourceName":protected]=>
        string(22) "catalog/product_option"
        ["_resource":protected]=>
        NULL
        ["_resourceCollectionName":protected]=>
        string(33) "catalog/product_option_collection"
        ["_cacheTag":protected]=>
        bool(false)
        ["_dataSaveAllowed":protected]=>
        bool(true)
        ["_isObjectNew":protected]=>
        NULL
        ["_data":protected]=>
        array(0) {
        }
        ["_hasDataChanges":protected]=>
        bool(false)
        ["_origData":protected]=>
        NULL
        ["_idFieldName":protected]=>
        NULL
        ["_isDeleted":protected]=>
        bool(false)
        ["_oldFieldsMap":protected]=>
        array(0) {
        }
        ["_syncFieldsMap":protected]=>
        array(0) {
        }
      }
      ["_options":protected]=>
      *RECURSION*
      ["_reservedAttributes":protected]=>
      NULL
      ["_isDuplicable":protected]=>
      bool(true)
      ["_calculatePrice":protected]=>
      bool(true)
      ["_defaultValues":protected]=>
      array(1) {
        ["url_path"]=>
        string(47) "charger.html"
      }
      ["_storeValuesFlags":protected]=>
      array(1) {
        ["url_path"]=>
        bool(true)
      }
      ["_lockedAttributes":protected]=>
      array(0) {
      }
      ["_isDeleteable":protected]=>
      bool(true)
      ["_isReadonly":protected]=>
      bool(false)
      ["_resourceName":protected]=>
      string(15) "catalog/product"
      ["_resource":protected]=>
      NULL
      ["_resourceCollectionName":protected]=>
      string(26) "catalog/product_collection"
      ["_dataSaveAllowed":protected]=>
      bool(true)
      ["_isObjectNew":protected]=>
      NULL
      ["_data":protected]=>
      array(71) {
        ["entity_id"]=>
        string(1) "9"
        ["entity_type_id"]=>
        string(1) "4"
        ["attribute_set_id"]=>
        string(1) "4"
        ["type_id"]=>
        string(6) "simple"
        ["sku"]=>
        string(6) "123456"
        ["has_options"]=>
        string(1) "1"
        ["required_options"]=>
        string(1) "1"
        ["created_at"]=>
        string(19) "2012-10-19 15:15:38"
        ["updated_at"]=>
        string(19) "2013-03-18 14:19:05"
        ["color"]=>
        NULL
        ["status"]=>
        string(1) "1"
        ["visibility"]=>
        string(1) "4"
        ["enable_googlecheckout"]=>
        string(1) "1"
        ["tax_class_id"]=>
        string(1) "4"
        ["featured"]=>
        string(1) "0"
        ["news_from_date"]=>
        NULL
        ["news_to_date"]=>
        NULL
        ["special_from_date"]=>
        string(19) "2012-10-17 00:00:00"
        ["special_to_date"]=>
        string(19) "2013-04-30 00:00:00"
        ["custom_design_from"]=>
        NULL
        ["custom_design_to"]=>
        NULL
        ["enjoyby"]=>
        NULL
        ["name"]=>
        string(42) "Charger"
        ["meta_title"]=>
        NULL
        ["meta_description"]=>
        NULL
        ["image"]=>
        string(34) "/3/1/31gzr5vwlwl._sl500_aa300_.jpg"
        ["small_image"]=>
        string(34) "/3/1/31gzr5vwlwl._sl500_aa300_.jpg"
        ["thumbnail"]=>
        string(34) "/3/1/31gzr5vwlwl._sl500_aa300_.jpg"
        ["url_key"]=>
        string(42) "charger"
        ["url_path"]=>
        string(47) "charger.html"
        ["custom_design"]=>
        NULL
        ["page_layout"]=>
        NULL
        ["options_container"]=>
        string(10) "container2"
        ["image_label"]=>
        NULL
        ["small_image_label"]=>
        NULL
        ["thumbnail_label"]=>
        NULL
        ["country_of_manufacture"]=>
        NULL
        ["msrp_enabled"]=>
        string(1) "2"
        ["msrp_display_actual_price_type"]=>
        string(1) "4"
        ["gift_message_available"]=>
        NULL
        ["starttime"]=>
        string(8) "11:59 PM"
        ["time"]=>
        string(8) "11:59 PM"
        ["target_number"]=>
        string(1) "1"
        ["percustomer"]=>
        string(1) "0"
        ["for_friend"]=>
        string(1) "0"
        ["dealcity"]=>
        string(5) "3,4,5"
        ["dealcategory"]=>
        string(1) "6"
        ["customeremail"]=>
        string(18) "test@test.com"
        ["questions"]=>
        string(10) "7185696643"
        ["customer_website"]=>
        string(16) "Electronics"
        ["merchant"]=>
        string(1) "1"
        ["price"]=>
        string(7) "19.9900"
        ["special_price"]=>
        string(6) "0.9900"
        ["weight"]=>
        string(6) "0.2000"
        ["msrp"]=>
        NULL
        ["description"]=>
        string(914) "

2 个答案:

答案 0 :(得分:3)

<强> [编辑]

使用以下代码获取自定义选项

<?php foreach($_product->getOptions() as $options) :?>
    <?php if ($options->getType() == 'drop_down'): ?>
        <select class="required-entry product-custom-option" id="select_<?php echo $options->getOptionId() ?>" name="options[<?php echo $options->getOptionId() ?>]">
            <option value="">-- Please Select --</option>
            <?php foreach ($options->getValues() as $value): ?>
                <option value="<?php echo $value['option_type_id']; ?>"><?php echo $value['title']; ?></option>
            <?php endforeach; ?>
        </select>
    <?php endif; ?>
<?php endforeach; ?>

您还可以根据选项类型

进行修改

答案 1 :(得分:0)

           <?php if ($_product->isSaleable() && $this->hasOptions()):?>


           <?php echo $this->getChildChildHtml('container2', '', true, true) ?>



           <?php endif;?>