Magento 2 购物车客户自定义选项编辑功能实现问题
Hey there! Let's work through this issue where your Vendor/COptions module can load custom options and selectors, but can't fetch the user's already selected options (leading to $checked being null in your Select.php). Here's how to fix this:
First, in your Vendor/COptions/view/frontend/templates/cart/item/default.phtml, you need to pull the selected custom options directly from the cart item object. Magento stores this data in the product options of the cart item.
Add this code to get the selected options:
// Get the current cart item $cartItem = $block->getItem(); // Fetch all product options associated with the item $productOptions = $cartItem->getProductOptions(); // Extract the selected options into an associative array (option_id => selected_value) $selectedOptions = []; if (!empty($productOptions['options'])) { foreach ($productOptions['options'] as $selectedOption) { $selectedOptions[$selectedOption['option_id']] = $selectedOption['value']; } }
Now, when you render your custom option selectors (calling your Select.php logic), pass the corresponding selected value for each option. For example:
// Loop through your loaded custom options foreach ($customOptions as $option) { // Get the selected value for this option (if exists) $selectedValue = isset($selectedOptions[$option->getId()]) ? $selectedOptions[$option->getId()] : ''; // Render the select HTML with the selected value echo $block->getCustomSelectHtml($option, $selectedValue); }
$checked Variable in Select.php Update your Select.php method to accept the selected value parameter and use it to set the $checked state:
public function getCustomSelectHtml($option, $selectedValue = '') { // Your existing code to build the option HTML $html = ''; foreach ($option->getValues() as $value) { // Determine if this value is the selected one $checked = ($value->getId() == $selectedValue) ? 'checked="checked"' : ''; // Add the checked attribute to the input/option element $html .= '<input type="radio" name="custom_option[' . $option->getId() . ']" value="' . $value->getId() . '" ' . $checked . '>'; $html .= '<label>' . $value->getTitle() . '</label>'; } return $html; }
If you're rewriting the Magento Product Helper in Vendor\COptions\Helper\Rewrite\Product, make sure you're not breaking the default option data fetching. Always call the parent method first before adding custom logic:
class Product extends \Magento\Catalog\Helper\Product { public function getOptions($product) { // Get the original options data from the parent class $options = parent::getOptions($product); // Add your custom logic here (don't modify the core data structure!) return $options; } }
If you're still not seeing data, print the $productOptions array in your template to verify the selected options exist:
echo '<pre>' . print_r($productOptions, true) . '</pre>';
This will show you the exact structure of the data Magento is storing for the cart item's options, so you can adjust your code to match.
内容的提问来源于stack exchange,提问作者user2315153

