X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   x5 - Finding the selected attribute in PHP (https://forum.x-cart.com/showthread.php?t=71894)

xgarb 04-23-2015 03:17 AM

x5 - Finding the selected attribute in PHP
 
I'm working on a module that creates a SKU dynamically. I need to find the currently selected options on the product page.

So far I have this in my View...

Code:

/**
 * @ListChild (list="product.details.page.info", zone="customer",  weight="5")
 */
 
class ClientPartCodeBuilder2 extends \XLite\View\Product\Details\Customer\Page\Main
{
 
    const PARAM_ATTRIBUTE_VALUES = 'attribute_values';

    protected function getAttributeValues()
    {
        $ids = array();
        $attributeValues = trim($this->getParam(static::PARAM_ATTRIBUTE_VALUES), ',');

        if ($attributeValues) {
            $attributeValues = explode(',', $attributeValues);
            foreach ($attributeValues as $v) {
                $v = explode('_', $v);
                $ids[$v[0]] = $v[1];
            }
        }

      return $this->getProduct()->prepareAttributeValues($ids);
    }


and in the template:

Code:

{foreach:getAttributeValues(),key,value}
+<br>
    Select ID: {key}<br>
    Option ID: {value.getID()}<br>


{foreach:value.attribute.attribute_options,key2,value2}
~~~Option Name ID : {value2.id}<br>
~~~Option Name : {value2.getName()}<br>
{end:}

+++<br><br>
{end:}


Which outputs this:

+
Select ID: 51
Option ID: 247
~~~Option Name ID : 117
~~~Option Name : flat
~~~Option Name ID : 118
~~~Option Name : metallic
+++

+
Select ID: 52
Option ID: 249
~~~Option Name ID : 119
~~~Option Name : normal
~~~Option Name ID : 120
~~~Option Name : big
+++

All very nice except I can't work out how to find which options are currently selected. I've tried various methods including working with

Code:

    /**
    * Return value is selected or not flag
    *
    * @param \XLite\Model\AttributeValue\AttributeValueSelect $value Value
    *
    * @return boolean
    */
    protected function isSelectedValue(\XLite\Model\AttributeValue\AttributeValueSelect $value)
    {
        $selectedIds = $this->getSelectedIds();

        return isset($selectedIds[$value->getAttribute()->getId()])
            ? $selectedIds[$value->getAttribute()->getId()] == $value->getId()
            : $value->isDefault();
    }

from \View\Product\AttributeValue\Customer\Select and other code but again.. I'm going round in circles.

Anyone any idea?

qualiteam 04-23-2015 08:26 AM

Re: x5 - Finding the selected attribute in PHP
 
I believe the method that retrieves attributes selected on the page is this one:
\XLite\View\Product\Details\Customer\Widget::getAt tributeValues()

Is it what you are looking for?

xgarb 04-23-2015 09:11 AM

Re: x5 - Finding the selected attribute in PHP
 
I've used that to get the options but I need to know which option values are currently selected.

joelfunk 10-20-2015 10:03 AM

Re: x5 - Finding the selected attribute in PHP
 
I'm working on a similar situation. Did you ever figure out the answer to this puzzle?

qualiteam 10-21-2015 11:17 PM

Re: x5 - Finding the selected attribute in PHP
 
Quote:

Originally Posted by joelfunk
I'm working on a similar situation. Did you ever figure out the answer to this puzzle?


What is the page and what is the class where you need information on the selected attributes?

joelfunk 10-22-2015 07:20 AM

Re: x5 - Finding the selected attribute in PHP
 
Quote:

Originally Posted by qualiteam
What is the page and what is the class where you need information on the selected attributes?


I needed this on the Product page so I could calculate a price based on Width, Length, and Height. After searching some other posts I cam up with a solution. Knowing the id of the attribute I wanted to check the value for (619-620) I found the following to work:

PHP Code:

$result = array();
$attrValues $this->getAttrValues();

if (!empty(
$attrValues) && \XLite\Model\Attribute::TYPE_TEXT != $this->getAttributeType()) {

    
$result = array();
    foreach (
$attrValues as $k => $attributeValue) {

        
$actualAttributeValue null;

        if (
$attributeValue instanceOf \XLite\Model\OrderItem\AttributeValue) {
            
$actualAttributeValue $attributeValue->getAttributeValue();
        } elseif (
$attributeValue instanceOf \XLite\Model\AttributeValue\AAttributeValue) {
            
$actualAttributeValue $attributeValue;
        } else {
            
$result[$k] = $attributeValue;
        }

        if (
$actualAttributeValue) {
            if (
$actualAttributeValue instanceOf \XLite\Model\AttributeValue\AttributeValueText) {
                
$value $actualAttributeValue->getValue();
            } else {
                
$value $actualAttributeValue->getId();
            }
            
$result[$actualAttributeValue->getAttribute()->getId()] = $value;
        }
    }

    
ksort($result);
}

$myWidth = \XLite\Core\Database::getRepo('\XLite\Model\AttributeValue\AttributeValueSelect')->find($result['619']);
$myLength = \XLite\Core\Database::getRepo('\XLite\Model\AttributeValue\AttributeValueSelect')->find($result['620']);
$myHeight = \XLite\Core\Database::getRepo('\XLite\Model\AttributeValue\AttributeValueSelect')->find($result['621']);

if (
is_object($myWidth->attribute_option) && is_object($myLength->attribute_option) && is_object($myHeight->attribute_option)) {
    
$myWidth = (int) $myWidth->attribute_option->getName();
    
$myLength = (int) $myLength->attribute_option->getName();
    
$myHeight = (int) $myHeight->attribute_option->getName();



xgarb 10-23-2015 12:29 AM

Re: x5 - Finding the selected attribute in PHP
 
Quote:

Originally Posted by joelfunk
I'm working on a similar situation. Did you ever figure out the answer to this puzzle?


I did mine in JavaScript in the end.


All times are GMT -8. The time now is 11:20 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.