Search code examples
x-cart

XCART-5 Get Attributes Values in programming and Assigning some other values progmatically


I am working on Xcart-5 website customization. And I created my own module and doing work on that. I just created some Global Attributes (" As a Plain text ") field and assign these attributes to some product. Now I want to access these fields value in programming in the product details page for assigning some other value programatically at run time.

How can I achieve this task. Kindly provide me the solution .


Solution

  • In your module you should decorate the \XLite\Model\Attribute class and extend the getAttributeValue() method there.

    For instance, if I use a module with developer ID Tony and module ID AttributesDemo, then I would need to create the XCartDirectory/classes/XLite/Module/Tony/AttributesDemo/Model/Attribute.php file with the following content:

    <?php
    // vim: set ts=4 sw=4 sts=4 et:
    
    namespace XLite\Module\Tony\AttributesDemo\Model;
    
    /**
     * Attribute
     * @MappedSuperClass
     */
    abstract class Attribute extends \XLite\Model\AttributeAbstract implements \XLite\Base\IDecorator
    {
        public function getAttributeValue(\XLite\Model\Product $product, $asString = false)
        {
            $result = parent::getAttributeValue($product, $asString);
    
            if (!$asString) {
                foreach ($result as $obj) {
                    if ($obj->asString() == 'Mac') {
                        $obj->getAttributeOption()->setName('Windows');
                    }
                }
            }
    
            return $result;
        }
    }
    

    Such implementation will change Mac values to Windows ones in all attributes.