以下の手順に従って、スーパー属性の価格を変更してください
最初にオブザーバー「catalog_product_get_final_price」を使用します。オブザーバーを次のようにします。
モジュールconfig.xmlを開き、以下のコードを使用します。
<events>
<catalog_product_get_final_price>
<observers>
<Setblue_Banner_Model_Observer>
<type>singleton</type>
<class>Setblue_Banner_Model_Observer</class>
<method>getFinalPrice</method>
</Setblue_Banner_Model_Observer>
</observers>
</catalog_product_get_final_price>
</events>
Observer.phpファイルをモデルと過去のコードで作成します
<?php
class Setblue_Banner_Model_Observer
{
public function getFinalPrice(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$product = $event->getProduct();
$qty = $event->getQty();
$selectedAttributes = array();
if ($product->getCustomOption('attributes')) {
Mage::log('yes-----', null, 'confPricing.log');
$selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());
}
if (sizeof($selectedAttributes)) return $this->getSimpleProductPrice($qty, $product);
}
public function getSimpleProductPrice($qty=null, $product)
{
$cfgId = $product->getId();
$product->getTypeInstance(true)
->setStoreFilter($product->getStore(), $product);
$attributes = $product->getTypeInstance(true)
->getConfigurableAttributes($product);
$selectedAttributes = array();
if ($product->getCustomOption('attributes')) {
$selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());
}
$db = Mage::getSingleton('core/resource')->getConnection('core_read');
$dbMeta = Mage::getSingleton('core/resource');
$sql = <<<SQL
SELECT main_table.entity_id FROM {$dbMeta->getTableName('catalog/product')} `main_table` INNER JOIN
{$dbMeta->getTableName('catalog/product_super_link')} `sl` ON sl.parent_id = {$cfgId}
SQL;
foreach($selectedAttributes as $attributeId => $optionId) {
$alias = "a{$attributeId}";
$sql .= ' INNER JOIN ' . $dbMeta->getTableName('catalog/product') . "_int" . " $alias ON $alias.entity_id = main_table.entity_id AND $alias.attribute_id = $attributeId AND $alias.value = $optionId AND $alias.entity_id = sl.product_id";
}
$id = $db->fetchOne($sql);
//Mage::log(Mage::getModel("catalog/product")->load($id)->getFinalPrice($qty), null, 'confPricing.log');
//return
$fp = Mage::getModel("catalog/product")->load($id)->getFinalPrice($qty);
return $product->setFinalPrice($fp);
}
}
?>
app / design / frontend / default / yourtheme / template / catalog / product / view / type / options / configurable.phtmlを開き、ファイル内の任意の場所に以下のコードを貼り付けます
<ul class="productIds" style="display:none;">
<?php
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$_productObj = Mage::getModel('catalog/product')->load($child->getId());
?>
<li id='simple_<?php echo $child->getId(); ?>'><?php echo Mage::helper('core')->currency($_productObj->getFinalPrice()); ?></li>
<?php
}
?>
</ul>
js / varien / configurable.jsを開き、reloadPrice関数を以下のように変更するか、この関数全体を置き換えることもできます
reloadPrice: function(){
if (this.config.disablePriceReload) {
return;
}
var price = 0;
var oldPrice = 0;
for(var i=this.settings.length-1;i>=0;i--){
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if(selected.config){
price += parseFloat(selected.config.price);
oldPrice += parseFloat(selected.config.oldPrice);
}
}
/* Edit Code By Chandresh Rana*/
//optionsPrice.changePrice('config', {'price': price, 'oldPrice': oldPrice});
optionsPrice.reload();
var existingProducts = new Object();
for(var i=this.settings.length-1;i>=0;i--)
{
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if(selected.config)
{
for(var iproducts=0;iproducts<selected.config.products.length;iproducts++)
{
var usedAsKey = selected.config.products[iproducts]+"";
if(existingProducts[usedAsKey]==undefined)
{
existingProducts[usedAsKey]=1;
}
else
{
existingProducts[usedAsKey]=existingProducts[usedAsKey]+1;
}
}
}
}
for (var keyValue in existingProducts)
{
for ( var keyValueInner in existingProducts)
{
if(Number(existingProducts[keyValueInner])<Number(existingProducts[keyValue]))
{
delete existingProducts[keyValueInner];
}
}
}
var sizeOfExistingProducts=0;
var currentSimpleProductId = "";
for ( var keyValue in existingProducts)
{
currentSimpleProductId = keyValue;
sizeOfExistingProducts=sizeOfExistingProducts+1
}
if(sizeOfExistingProducts==1)
{
if($('product-price-'+this.config.productId)){
$('product-price-'+this.config.productId).innerHTML = jQuery("#simple_"+currentSimpleProductId).html();
}
}
// End Code By Chandresh Rana
return price;
if($('product-price-'+this.config.productId)){
$('product-price-'+this.config.productId).innerHTML = price;
}
this.reloadOldPrice();
},
取得元コード:http : //chandreshrana.blogspot.in/2016/03/set-simple-product-price-instead-of.html