我正在尝试使用已经添加的新自定义元素来获取所有运输表单的数组。该代码不起作用:
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
Woocommerce WC_Checkout方法:
访问https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#197-281了解更多信息! https://superuser.com?
我得到NULL,所以对象不存在
详细信息:
我正在开发一个网站,该网站的总价是根据某些城市的送货地区计算得出的。我决定在送货表格中创建一个自定义字段:
// Adding districts for the city of Lima on shipping form
add_filter('woocommerce_checkout_fields', 'custom_district_checkout_field');
function custom_district_checkout_field($fields) {
//the list for this example was shortened
$option_cities = array(
'' =>__('Select your district'),
'chorrillos' =>'Chorrillos',
'miraflores' =>'Miraflores'
};
$fields['shipping']['shipping_district']['type'] = 'select';
$fields['shipping']['shipping_district']['options'] = $option_cities;
$fields['shipping']['shipping_district']['class'] = array('update_totals_on_change');
$fields['shipping']['shipping_district']['input_class'] = array('wc-enhanced-select');
$fields['billing']['billing_district']['type'] = 'select';
$fields['billing']['billing_district']['options'] = $option_cities;
$fields['billing']['billing_district']['input_class'] = array('wc-enhanced-select');
wc_enqueue_js("jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {var select2_args = { minimumResultsForSearch:5};jQuery( this ).select2( select2_args ).addClass( 'enhanced' );})");
return $fields;
我可以确认custome字段正在运行。另一方面,我正在尝试更改WooCommerce Advance Shipping插件的工作方式,如Jeroen Sormani(开发人员)在他的博客中所述:
How the plugin works! 和WAS Shipping fields conditions
想法是将运输字段添加到条件列表中,插件默认显示以下字段:WC Advanced Shipping Fields by Default
目标是能够在条件中选择新创建的字段(例如:“ districts”),以便当用户选择正确的选项时价格将显示在购物车中,该插件已经具有列表不同地区的价格分别。但是,插件中有一个错误,因为该行不起作用(请检查Github中第一个函数内的WAS Shipping字段条件:
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
数周以来,我一直在努力解决这个问题,因此,本帖最初提出的要求。
/**
* WAS all checkout fields
*/
add_filter('was_conditions', 'was_conditions_add_shipping_fields', 10, 1);
function was_conditions_add_shipping_fields($conditions) {
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
debugToConsole($shipping_fields);
foreach ($shipping_fields as $key => $values) :
if (isset($values['label'])) :
$conditions['Shipping Fields'][$key] = $values['label'];
endif;
endforeach;
return $conditions;
}
上面的结果导致debugToConsole函数为NULL。