如何在Woocommerce中检查产品是否具有特定的产品属性 - php

我想确定产品是否具有属性。例如:

if (product has attribute 'pa_color')
{
    //do something
}

我该怎么做?

参考方案

您可以通过以下方式简单地使用WC_Product方法get_attribute()

// (If needed) Get an instance of the WC_Product Object from the product ID
$product = wc_get_product( $product_id );

// Get the product attribute value(s)
$color = $product->get_attribute('pa_color');

// if product has attribute 'pa_color' value(s)
if( ! empty( $color ) ){
    // do something
} else {
    // No product attribute is set for this product
}

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

更改默认的URL PHP - php

如何更改默认网址。例如www.example.com/index.php-> www.example.com现在,我要将其设置为www.example.com/test.php。我应该在php.ini中进行更改吗? 参考方案 假设您正在使用apache,则可以通过DirectoryIndex指令执行此操作。Check out the docs。

PHP:将字符串拆分为字母和数字部分的最佳方法 - php

我有几个格式的字符串AA11 AAAAAA1111111 AA1111111 分离字符串的字母和数字部分的最佳方法(最有效)? 参考方案 如果它们都是一系列字母,然后是一系列数字,并且没有非字母数字字符,那么sscanf()可能比regexp更有效$example = 'AAA11111'; list($alpha,$numeric) =…