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

Show product variant in list view instead of drop down

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 08-12-2014, 07:45 AM
 
jp@ewaycorp.com jp@ewaycorp.com is offline
 

Member
  
Join Date: Aug 2009
Posts: 23
 

Lightbulb Show product variant in list view instead of drop down

Hello Everyone & Specially Tony , i have been using x-cart 5 for my new project and quite like working on it.

Now i have to show product variant in list view instead of drop down list along with their name, price, sku, qty, Add to cart button.

My Product consist of "ARTIST/AUTHOR" And "MEDIA TYPE" Attribute.

Now my variant are based on "MEDIA TYPE" Attribute.

I have seen one of your example and created this

PHP Code:
<?php

namespace XLite\Module\EwayCorp\EdactSkin\View\Product\Details\Customer\Page;

abstract class 
APage extends \XLite\View\Product\Details\Customer\Page\APage implements \XLite\Base\IDecorator
{
    protected function 
getArtistAttributes() 
    { 
        
$attributesToDisplay = array ('ARTIST/AUTHOR'); 

        
$return = array(); 

        
$widgets $this->getAttributesWidgets(); 

        foreach (
$widgets as $widget) { 

            foreach (
$widget->getAttrList() as $attribute) { 

                if (
in_array(strtoupper($attribute['name']), $attributesToDisplay)) { 

                    
$return[] = $attribute

                } 

            } 

        } 

        return 
$return
    }  
    protected function 
getMediaTypeAttributes() 
    { 
        
$attributesToDisplay = array ('MEDIA TYPE'); 

        
$return = array(); 

        
$widgets $this->getAttributesWidgets(); 

        foreach (
$widgets as $widget) { 

            foreach (
$widget->getAttrList() as $attribute) { 

                if (
in_array(strtoupper($attribute['name']), $attributesToDisplay)) { 

                    
$return[] = $attribute

                } 

            } 

        } 

        return 
$return
    } 
}

I get getArtistAttributes() values as CSV for multiple values for authors/artists which is fine.

When i call getMediaTypeAttributes() i get only the names in CSV where want variant information like price, sku, qty etc..

Please help on how to achieve this and thanks in advance.
Reply With Quote
  #2  
Old 08-13-2014, 12:03 PM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default Re: Show product variant in list view instead of drop down

Hi!

This is an example of how to pull info about variants:

PHP Code:
<?php

require_once 'top.inc.php';

$product = \XLite\Core\Database::getRepo('\XLite\Model\Product')->find(22);

$variants $product->getVariants();
foreach (
$variants as $variant) {
    echo 
'qty ' . (($variant->defaultAmount) ? $product->getQty() : $variant->amount) . 
    
'; price ' .  (($variant->defaultPrice) ? $product->price $variant->price) . '<br /><br />';
}

This is a PHP script that must be put into X-Cart 5's root folder.

Hopefully, it will help.

If there is further help needed, please explain a bit what result you are trying to achieve. I still did not grasp it. Do you want to list product attributes on product lists (like category pages) or something different?

Tony.
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
Reply With Quote
  #3  
Old 08-20-2014, 02:37 AM
 
jp@ewaycorp.com jp@ewaycorp.com is offline
 

Member
  
Join Date: Aug 2009
Posts: 23
 

Default Re: Show product variant in list view instead of drop down

Thanks Tony your answer really helped a lot.
Another simple thing i want to ask. Now i want variant attribute values ID how do i get this information.

I can get variant id using
PHP Code:
$variant->getId(); 



Also, I cannot do var_dump() or print_r() from the views just for debugging purpose how can i do those.

Debugging through var/run/ codes helps me now to not rebuild application so often.
Reply With Quote
  #4  
Old 08-20-2014, 01:56 PM
 
jp@ewaycorp.com jp@ewaycorp.com is offline
 

Member
  
Join Date: Aug 2009
Posts: 23
 

Thumbs up Re: Show product variant in list view instead of drop down

I have figured out how to do it on my own.
PHP Code:
protected function getVariantsInfo() 
    {

        
$product $this->getProduct();
        
//echo $id = $product->getProductId();
        
        
$variants $product->getVariants(); 

        foreach (
$variants as $variant) {
            
$v_info=array();
            
$v_info['quantity']=(($variant->defaultAmount) ? $product->getQty() : $variant->amount);
            
$v_info['price']=(($variant->defaultPrice) ? $product->price $variant->price);
            
$v_info['sku']=(($variant->sku) ? $product->sku $variant->sku);
            
$v_info['product_id'] = $product->getProductId();
            
$v_info['category_id'] = $product->getCategoryId();
            
$v_info['attr_group_id'] = "62"// Your select box ID
            
foreach($variant->getAttributeValues() as $attribute)
            {
                
$v_info['media_type']= trim($attribute->getAttributeOption()->getName());
                
$v_info['attribute_id'] = $attribute->getId();
            }
            
$variant_info[]=$v_info;
        }
        return 
$variant_info
    } 

In Template File

Code:
{foreach:getVariantsInfo(),a} <li> <span class="icon"><img src="images/cd-icon.png" alt="cd-icon"></span> <span class="details-with-sku">{product.name:h} ({a.media_type})<br/><p>SKU: {a.sku}</p></span> <span class="buy-now"> ${a.price}<br/> <form action="{buildURL(#cart#)}" method="post" accept-charset="utf-8"> <div class="form-params" style="display: none;"> <input type="hidden" name="target" value="cart"> <input type="hidden" name="action" value="add"> <input type="hidden" name="product_id" value="{a.product_id}"> <input type="hidden" name="category_id" value="{a.category_id}"> <input type="hidden" name="attribute_values[{a.attr_group_id}]" value="{a.attribute_id}"> <input type="hidden" name="amount" value="1"> <input type="hidden" name="returnURL" value="{product.cleanURL}"> </div> <input class="add_to_cart_mediatype" type="submit" value="" /> </form> </span> <div class="clearfix"></div> </li> {end:}

Now you can have a list of variant with buy now button and clicking on it will add the specific variant to your cart.

I hope this would help someone who is trying to figure out this kind of requirement.

Also print_r is done like this

Code:
echo "<pre>"; \Doctrine\Common\Util\Debug::dump($entity); echo "</pre>";
Reply With Quote

The following 2 users thank jp@ewaycorp.com for this useful post:
sysp (09-07-2016), tony_sologubov (08-22-2014)
  #5  
Old 08-22-2014, 03:15 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default Re: Show product variant in list view instead of drop down

Hi Jwala!

Thank you for sharing the code and happy to hear that you managed to apply the solution you need.

Tony
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
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 03:20 PM.

   

 
X-Cart forums © 2001-2020