Skip to content

Instantly share code, notes, and snippets.

@kohenkatz
Created May 18, 2020 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kohenkatz/b67e9454ce28e39ecc5698ceb7525cb0 to your computer and use it in GitHub Desktop.
Save kohenkatz/b67e9454ce28e39ecc5698ceb7525cb0 to your computer and use it in GitHub Desktop.
Product option prices by customer group OpenCart Extension for 3.x
<modification>
<notes><![CDATA[
This plugin is a conversion of https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=32403
to work with OpenCart 3.x in OCMOD format.
To use it, put this file into a ZIP file named "product-option-prices-by-customer-group.ocmod.zip"
(the important part is the `.ocmod.zip` extensions) and install it in your OpenCart.
YOU WILL ALSO NEED TO RUN THE FOLLOWING DATABASE COMMAND MANUALLY to create the extra column:
"ALTER TABLE product_option_value ADD COLUMN customer_group_id INT NOT NULL DEFAULT 0;"
]]></notes>
<name>Product_Option_By_Customer_Group</name>
<code>Product_Option_By_Customer_Group</code>
<version>2.x-3.x</version>
<author>phpopencart@gmail.com</author>
<description>Allow product options to be set based on customer group</description>
<file path="admin/model/catalog/product.php">
<operation>
<search><![CDATA[$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");]]></search>
<add position="replace" ><![CDATA[$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', customer_group_id = '" . (int)$product_option_value['customer_group_id'] . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");]]></add>
</operation>
<operation>
<search ><![CDATA[$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "', product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");]]></search>
<add position="replace" ><![CDATA[$this->db->query("INSERT INTO `" . DB_PREFIX . "product_option_value` SET `product_option_value_id` = '" . (int)$product_option_value['product_option_value_id'] . "', product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', option_value_id = '" . (int)$product_option_value['option_value_id'] . "', quantity = '" . (int)$product_option_value['quantity'] . "', subtract = '" . (int)$product_option_value['subtract'] . "', price = '" . (float)$product_option_value['price'] . "', price_prefix = '" . $this->db->escape($product_option_value['price_prefix']) . "', customer_group_id = '" . (int)$product_option_value['customer_group_id'] . "', points = '" . (int)$product_option_value['points'] . "', points_prefix = '" . $this->db->escape($product_option_value['points_prefix']) . "', weight = '" . (float)$product_option_value['weight'] . "', weight_prefix = '" . $this->db->escape($product_option_value['weight_prefix']) . "'");]]></add>
</operation>
<operation>
<search ><![CDATA['points_prefix' => $product_option_value['points_prefix'],]]></search>
<add position="after" ><![CDATA[
'customer_group_id' => $product_option_value['customer_group_id'],
]]></add>
</operation>
</file>
<file path="admin/controller/catalog/product.php">
<operation>
<search ><![CDATA['points_prefix' => $product_option_value['points_prefix'],]]></search>
<add position="after" ><![CDATA[
'customer_group_id' => $product_option_value['customer_group_id'],
]]></add>
</operation>
</file>
<file path="admin/view/template/catalog/product_form.twig">
<operation>
<search ><![CDATA[<td class="text-right"><button type="button" onclick="$(this).tooltip('destroy');$('#option-value-row{{ option_value_row }}').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>]]></search>
<add position="before" ><![CDATA[
<td class="text-right">
<select name="product_option[{{ option_row }}][product_option_value][{{ option_value_row }}][customer_group_id]" class="form-control">
{% for customer_group in customer_groups %}
{% if product_option_value.customer_group_id == customer_group.customer_group_id %}
<option value="{{ customer_group.customer_group_id }}" selected="selected">{{ customer_group.name }}</option>
{% else %}
<option value="{{ customer_group.customer_group_id }}">{{ customer_group.name }}</option>
{% endif %}
{% endfor %}
</select>
</td>
]]></add>
</operation>
<operation>
<search index="0"><![CDATA[<td colspan="6"></td>]]></search>
<add position="replace" ><![CDATA[<td colspan="7"></td>]]></add>
</operation>
<operation>
<search><![CDATA[html += ' <td colspan="6"></td>';]]></search>
<add position="replace" ><![CDATA[html += ' <td colspan="7"></td>';]]></add>
</operation>
<operation>
<search ><![CDATA[html += ' <td class="text-left"><button type="button" onclick="$(this).tooltip(\'destroy\');$(\'#option-value-row' + option_value_row + '\').remove();" data-toggle="tooltip" rel="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';]]></search>
<add position="before" ><![CDATA[
html += '<td class="text-right">';
html += '<select name="product_option[' + option_row + '][product_option_value][' + option_value_row + '][customer_group_id]" class="form-control">';
{% for customer_group in customer_groups %}
{% if product_option_value.customer_group_id == customer_group.customer_group_id %}
html += ' <option value="{{ customer_group[customer_group_id] }}" selected="selected">{{ customer_group[name] }}</option>';
{% else %}
html += ' <option value="{{ customer_group[customer_group_id] }}">{{ customer_group[name] }}</option>';
{% endif %}
{% endfor %}
html += '</select>';
html += '</td>';
]]></add>
</operation>
<operation>
<search index="0"><![CDATA[<td class="text-right">{{ entry_weight }}</td>]]></search>
<add position="after" ><![CDATA[<td class="text-center">{{ entry_customer_group }}</td>]]></add>
</operation>
<operation>
<search><![CDATA[html += ' <td class="text-right">{{ entry_weight }}</td>';]]></search>
<add position="after"><![CDATA[html += ' <td class="text-center">{{ entry_customer_group }}</td>';]]></add>
</operation>
</file>
<file path="catalog/model/catalog/product.php">
<operation>
<search ><![CDATA[public function getProductOptions($product_id) {]]></search>
<add position="after" ><![CDATA[
if ($this->customer->isLogged()) {
$customer_group_id = $this->customer->getCustomerGroupId();
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}
]]></add>
</operation>
<operation>
<search ><![CDATA[$product_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_id = '" . (int)$product_id . "' AND pov.product_option_id = '" . (int)$product_option['product_option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order");]]></search>
<add position="after" ><![CDATA[
$product_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_id = '" . (int)$product_id . "' AND pov.customer_group_id = '" . (int)$customer_group_id . "' AND pov.product_option_id = '" . (int)$product_option['product_option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order");
]]></add>
</operation>
<operation>
<search ><![CDATA['price_prefix' => $product_option_value['price_prefix'],]]></search>
<add position="after" ><![CDATA[
'customer_group_id' => $product_option_value['customer_group_id'], 'price_prefix' => $product_option_value['price_prefix'],
]]></add>
</operation>
</file>
<file path="system/library/cart/customer.php">
<operation>
<search ><![CDATA[public function getBalance()]]></search>
<add position="before" ><![CDATA[public function getCustomerGroupId() {
return $this->customer_group_id;
}]]></add>
</operation>
</file>
</modification>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment