Jump to content

suntrop

Members
  • Posts

    316
  • Joined

Posts posted by suntrop

  1. HI all.

    I recently built a web shop with PW and it works pretty good. I love the simplicity and freedom working with templates and how stuff works and the client loves the back-end.

    However, the part I am unhappy about is the site search. Beside the API selectors work fine, it seems not be suitable for that kind of search. People search for words, phrases, they mix spaces, hyphens etc., add measurements … so a lot of searches end up in 0 results.

    I know of the ElasticSearch module and other external things like Sphider, Google CSE, algolia.com … but some are quite complicated and limit my control or bring google's ads on the site :) 

    So perhaps you have any improvements, ideas how to get a better search engine on my website. Most problems come from spelling issues, singular/plural, hyphenated words/searches. 

    <?php 
    
    $vars['q'] = $q;
    $input->whitelist('q', $q);
    $q = $sanitizer->selectorValue($q);
    
    
    
    // 1.
    // Item Number
    // If there is a number in it that matches a product's numbers, redirect to product directly
    $q_numeric           = preg_replace('/\D/', '', $q);
    $prodcut_item_number = $pages->get("template=product|product_variation, product_item_number=$q_numeric");
    
    if ( $prodcut_item_number->id && $prodcut_item_number->template == 'product') {
    	$session->redirect($prodcut_item_number->url);
    	die();
    }
    elseif ( $prodcut_item_number->id && $prodcut_item_number->template == 'product_variation') { // variations are saved as sub-pages, but have no public web-page
    	$session->redirect($prodcut_item_number->parent->url);
    	die();
    }
    
    
    
    // 2.
    // Product title and description
    // Search the product's title, headline and summary
    $matches = $pages->find("title|headline|summary~=$q, template=product, limit=50");
    
    
    
    // 3.
    // Categories
    // If search is a category, append all its products
    $categories = $pages->find("title%=$q, template=category, limit=10");
    if ( count($categories) ) {
    	$category_ids = $categories->each('id');
    	$category_ids = implode('|', $category_ids);
    	$append_products_from_category = $pages->find("template=product, has_parent={$cat_ids}");
    	$matches->append($append_products_from_category);
    }
    
    
    
    // 4.
    // Split search phrase
    // If nothing above matches, try each word separatly
    if( !count($matches) ) {
    	$q_separate = preg_replace('/\PL/u', ' ', $q); // "Remove" everything but letters 
    	$q_separate = preg_split('/\s+/', $q_separate);
    
    	foreach ($q_separate as $q_word) {
    		if ( $q_word != '' && strlen($q_word) > 4 ) {
    			$append_products_separate = $pages->find("title|headline|summary~=$q_word, template=product, limit=50");
    			$matches->append($append_products_separate);
    		}
    	}
    }
    
    // PLEASE NOTE – this doesn't come from my production code, but is a current idea I have
    // There is no highly need of weighting results and order best results on top, because there are just a few products for each keyword :-)

     

  2. Hi all. Perhaps this is just my fault, but I can't get behind it :-(

    When I store an array in a $session and want to read its contents – after a $session->redirect() – it is NULL by var_dump()

    If I use the exact same code and just store a string or a Page ID the output is correct.

    // file-1, product page
    $last_add_to_cart['product'] = $selected_product;
    $last_add_to_cart['quantity'] = $selected_product_quantity;
    $session->last_add_to_cart = $last_add_to_cart;
    
    $session->redirect('/warenkorb/');
    
    
    // file-2, cart
    var_dump($session->last_add_to_cart); // is  NULL

    Strangely this works 

    // file-1, product page
    $last_add_to_cart['product'] = $selected_product->id;
    #$last_add_to_cart['quantity'] = $selected_product_quantity;
    $session->last_add_to_cart = $last_add_to_cart;
    
    $session->redirect('/warenkorb/');
    
    
    // file-2, cart
    var_dump($session->last_add_to_cart); // is 1234

    EDIT: When I var_dump the $session before the redirect everything is ok

  3. Hi all

    All I could find was the opposite of what I need, how to unpublish a page via API. But I need to publish 500 pages. Those pages are children of/inside a PageTable field … don't know if this matters.

    Anyhow, I tried to loop through the child pages and publish them. But even one doesn't work … it just stays unpublished

    // Pagae 2603 is my page with 500 unpublished children 
    if ($page->id == 2603) {
    	$fcp = $page->children('status=unpublished')->first();
    	$fcp->removeStatus(Page::statusUnpublished);
    	$fcp->save;
    	// foreach ($page->children as $c) {
    	// 	…
    	// }
    }

    There is no such method like publishPage?

  4. Robin, thanks for that! Indeed this one works differently. It feels a bit off the road using the wire('pages') instead of wire('users), but that would work as expected.

    But again, this is quite against the "everything is a page" mantra in PW. However, I will include a status!=unpublished in the selectors.

    Thanks again! 

  5. I am wondering why this one 

    <?php
    	$tsubscribers = wire('users')->find('template=user');
    	foreach ($tsubscribers as $ts) {
    		echo $ts->email . '<br>';
    	}
    ?>

    will include unpublished users as well. The docs say https://processwire.com/api/selectors/#access_control

    Pages with hidden or unpublished status will not appear in the results from database-querying selectors that can return multiple pages (i.e. functions that return the PageArray type). For instance $pages->find("..."), $page->children(), etc.

    The site is running PW 2.7.2 … is this only new to PW 3?

    I had all users unpublished and see them as unpublished in the back-end. But when selecting users via API the unpublished are included as well. I have to add status!=unpublished to opt them out.

  6. I would like to handle Server-Error 410 (gone) the same way like PW handles 404. When I add to my .htaccess "ErrorDocument 410 /index.php" and create a /http410/ in the back-end that doesn't seem to be enough.

    While I could point it directly to /http410/ I am wondering if the other way is possible as well?

  7. The last days two new sites went online and both got a SSL certificate installed.

    But one has a HTTP problem, although both are configured the same (seem to be). When I run 

    curl -I -L https://www.example.com/

    (http://bit.ly/2pOdjGj)

    it gets redirected to itself again and again. I had a HTTPS redirect in my .htaaccess but have removed it. The home template was configured to use only HTTPS but is now configured to accept both. If I change that to HTTP only, I get and error.

    The origin must come from PW, because all files not processed by PW don't have the redirect problem.

    I had a look at the /site/assets/cache/ folder but don't know what to delete. And there is a cache table in the DB as well. Don't know either if I can just delete its entries.

    Apache 2.4.18 
    PHP 7.0.11
    PW 3.0.42
    No dedicated cache installed, no cache activated in PW

  8. I included the PW index.php in a script file and need to return a full URL of pages (autocomplete Ajax response)

    However, the file is located on another host and I get the wrong URL
    ajax.example.com/public/search_autocomplete.php

    I am wondering why ->url and ->httpUrl is /public/found/page/ or ajax.example.com/public/found/page/ instead of /found/page/ or example.com/found/page/ … and how to get the proper URL. Do I need to set the $config->httpHost again or something else?

    <?php namespace ProcessWire; 
    header('Access-Control-Allow-Origin: *'); 
    error_reporting(-1);
    include '../../../www/index.php';
    
    $q = wire('sanitizer')->selectorValue( wire('input')->get('q') );
    $results =  wire('pages')->find('title%=' . $q . ', template=basic-page, limit=10');
    
    $array['query'] = $q;
    foreach ($results as $result) {
    	$array['suggestions'][] = ['value' => $result->title, 'data' => $result->url];
    }
    
    echo json_encode($array);

     

  9. Thanks for your quick response! I looked in Captain Hook and saw …

    #1280: public function ___added(Page $page)

    … but didn't noticed it was in the Pages section. Had just seen Page $page. 

    Anyhow, the code above works great. I only need it for pages created in the CMS backend. 

    17 minutes ago, LostKobrakai said:

    if you add pages as part of any other page

    I guess you mean front-end pages (template), right? 

  10. Hi all. 

    I have to set default values for some fields (page select and text fields) on my page. I think the /site/templates/admin.php is the appropriate place? But I can't get anything to work from there. That is my current code, that looks to me like it should work. But it doesn't :-) 

    wire()->addHookAfter('Page::added', function($event) {
    	wire('page')->setAndSave('page_select_field', 1042);
    	wire('page')->setAndSave('text_field', 'Default Value');
    });
    
    require($config->paths->adminTemplates . 'controller.php');

     

  11. I cam across this issue a long time ago. When I create a page via API that page is always "created by" the guest user. 

    I tried to add a dedicated user or the admin, but had no luck. Perhaps that field is – like created date – not API-able. 

    $c = new Page();
    $c->createdUser = wire('users')->get('name=admin');

    Although I know that some fields won't be editable via API, I think this one is really confusing. For example, the first time I noticed this, was a client of mine, who nervously reported there is a protected page created by "someone from outside". It took me a while to get behind it :-D

  12. I am wondering if it is possible to hide a field depending on the PageTable field. 

    I would like to hide a field when the PageTable has any child-pages saved (or show only, if there are no pages saved). But I couldn't figure out how or if it is supported at all. 

    product_variations.count!=0  // … hides it in any case
    product_variations=''  // … hides it in any case

     

×
×
  • Create New...