Post by ywc6xrlnf58asnha » Mon Dec 11, 2017 8:20 am

I am trying to build a custom shipping method. I need to get a custom attribute for some product(s) in the shopping cart. I am using

Code: Select all

$this->cart->getProducts();
to get product info, but it does not include custom attributes. How is this achieved? Thanks.

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am

Post by straightlight » Mon Dec 11, 2017 8:26 am

In system/library/cart/cart.php file,

find:

Code: Select all

$option_price = 0;
add right above:

Code: Select all

$product_attribute_group_data = array();

		$product_attribute_group_query = $this->db->query("SELECT ag.attribute_group_id, agd.name FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_group ag ON (a.attribute_group_id = ag.attribute_group_id) LEFT JOIN " . DB_PREFIX . "attribute_group_description agd ON (ag.attribute_group_id = agd.attribute_group_id) WHERE pa.product_id = '" . (int)$product_query->row['product_id'] . "' AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY ag.attribute_group_id ORDER BY ag.sort_order, agd.name");

		foreach ($product_attribute_group_query->rows as $product_attribute_group) {
			$product_attribute_data = array();

			$product_attribute_query = $this->db->query("SELECT a.attribute_id, ad.name, pa.text FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$product_query->row['product_id'] . "' AND a.attribute_group_id = '" . (int)$product_attribute_group['attribute_group_id'] . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "' AND pa.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY a.sort_order, ad.name");

			foreach ($product_attribute_query->rows as $product_attribute) {
				$product_attribute_data[] = array(
					'attribute_id' => $product_attribute['attribute_id'],
					'name'         => $product_attribute['name'],
					'text'         => $product_attribute['text']
				);
			}

			$product_attribute_group_data[] = array(
				'attribute_group_id' => $product_attribute_group['attribute_group_id'],
				'name'               => $product_attribute_group['name'],
				'attribute'          => $product_attribute_data
			);
		}
Then, find:

Code: Select all

'option'          => $option_data,
add below:

Code: Select all

'attribute'		  => $product_attribute_group_data,
Then, from the controllers or models, you can use this example:

Code: Select all

$cart_products = $this->cart->getProducts();

foreach ($cart_products as $product) {
    if ($product['attribute']) {
        foreach ($product['attribute'] as $attribute) {
           // Your codes here.
        }
    }
}
Last edited by straightlight on Mon Dec 11, 2017 8:38 am, edited 1 time in total.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ywc6xrlnf58asnha » Mon Dec 11, 2017 8:35 am

I get an error in cart.php
<b>Notice</b>: Undefined property: Cart\Cart::$load in <b>/home/<blank>/public_html/system/library/cart/cart.php</b> on line <b>45</b><br />
<b>Fatal error</b>: Uncaught Error: Call to a member function model() on null in /home/<blank>/public_html/system/library/cart/cart.php:45

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am

Post by straightlight » Mon Dec 11, 2017 8:39 am

That's what happens when a team do not reference the Opencart Registry but rather works in redundancy. ;D

Code updated above.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ywc6xrlnf58asnha » Mon Dec 11, 2017 8:44 am

That resolved the error. Thank you

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am

Post by straightlight » Mon Dec 11, 2017 8:46 am

ywc6xrlnf58asnha wrote:
Mon Dec 11, 2017 8:44 am
That resolved the error. Thank you
No problem.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 14 guests