你如何编写基于WooCommerce属性的条件PHP


How do you write conditional PHP based on WooCommerce attribute

我已经在互联网上搜索了高低,寻找一种在某些页面上有条件地显示WooCommerce产品属性数据的方法。

例如:如果属性 somethingcool 有一个值并且它在/?filtering=1&filter_manufacturer=1648 页面上,则显示值

如果属性位于特定的过滤页面上,我想以不同的方式显示属性

前任:http://www.colorselectcoil.com/color-match-tool/?filtering=1&filter_manufacturer=1648

根据此过滤页面,显示产品属性"很酷的东西">

<?php if ( is_product_attribute('somethingcool') ) {
    echo 'Hi! This is something cool?';}
    else {
    echo '';
  }
?>

如果它是一个普通的 WordPress 页面,而不是一个过滤页面,我可以根据 body 类标签隐藏和显示,但不幸的是,body 类不会根据查询字符串 url 而改变。

您可以使用 url 搜索字符串并从中提取您想要的任何部分。例如,如果您只想知道您是否在过滤页面上,那么您就不会费心检查制造商。

   <?php if ( $product->get_attribute('Certainteed') && intval($_GET['filtered']) == 1 && intval($_GET['filter_manufacturer']) == 1648 ) {
         echo 'Hi! This is something cool?';
         }
   ?>

intval()位应该足以防止注入。

如果你想找到 uri 的一部分,你可以使用如下的东西:

  if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && is_product_attribute('somethingcool') ){
  echo 'Hi! This is something cool!';
  }

检查查看根据您的示例返回的echo is_product_attribute('Everlast');

$_GET...按名称(位于方括号中(获取搜索字符串中的变量。

      <?php if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1694 && is_product_attribute('Everlast') ){
             echo 'Hi! This is something cool!';  
             } ?>
一次

只用一个项目尝试,并逐步掌握它的窍门。

加载有关在字符串中查找字符串的内容 如何检查字符串是否包含 PHP 中的特定单词?

如果需要,获取和回显产品属性的其他方法是:Woocommerce获取自定义属性

我不确定这是否有效,但它可能具有足够的适应性。

您可以将过滤器值传递$ff您想要$ffv的过滤器整数值(可能与 1 不同(,$fm$filter_manufacturer整数值$fmv - 是您正在寻找的值 - 是作为您的示例 1648 然后产品数组$product,您的$collection变量和 $aiw 是文本形式的"我想要的属性",但您也可以以变量。

将此功能放在您的functions.php

 function attribute_i_want( $ff,$ffv, $fm, $fmv, $prod, $coll, $aiw){
      if( $ff == 1 && $fm == $fmv && $collection = $prod->get_attribute($aiw) ){
           echo '<div class="attribute-titles">. $aiw . ' name: ';
           echo ($coll = $prod->get_attribute($aiw) ) ? $collection : __('', 'woocommerce'); echo '</div>';
      }
 }
 add_action( 'wp_enqueue_scripts', 'attribute_i_want' );

并用传递给它的这个小地段来称呼它

 <?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), 1648, $product, $coll, 'Certainteed'); ?>

我假设您发布的代码正在正常工作。

查看您的页面,我看不到它如何从下拉列表中获取值,因为它没有名称或 ID - 理想情况下在 onchange 事件onchange="setManufacturer()";中 - 我也找不到,但相信它是一个事件列表脚本 - 你会使用这样的东西来设置一个隐藏变量,该变量获取下拉文本值并将其用于函数调用中的插槽,您将在其中当前必须手动输入文本,例如示例中的"Certainteed":

    <script language=JavaScript>
    function setManufacturer(){
    var manufacturerName = manufacturerDropdown.options[manufacturerName.selectedIndex].innerHTML;
    documentGetElementById('submittedManufacturerName').value = manufacturerName; 
    }
    </script>

这将获取从选择下拉列表中选择的文本 - 这将需要 ID "制造商名称"并将该文本值插入到隐藏输入的值中,PHP 将能够在 PHP 函数调用中选取和使用。

onchange 事件将触发 JavaScript 并提交页面,php 将从隐藏的输入中获取文本值,这就是放入函数调用的内容:

  <input type="hidden" id ="submittedManufacturerName" name="submittedManufacturerName" value = "" />
 <?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), $manufacturerName, $product, $coll, $submittedManufacturerName); ?>

除非有任何其他方法可以在PHP变量中获取制造商名称的值 - 这可能是可能的,因为制造商名称确实出现在页面下拉菜单上方链接的其他地方,因此它可能已经作为PHP变量存在,您可以按原样使用。这样,您的函数将是完全自动的,而无需此额外的JavaScript。该值显示在现有网页上名为"移除过滤器"的a href中。

$manufacturerName将是下拉列表中实际值中提交的值,收集为$manufacturerName = htmlspecialchars($_GET['manufacturerDropdown']);

史蒂夫把它挂上了,并帮助我弄清楚如何基于过滤器编写条件,这解决了我的问题。谢谢史蒂夫!

<?php if( intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1648 && $collection == $product->get_attribute('Certainteed') ){ echo '<div class="attribute-titles">Certainteed name: '; echo ($collection == $product->get_attribute('Certainteed') ) ? $collection : __('', 'woocommerce'); echo '</div>'; } ?>