Post by ywc6xrlnf58asnha » Mon Dec 04, 2017 11:36 am

I am trying to build a custom shipping method. I need to get the sku of each product in the shopping cart. I am using

Code: Select all

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

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am

Post by straightlight » Tue Dec 05, 2017 12:32 am

Code: Select all

$this->load->model('catalog/product');

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

$product_skus = array();

foreach ($cart_products as $cart_product) {
	$product = $this->model_catalog_product->getProduct($cart_product['product_id']);
	
	if ($product) {
		$product_skus[] = html_entity_decode($product['sku'], ENT_QUOTES, 'UTF-8');
	}
}
If it needs to be outputted to a template, replace:

Code: Select all

$product_skus
with:

Code: Select all

$data['product_skus']
Take note that the product SKU must still exist and being active in the store in order to compare with the added products that have been added to the core to accomplish your request.

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 6:49 am

Thanks for the reply. I found this post viewtopic.php?t=16890&p=82879&f=20#p82879 and it seems to work (though the directory structure is slightly different that specified in the post). Is there a better method to solve this between your suggested and the link? I'm not sure if there is a difference in performance, etc?

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am

Post by straightlight » Mon Dec 11, 2017 6:57 am

The only difference in those directories is the system/library/cart.php which is now the system/library/cart/cart.php file.

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 7:05 am

I understand the directory structure. My question is - is your suggested method or the link a better solution as far as performance etc?

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am

Post by straightlight » Mon Dec 11, 2017 7:07 am

For database performance, the other link would be ideal since you do not need to re-pull the product ID with the SKUs individually.

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:10 am

Can that link be applied to also get a custom attribute? If so, how?
I realize now this is a little off topic from my original question of SKUs. But I still need to get the value of a custom attribute as well as the SKU in the shopping cart.

Edit:
I created a new thread since this is basically a new question. My original question for this thread has been solved. Thanks.

New member

Posts

Joined
Mon Dec 04, 2017 11:32 am
Who is online

Users browsing this forum: No registered users and 152 guests