Jump to content

ProductController : set Custom Field value via url Get


Recommended Posts

Hi, I have designed ProductController:initContent to allow get variable to set product custom field value. Problem is that sometimes the custom field value is not saved to the (sometimes newly created) shopping cart.. Probable cookie issue on private navigation ? 

 

code sample describe the following behaviour

 

1. ProductController::getMyCustomFieldId() identify custom field id (to allow this possibility on many products that have that custom field , identified by its name, id depends on product)

2. ProductController::initContent() : create cart if does not exist, then use native function to set custom field value in that cart. THEN only after that call parent:initContent()... Note that removing the "cookie test" (pasted from native ProductController::241) solved many occurences of the bug ... (//&& isset($_COOKIE[$this->context->cookie->getName()])  

any idea experts ? Thks

public function getMyCustomFieldId()
    {
        $myCustomFieldId=null;
        $customization_fields = $this->product->getCustomizationFields($this->context->language->id);
        if (is_array($customization_fields)) {
            foreach ($customization_fields as $customization_field) {
                {
                    if ( $customization_field['name']== "field_name") $myCustomFieldId=$customization_field['id_customization_field'];
                }
            }}
        return $myCustomFieldId;
    }
 public function initContent() 
    {
   $myCustomFieldId=$this->getMyCustomFieldId();
   if ($_GET['mycustomvar'] && $myCustomFieldId) {

            // If cart has not been saved, we need to do it so that customization fields can have an id_cart
            // We check that the cookie exists first to avoid ghost carts
            if (!$this->context->cart->id //&& isset($_COOKIE[$this->context->cookie->getName()])
                ) {
                $this->context->cart->add();
                $this->context->cookie->id_cart = (int) $this->context->cart->id;
            }

             $this->context->cart->addTextFieldToProduct($this->product->id, $myCustomFieldId, Product::CUSTOMIZE_TEXTFIELD,$_GET['mycustomvar']);
        }

        if ($myCustomFieldId) {
            if (!$this->context->cookie->id_cart) $this->context->cart->deleteCustomizationToProduct((int)$this->product->id, $myCustomFieldId);
        
        }

        parent::initContent();
    }

 

Edited by breizhwave (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More