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)
-   -   Show product variant in list view instead of drop down (https://forum.x-cart.com/showthread.php?t=69791)

jp@ewaycorp.com 08-12-2014 07:45 AM

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.:-)

tony_sologubov 08-13-2014 12:03 PM

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.

jp@ewaycorp.com 08-20-2014 02:37 AM

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.

jp@ewaycorp.com 08-20-2014 01:56 PM

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>";


tony_sologubov 08-22-2014 03:15 AM

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


All times are GMT -8. The time now is 03:30 PM.

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