在WordPress上> Woocommerce购物车页面,我正在使用产品变量。我希望显示标签而不是变体的值。
这是值:
["_gravity_form_lead"]=>
array(9) {
[3]=>
string(1) "6"
[2]=>
string(1) "6"
[5]=>
string(1) "1"
[6]=>
string(4) "1.25"
[7]=>
string(1) "0"
[8]=>
string(4) "2.85"
["1.1"]=>
string(11) "Total Price"
["1.2"]=>
string(6) "$10.24"
["1.3"]=>
string(1) "1"
}
但是,例如,[5]=>string(1) "1"
代替1
,我应该拥有名为“Single Sided
”的变体标签。
是否有任何功能可以帮助我列出每个分离的产品变体而不是分组,因此我可以完全控制它们列出有关变体的详细信息?
答案 0 :(得分:2)
$form_id = RGFormsModel::get_form_id('Form name'); // replace Form name with your form name
$form = GFFormsModel::get_form_meta($form_id);
$field = GFFormsModel::get_field($form, ##); // ## Is the id, so 5 or "1.3"
if(is_array(rgar($field, "inputs"))){ // For the "1.1" etc ID's
foreach($field["inputs"] as $input){
if ( $input['id'] == "##" ) { // ## Is the id, so "1.1", "1.2" etc..
$label = $input['label'];
}
}
} else {
$label = GFFormsModel::get_label($field);
}
如果ID为“1.1”,“1.2”,“1.3”,它们是分组ID,则get_label将返回组名称,就像您拥有名称和名字,姓氏一样。它将返回Name。检查is_array将为您提供标签名称,例如“名字”。
代码方面应该改进,比如循环遍历你拥有的字段并执行上面的代码,但我假设你会知道如何编写代码,
答案 1 :(得分:0)
根据上述答案,我想出了我需要的东西。希望这也有助于他人。非常感谢 Peter van der !
// get form fields individually
echo '<dl class="variation">';
foreach ($woocommerce->cart->cart_contents as $cart_key => $cart_item_array) {
$form = GFFormsModel::get_form_meta($cart_item_array['_gravity_form_data']['id']);
// height
$height = GFFormsModel::get_field($form, '3');
foreach($height['choices'] as $choice) {
if($choice['value'] == $cart_item_array['_gravity_form_lead']['3']) {
echo '<dt>Height:</dt> <dd>'.$choice['text'].'</dd>';
}
}
// width
$width = GFFormsModel::get_field($form, '2');
foreach($width['choices'] as $choice) {
if($choice['value'] == $cart_item_array['_gravity_form_lead']['2']) {
echo '<dt>Height:</dt> <dd>'.$choice['text'].'</dd>';
}
}
// printing
$printing = GFFormsModel::get_field($form, '5');
foreach($printing['choices'] as $choice) {
if($choice['value'] == $cart_item_array['_gravity_form_lead']['5']) {
echo '<dt>Printing:</dt> <dd>'.$choice['text'].'</dd>';
}
}
// lamination
$lamination = GFFormsModel::get_field($form, '6');
foreach($lamination['choices'] as $choice) {
if($choice['value'] == $cart_item_array['_gravity_form_lead']['6']) {
echo '<dt>Lamination:</dt> <dd>'.$choice['text'].'</dd>';
}
}
// quantity
$quantity = GFFormsModel::get_field($form, '8');
foreach($quantity['choices'] as $choice) {
if($choice['value'] == $cart_item_array['_gravity_form_lead']['8']) {
echo '<dt>Quantity:</dt> <dd>'.$choice['text'].'</dd>';
}
}
}
echo '</dl>';