Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

x5 - Finding the selected attribute in PHP

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 04-23-2015, 03:17 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default 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?
__________________
Core version: 5.5.xx
Reply With Quote
  #2  
Old 04-23-2015, 08:26 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default 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?
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #3  
Old 04-23-2015, 09:11 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default 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.
__________________
Core version: 5.5.xx
Reply With Quote
  #4  
Old 10-20-2015, 10:03 AM
 
joelfunk joelfunk is offline
 

Newbie
  
Join Date: Aug 2015
Posts: 2
 

Default 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?
__________________
5.2.6
Reply With Quote
  #5  
Old 10-21-2015, 11:17 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default 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?
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #6  
Old 10-22-2015, 07:20 AM
 
joelfunk joelfunk is offline
 

Newbie
  
Join Date: Aug 2015
Posts: 2
 

Default 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();

__________________
5.2.6
Reply With Quote

The following user thanks joelfunk for this useful post:
qualiteam (10-25-2015)
  #7  
Old 10-23-2015, 12:29 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default 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.
__________________
Core version: 5.5.xx
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 11:55 PM.

   

 
X-Cart forums © 2001-2020