I want to check if some products are in stock but whatever I do the isInStock() method always returns TRUE. My products are configurable products with no associated products and under the "Inventory" tab "Stock Availability" is set to "Out of Stock".
What am I doing wrong?
Thanks!
Magento has a lot of history at this point, so it's a good idea to not always
trust that method names will do what "seems obvious". Obvious now wasn't obvious a few years ago.
If you look at the following two methods on the Mage_Catalog_Model_Product class
public function isInStock()
{
return $this->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_ENABLED;
}
public function getStatus()
{
return $this->_getData('status');
}
You can see that isInStock checks the status attribute, set in the "General" section of the Product admin.
Try this instead
$stockItem = $product->getStockItem();
if($stockItem->getIsInStock())
{
//in stock!
}
else
{
//not in stock!
}
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
<?php if ((int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()>0) { ?>
<?php } else {} ?>
It worked for the simple product category view.
Related
I've been trying to make posts from a specific category private in wordpress (php) but I cannot seem to make it work.
I've tried this:
function force_type_private($post) {
if (get_the_category($post->ID) == [11]) {
$post['post_status'] = 'private';
}
return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');
and this:
function force_type_private($post) {
if ($post['post_category'] == [11]) {
$post['post_status'] = 'private';
}
return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');
...but it doesn't work. The problem is in the if-statement, without it all posts indeed turn to private.
All my posts have one (and only one) category. The category ID of the category I want to set to private is '11'.
I'm quite new to php. Can anyone tell me what I'm doing wrong?
(PS I know I have to add something to the if-statement like '$post[post_status] != trash' to make sure only the published posts are set to private, but I try to focus on selecting posts from the given category first)
You compare array of objects with array of ints. You should extract ID's of categories:
function force_type_private($post) {
if (in_array(11, array_column(get_the_category($post->ID), 'term_id'))) {
$post['post_status'] = 'private';
}
return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');
I have the following situation, Im trying to modify the price of products displayed in a platform.
Everything works ok for only 1 product (eg: product view) but I dont know what I have to do in order to modify the price of each product in an eloquent collection.
this is the code in my app:
ProductRepository.php:
public function CalcPrice($product){
$x = $product->price; //eg 5
$y = 4;
$amount= $x + $y;
return $amount;
}
For the details view of each product inside ProductController I have the following code and everything works perfect:
public function details($id){
$product = $this->product->getProductById($id);
$productprice = $this->product->getCalcPrice($product = $product);
return view('products.view',compact('product','productprice'))
}
On the other hand, my idea is to use the code contained in ProductRepository.php function CalcPrice in a collection.
My main doubt is what do I have to do, because in a collection probably I can have a variable $category in order to retrieve all products in a category, but I will not have a variable for each $product (for eg: a $productid like in details).
What can I do in order to eg:
modify each product price contained in a collection of a category
using CalcPrice function code?
eg: of code:
productrepository.php
public function AllProductsInCategory($catid)
{
return App\Product::where('categoryid', $catid)
->get();
}
but each product displaying their ($product->price + 4) as CalcPrice performs. thanks!.
You can achieve this by defining an attribute accessor on model and append it to model. This way it would be available for you on each instance like its other attributes.
As Taylor Otwell mentioned here, "This is intentional and for performance reasons." However there is an easy way to achieve this, say you have model named Product;
class Product extends Eloquent {
protected $appends = array('calc_price');
public function getCalcPriceAttribute()
{
//if you want to call your method for some reason
return $this->getCalcPrice($this);
// Otherwise more clean way would be something like this
// return $this->price + 4 // un-comment if you don't want to call getCalcPrice() method
}
}
Now you can access calculated price on each $product by simply calling $product->calc_price.
On my product.tpl in Opencart, I am trying to display a "Warranty Included" icon.
My problem is, every product on my store doesnt have a Warranty, only specific Products do. On each of my products I have a whole list of Attributes (Specifications). I was wondering if there was some PHP code that I could wrap around the image that I would be able to use an IF statement to say something like this:
Single Statement:
<?php if (products attributes "Product type = Computer") {
//Do this
} ?>
Multiple Statement:
<?php if (products attributes "Product type = Computer" & "Screen Size = 56") {
//Do this
} ?>
I know thats not the code above, but I was wondering if there was away to use PHP if statements with Opencart Attributes.
Thanks in Advance!!
Method 1 : (Dirty method)
if ($attribute_groups) {
foreach ($attribute_groups as $attribute_group) {
foreach ($attribute_group['attribute'] as $attribute) {
if($attribute['name']=='warranty' && $attribute['text']=="yes")
{
// display image
// stop the loop if you don't need it further
}
}
}
}
Method 2 : create a function in model/catalog/product.php like checkProductWarranty() which may return a Boolean value
In this method basically you need to join tables product_attribute and attribute_description on attribute_id put name="warranty" in where clause and do your thing
for reference you can see public function getProductAttributes($product_id) in the same model file
Method 1 works but it will loop through all attributes which might not be efficient thing to do
You can add product to compare. I have to show the link "Add to Compare" if the product is not added already otherwise show "Compare". I have to check if the product is in comparison list.
I have list.phtml file.
I tried this but this gives all the products added in comparison list.
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()
I can loop through the returned products and can check if the product is in this collection but I am looking for a single call which take the product id or sku and return true or false accordingly.
I also added the filter like this but does not work
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()
->addAttributeToFilter('sku', $item->getSku());
Try to use
Mage_Catalog_Model_Product_Compare_List
and its method:
getItemCollection
Like this:
$collection = Mage::getModel('catalog/product_compare_list')->getItemCollection();
$collection->.....Additional filters go here.
Why helper didn't worked? Because collection is already loaded there:
v 1.6
public function getItemCollection()
{
if (!$this->_itemCollection) {
$this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
->useProductItem(true)
->setStoreId(Mage::app()->getStore()->getId());
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
} elseif ($this->_customerId) {
$this->_itemCollection->setCustomerId($this->_customerId);
} else {
$this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
}
Mage::getSingleton('catalog/product_visibility')
->addVisibleInSiteFilterToCollection($this->_itemCollection);
/* Price data is added to consider item stock status using price index */
$this->_itemCollection->addPriceData();
$this->_itemCollection->addAttributeToSelect('name')
->addUrlRewrite()
->load();
/* update compare items count */
$this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection));
}
return $this->_itemCollection;
}
So you can load collection by model and filter itself in template or in your own custom helper - model.
In Magento, if you need to get / fetch the Shopping Cart's Item details, you can do it in any of the two possible ways, which will provide you with all the shopped Items in an array:-
$cartItems1 = $cart->getQuote()->getAllItems();
$cartItems2 = $cart->getItems()->getData();
But before using any one of the above two methods, you need to initialize the shopping cart object as:-
$cart = new Mage_Checkout_Model_Cart();
$cart->init();
Can anyone please describe in details as to what the two options provide & their differences between each other, along with their possible usage.
In any more such option is available in Magento, can anyone please highlight it?
If you look at the code of the Cart and Quote classes everything will become clear.
Here's the code for $cart->getItems():
public function getItems()
{
return $this->getQuote()->getAllVisibleItems();
}
Plain and simple - it just calls a method of the Quote object. So the question now is: What is the difference between getAllVisibleItems() and getAllItems()?
Let's look at the code of both methods:
public function getAllItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted()) {
$items[] = $item;
}
}
return $items;
}
public function getAllVisibleItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;
}
}
return $items;
}
The only difference: getAllVisibleItems() has an additional check for each item:
!$item->getParentItemId()
which tests if the product has a parent (in other words, it tests if it is a simple product). So this method's return array will be missing simple products, as opposed to getAllItems().
Are there any other ways to retrieve items?
One would be to directly get the product collection from the quote object:
$productCollection = $cart->getQuote()->getItemsCollection();