【问题标题】:Magento PHP: How do I set configurable attributes to a product?Magento PHP:如何为产品设置可配置属性?
【发布时间】:2016-10-28 19:55:56
【问题描述】:

我在为可配置产品设置可配置属性时遇到问题。
我的可配置和简单的产品正在管理端显示。
但是当我点击一个可配置的产品时,我看到了这个

我发现这个问题与我的类似
Magento 'Select Configurable Attributes' with PHP 但我仍然无法正确设置我的配置产品。



我正在调用我的类 MagentoProduct 并将“color_of_product”作为我的属性代码传递给我的“setConfigurableAttributesData”方法。
我稍后通过调用“save()”来保存产品。

class MagentoProduct {
private $product;
public function __construct ( ) {
    Mage::init();
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $this->product = Mage::getModel('catalog/product');
}



...
...
...


public function setConfigurableAttributesData($attribute_code){

    $super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attribute_code);

    $configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
    $newAttributes[] = array(
       'id'             => $configurableAtt->getId(),
       'label'          => $configurableAtt->getLabel(),
       'position'       => $super_attribute->getPosition(),

       //not 100% if values is correct, what do I set this to?
       'values'         => $configurableAtt->getPrices() ? $this->product->getPrices() : array(),
       'attribute_id'   => $super_attribute->getId(),
       'attribute_code' => $super_attribute->getAttributeCode(),
       'frontend_label' => $super_attribute->getFrontend()->getLabel(),
    );

    echo $configurableAtt->getId()."\n";
    echo $configurableAtt->getLabel()."\n";
    echo  $super_attribute->getPosition()."\n";

   //not 100% if values is correct, what do I set this to?
    $temp = $configurableAtt->getPrices() ? $this->product->getPrices() : array();
    echo $temp."\n";
    echo $super_attribute->getId()."\n";
    echo $super_attribute->getAttributeCode()."\n";
    echo $super_attribute->getFrontend()->getLabel()."\n";


    $this->product->setCanSaveConfigurableAttributes(true);
    $this->product->setConfigurableAttributesData($newAttributes);

}

...
...

public function save(){
    try{
        $this->product->save();
        }catch(Exception $e){
        echo $e->getMessage();
        Mage::log($e->getMessage());
    }   
}

}

我的 setConfigurableAttributesData 函数中的回声正在打印:

颜色
0
数组
176
产品颜色
颜色

我做错了什么?
我已经尝试调试了几个小时,但无法弄清楚。

【问题讨论】:

    标签: php magento


    【解决方案1】:

    如果您想使用 color_of_product 设置可配置产品,则必须将此属性设置为

    1) 全局

    2) 下拉菜单

    3) 适用于可配置产品和简单产品

    4) 设置是用于创建可配置产品。

    如果您想了解更多,您可以查看默认 magento 的 Color 属性。

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多