0

我有一些使用 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";

感谢您的帮助

4

3 回答 3

8
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 
于 2013-10-10T18:41:49.460 回答
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(); 
于 2013-10-10T18:52:35.033 回答
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(); 
于 2013-10-10T18:35:15.277 回答