Woocommerce PHP

Woocommerce PHP

我的 WooCommerce 结帐中有很多自定义字段,我试图在管理页面上显示它们。

字段的数量因用户而异,因此我需要计算名称“$order-billing_date_of_birth”有多少个字段。

$order-billing_date_of_birth1
$order-billing_date_of_birth2
$order-billing_date_of_birth3

...等等

我想也许我可以使用这样的循环。

for ($x = 0; $x <= 100; $x++) {
    echo '<p><strong>'.__('Date of birth').'</strong>: ' . $order->billing_date_of_birth.$x . '</p>';
}

我的问题是:如何将 "billing_date_of_birth" 和 "$x" 这两个组合起来作为 "$order->billing_of_birth1"。

可能还有更好的方法来解决这个问题。

以下是您需要做的

for ($x = 0; $x <= 100; $x++) {
    $prop = "billing_date_of_birth".$x ;
    echo '<p><strong>'.__('Date of birth').'</strong>: ' . $order->$prop . '</p>';
}

这是实现要求的示例代码。

将对象转换为数组,然后找到元素。

<?php
$object = new StdClass;
$object->foo1 = 'Hey';
$object->bar1 = 'Friend';
$array_obj=(array) $object;
//var_dump( $array_obj );

$bar='bar';
$num=1;

echo ($array_obj[$bar.$num]);

?>

尝试类似的方法,应用于 class。

例如 列表组项目 1 列表组项目 2 列表组项目 3

使用此代码

jQuery(function($) {
$('.list-group-item').each(function(i) {
   $(this).attr('class',"list-group-item"+i);
});
});