0

I have a small piece of code in my plugin that allows a user to claim a listing. With a focus of wanting to make the process as easy as possible for the user, I have a single button click, allowing the listing to be claimed. This button calls a function call that adds the product to the cart, pre-fills out the checkout fields and takes them to the check out automatically.

I pre-populate the fields according to the data I have on the server.

The problem is, while the text input fields display wonderfully, the drop down field boxes do not. Specifically, State and Country. Even with the matching values within the box. I even check case and forced upper case, plus hard coded. It just will not select the drop down box and it needs to.

WHAT AM I MISSING?

Here is the code:

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);
  • Please check your Caps Lock key, it seems to be stuck. – user3135691 May 13 '19 at 7:55
0

Found my own solution. The key was to use a different callback and reference woocommerces actual field name. Have simplified the code below down to the essence.

 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 );   
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.