0

I wonder if you can help me out. I've just created a customer Ajax module for a product selector.

It's been working fine locally, I've just pushed it LIVE and unfortunatley it's now stopped working.

Here is the code:

$result = $this->resultRawFactory->create();

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');

//Get filter values.
$categoryData = $this->getRequest()->getParam('whoisitfor');
if($categoryData) {
    $categoryData = $this->implodeFormValues($categoryData);
}
$userExperienceLevel = $this->getRequest()->getParam('user_experience_level');
$userDesiredPleasure = $this->getRequest()->getParam('desired_pleasure');
$userVibrating = $this->getRequest()->getParam('vibrating');
$userPowerSource = $this->getRequest()->getParam('power_source');
$userPrice = $this->getRequest()->getParam('price');


// Get category selected.
if(isset($categoryData)) {
    $categoryData = $this->expandFormValues($categoryData);
    $categories = [$categoryData];//category ids array
    $collection->addCategoriesFilter(['in' => $categories]);
}

// Filter on Experience
if(isset($userExperienceLevel)) {
    foreach($userExperienceLevel as $item) {
        $collection->addFieldToFilter('user_experience_level', array(
            array('finset' => array($item)),
        ));
    }
}

// Filter on Desired pleasure
if(isset($userDesiredPleasure)) {
    foreach($userDesiredPleasure as $item) {
        $collection->addFieldToFilter('desired_pleasure', array(
            array('finset' => array($item)),
        ));
    }
}

// Filter on Vibrating
if(isset($userVibrating)) {
    foreach($userVibrating as $item) {
        $collection->addFieldToFilter('vibrating', array(
            array('finset' => array($item)),
        ));
    }
}

// Filter on Power Source
if(isset($userPowerSource)) {
    foreach($userPowerSource as $item) {
        $collection->addFieldToFilter('power_source', array(
            array('finset' => array($item)),
        ));
    }
}

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager
$storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStore = $storeManager->getStore();

$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

$html = '<div class="row">';
foreach ($collection as $item) {
    $html .= '<div class="col-xs-12 col-sm-6 col-md-3">';
        $html .= '<div class="product-filter-block">';
            $html .= '<a href="'.$item->getProductUrl().'">';
                $html .= '<img src="'.$mediaUrl.'catalog/product'.$item->getThumbnail().'" />';
            $html .= '</a>';
            $html .= '<div class="product-filter-data row">';
                $html .= '<div class="product-filter-title col-xs-12"><h1>'.substr($item['name'], 0, 50).'</h1></div>';
                $html .= '<div class="product-filter-price col-xs-12">£'.number_format($item->getPrice(), 2).'</div>';
                $html .= '<div class="product-filter-view col-xs-12"><a href="'.$item->getProductUrl().'"><button>View Product</button></a></div>';
            $html .= '</div>';
        $html .= '</div>';
    $html .= '</div>';
}
$html .= '</div>';

$result->setContents($html);

return $result;

Like I said it's been working fine.

Filtering by category is still working but when you select a custom attribute it doesn't filter any further.

Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.