1

I have some custom attributes set in the customer information in the admin.

I have a page setup with checkboxes which if checked should set these attributes to yes or no.

I'm struggling with setting the values after the checkbox has been clicked.

Here's my code:

var_dump($customer->getCustomAttribute('new_products_attr'));
?>
<?= $block->getChildHtml('form_before') ?>
<form class="form form-newsletter-manage" action="<?= $block->escapeUrl($block->getAction()) ?>" method="post" id="form-validate">
    <fieldset class="fieldset">
        <?= $block->getBlockHtml('formkey') ?>
        <div class="customCheckbox">
            <label for="subscription" class="control control--checkbox">
                Newsletter subscription
                <input type="checkbox" name="is_subscribed" id="subscription" value="1" title="<?= $block->escapeHtmlAttr(__('General Subscription')) ?>"<?php if ($block->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
                <div class="control__indicator"></div>
            </label>
            <p>Lorem ipsum dolor sit amet,
            consectetuer adipiscing elit, sed
            diam nonummy nibh euismod
            tincidunt ut laoreet dolore</p>
        </div>
        <div class="newProducts customCheckbox">
            <label class="control control--checkbox">
                New products
                <input type="checkbox" name="newProducts" id="new_products" <?php if($customer->getCustomAttribute('new_products_attr') == true):?> checked="checked" <?php endif; ?>/>
                <div class="control__indicator"></div>
            </label>
            <p>Lorem ipsum dolor sit amet,
            consectetuer adipiscing elit, sed
            diam nonummy nibh euismod
            tincidunt ut laoreet dolore</p>
        </div>
        <div class="exclusiveOffers customCheckbox">
            <label class="control control--checkbox">
                Exclusive offers
                <input type="checkbox" name="exclusiveOffers" />
                <div class="control__indicator"></div>
            </label>
            <p>Lorem ipsum dolor sit amet,
            consectetuer adipiscing elit, sed
            diam nonummy nibh euismod
            tincidunt ut laoreet dolore</p>
        </div>
        <div class="discountsAndSales customCheckbox">
            <label class="control control--checkbox">
                Discounts and sales
                <input type="checkbox" name="discountsAndSales" />
                <div class="control__indicator"></div>
            </label>
            <p>Lorem ipsum dolor sit amet,
            consectetuer adipiscing elit, sed
            diam nonummy nibh euismod
            tincidunt ut laoreet dolore</p>
        </div>
        <?php /* Extensions placeholder */ ?>
        <?= $block->getChildHtml('customer.form.newsletter.extra') ?>
    </fieldset>
    <div class="actions-toolbar">
        <div class="primary"><button type="submit" title="<?= $block->escapeHtmlAttr(__('Save')) ?>" class="action save primary"><span><?= $block->escapeHtml(__('Save')) ?></span></button></div>
        <div class="secondary"><a class="action back btn" href="<?= $block->escapeUrl($block->getBackUrl()) ?>"><span><?= $block->escapeHtml(__('Back')) ?></span></a></div>
    </div>
</form>
<?php /* Extensions placeholder */ ?>
<?= $block->getChildHtml('customer.form.newsletter.extra2') ?>
<script>
require(["jquery"], function($){
    $(document).ready(function(){
        var ckbox = $('#new_products');
        <?php $data = true; ?>
        $('input[name="newProducts"]').on('click',function () {
            if (ckbox.is(':checked')) {

            }
        });
    });
});

1
  • please accept and upvote answer. So, It's useful for other also. Sep 6, 2018 at 15:53

1 Answer 1

0

Try to use this code :

protected $customerRepositoryInterface;
public function __construct(
    ............................
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ............................
) {
    ............................
    $this->customerRepositoryInterface = $customerRepositoryInterface;
    parent::__construct($context);
    ............................
}
public function execute()
{
    $customer =$this->customerRepositoryInterface->getById($customerID);
    print_r($customer->getCustomAttribute('new_products_attr')->getValue());
}

It maybe helpful for you.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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