2017-08-24 4 views
1

私はここで非常に奇妙な問題があります。カートページの商品サムネイルとして最初の商品添付ギャラリー画像を使用したいと思います。 だから私はギャラリー添付ファイルIDを取得するにはcart.phpに次のコードを使用します。カートオブジェクト内のWooCommerce変数の商品添付ファイルギャラリーIDを取得

foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 
      $_product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key); 
      $attachment_ids = $_product->get_gallery_attachment_ids(); 

奇妙なことは、それは私のローカルホスト(woocommerceのバージョンは2.6.8である私のテストサイト)に完璧に働いた、です。しかし、それは私のオンラインウェブサイト(woocommerceバージョン3.1.2です)上の変数製品のデータを取得できませんでした。しかし、それは単純な製品の正しいデータを得ることができます。

私はそれでデータを参照するために print_r($_product)を使用し、 WC_Product_Simpleオブジェクトは以下のように、正しいギャラリー画像IDを持っていることを見つける

[gallery_image_ids] => Array ([0] => 1174 [1] => 1175 [2] => 1176) しかしWC_Product_Variationオブジェクトが配列で値を持たない:[gallery_image_ids] => Array ()

私はそれがだと思いますWoocommerceのアップグレードによるものです。私のlocalhostは$ _productのオブジェクト構造とはまったく異なっています。

カートのページで可変商品のギャラリー画像IDを取得する別の方法をご存知ですか?

答えて

1
  1. 方法get_gallery_attachment_ids()が3
  2. WC_Product_variationsがギャラリーに自分自身を持っていけない廃止されており、WooCommerceでget_gallery_image_ids()方法に置き換えられている、それはそれを持っている親WC_Product_Variableです。

だから、それをこのように管理することができます。

$product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key); 
$attachment_ids = $product->get_gallery_image_ids(); 
// For Product variations 
if($product->is_type('variation') && empty($attachment_ids)) { 
    // Get the parent variable product 
    $parent_product = wc_get_product($product->get_parent_id()); 
    $attachment_ids = $parent_product->get_gallery_image_ids(); 
    $image_id = $product->get_image_id(); // The variation main Image ID 
} 
// Testing output 
print_r($attachment_ids); 
関連する問題