预填充的WooCommerce签出字段:下拉字段问题

时间:2019-05-13 作者:Debbie Kurth

我的插件中有一小段代码,允许用户声明列表。我只需单击一个按钮,就可以声明列表,重点是希望让用户尽可能轻松地完成此过程。此按钮调用一个函数调用,将产品添加到购物车,预先填写结帐字段并将其自动带到结帐处。

我根据服务器上的数据预先填充字段。

问题是,虽然文本输入字段显示得很好,但下拉字段框却没有。具体而言,州和国家。即使框中有匹配的值。我甚至检查大小写,强制使用大写,加上硬编码。它只是不会选择下拉框,需要这样做。

我错过了什么?

代码如下:

function mmd_list_claimlisting($DBRecordId)
{
    global $woocommerce;
    $Record = mmd_list_GetDBRecord($DBRecordId);

    $User      = get_user_by( \'email\', $Record[\'Email\'] );            
    $FirstName = $User->first_name;
    $LastName  = $User->last_name;
    $UserId    = $User->ID;
    $MonthlyListingId = mmd_list_readDBSettings("MONTHLY_LISTING_PRODUCT");

    // Get List Id by name
    $CatagoryName       = get_the_title( $Record[\'ListId\'] );

    // match to variation by name
    $SubscriptionProduct = new WC_Product_Variable($MonthlyListingId);
    $VariationList       = $SubscriptionProduct->get_available_variations();
    foreach ($VariationList as $Variation) 
    {
        $VarationName = $Variation[\'attributes\'][\'attribute_list-category\'];    
        if (strcmp($CatagoryName, $VarationName) ==0 ) 
        {
            // Add List Id as product attribute
            $VariationId         = $Variation[\'variation_id\'];
            WC()->cart->add_to_cart( get_product($MonthlyListingId), 1, $VariationId ); // Add Claim to cart

            $billing_fields  = get_option( \'wc_fields_billing\', array() );

            $woocommerce->session->set(\'claim_state\', $Record[\'BusinessState\']);
            $woocommerce->session->set(\'claim_country\', $Record[\'BusinessCountry\']);

            $checkout_url = wc_get_checkout_url();
            wp_safe_redirect($checkout_url);

            return;
        }
    }

    // No matching variation found
    return; 
}

////////////////////////////////////////////////////////////////////
//  Pre-fill out the check out fields with the listing data
///////////////////////////////////////////////////////////////////
function FillInCheckOutFields($fields)
{
    global $woocommerce;    

    $claim_state = $woocommerce->session->get(\'claim_state\');
    if(!is_null($claim_state))
        $fields[\'billing\'][\'physical-state\'][\'placeholder\']  = $claim_state;    

    $claim_country = $woocommerce->session->get(\'claim_country\');
    if(!is_null($claim_country))
        $fields[\'billing\'][\'physical-country\'][\'placeholder\']  = $claim_country;    

    return $fields; 
}
add_filter( \'woocommerce_checkout_fields\' , \'FillInCheckOutFields\', 10, 1);

1 个回复
SO网友:Debbie Kurth

找到了我自己的解决方案。关键是使用不同的回调和引用woocommerces的实际字段名。已将下面的代码简化为基本内容。

 function mmd_list_claimlisting($DBRecordId)
  {
    global $woocommerce;
    $Record = mmd_list_GetDBRecord($DBRecordId);

    $woocommerce->session->set(\'claim_state\', $Record[\'BusinessState\']);
    $woocommerce->session->set(\'claim_country\', $Record[\'BusinessCountry\']);

     // No matching variation found
  }

 ////////////////////////////////////////////////////////////////////
 //  Pre-fill out the check out fields with the listing data
 ///////////////////////////////////////////////////////////////////
 function FillInCheckOutFields($fields)
    {
     global $woocommerce;    


      switch($input)
           {
           case  \'physical-state\':
              $claim_state = $woocommerce->session->get(\'claim_state\');
              if(!is_null($claim_state))
                 return $claim_state;   
            break;

           case  \'physical-state\':
              $claim_country= $woocommerce->session->get(\'claim_country\');
              if(!is_null($claim_country))
                return $claim_country;  
            break;

          } 

    return $fields; 
  }
 add_filter( \'woocommerce_checkout_get_value\', \'populating_checkout_fields\', 10, 2 );   

相关推荐