我有一些我使用SKU的PHP代码:
$sku = $id
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();
如果$ sku错误(在带有$ _product行的数据库中找不到),我想添加到此代码中,以便能够手动更改它,例如,如果$ sku不存在,请将其更改为$ sku =“ 909010" ;
感谢您的帮助
答案 0 :(得分:6)
function get_product($sku){
$product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if (!$product){
$new_sku = '909010'; // If Real SKU doesn`t exsist, change to '909010'
$product=Mage::getModel('catalog/product')->loadByAttribute('sku',$new_sku);
}
return $product;
}
var_dump(get_product('your-sku')); // Real Sku
答案 1 :(得分:2)
$sku = $id
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
if(!is_object($_product) || !$_product->getId()){
//do your stuff here
}
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();
答案 2 :(得分:1)
$sku = $id
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
$_product = !empty($product) ? $product : Mage::getModel('catalog/product')->loadByAttribute('sku','YOUR-ERROR-SKU');
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();