เครือข่ายหลายไซต์ของ Woocommerce =› รับเงื่อนไขแอตทริบิวต์ของผลิตภัณฑ์จากรหัสบล็อกอื่น ๆ

เรามีการติดตั้ง Worpdress หลายไซต์พร้อมร้านค้า woocommerce สองแห่ง

ใน blog_id 2 เราจำเป็นต้องได้รับผลิตภัณฑ์บางอย่างที่มีค่าแอตทริบิวต์ (pa_testattr) จาก blog_id 1 มันทำงานได้ดี เราได้ผลิตภัณฑ์ที่ต้องการ ชื่อผลิตภัณฑ์ และรูปภาพปรากฏขึ้นมาจนถึงตอนนี้ แต่เราไม่ได้รับค่าแอตทริบิวต์/ข้อกำหนดจาก pa_testattr กำหนดให้ผลิตภัณฑ์เหล่านี้แสดง

นี่คือรหัสที่เรามี:

switch_to_blog(1); 

$args = array(
    'post_type' => 'product',
    'category' =>  'my_cat',
    'orderby'  => 'name',
    'order'  => 'ASC',
    'posts_per_page' => 10 
);

foreach( wc_get_products($args) as $product ){
    $product_id = $product->get_id();
    // echo  $product_id.'<br>';

// THIS DOES NOT WORK 
foreach( wc_get_product_terms( $product_id, 'pa_testattr' ) as $attribute_value ){
   echo $attribute_value . '<br>';
}
// THIS DOES NOT WORK 


// THIS DOES ALSO NOT WORK  (Output empty: string(0) "")
$myattr = $product->get_attribute( 'pa_testattr' );
var_dump($myattr);
// THIS DOES ALSO NOT WORK  (Output empty: string(0) "")

echo '<p>' . $product->get_name() . '</p>';
echo $product->get_image();


}


restore_current_blog();

ผิดพลาดตรงไหน? เราไม่สามารถเข้าถึงคุณลักษณะจากรหัสบล็อกอื่นได้หรือไม่


person Toni_Nutone    schedule 12.10.2020    source แหล่งที่มา
comment
มันใช้งานได้ตอนนี้ ฉันคิดว่ามีปัญหากับทากคำที่ซ้ำกัน (แต่พวกเขายังคงได้รับมอบหมายให้จัดอนุกรมวิธานที่แตกต่างกัน ... ฮัม?)   -  person Toni_Nutone    schedule 13.10.2020


คำตอบ (1)


ฉันประสบปัญหาเดียวกันแม้ว่าฉันจะเปลี่ยนบล็อกที่ใช้อนุกรมวิธานเพื่อใช้ตัวอย่างของคุณ บล็อก #1 ไม่พร้อมใช้งานในบล็อก #2 วิธีแก้ปัญหาของฉันคือหลอก WordPress ชั่วคราวให้คิดว่ามีอนุกรมวิธานอยู่ ซึ่งจะช่วยให้คุณสามารถเข้าถึงข้อกำหนดได้

switch_to_blog(1);

// get the global taxonomies object...
global $wp_taxonomies;

// trick Wordpress into thinking 'pa_testattr' is a value taxonomy on blogs other than #1
// so we don't get the 'Invalid Taxonomy' error
if (!array_key_exists('pa_testattr', $wp_taxonomies)) {
  register_taxonomy(
    'pa_testattr',
    array(
      'product',
    ),
    array(
      'hierarchical' => true,
    )
  );
  $added_temp_taxonomy = true;
} else {
  $added_temp_taxonomy = false;
}

// get terms, for example
$tags = get_terms( array(
  'taxonomy' => 'pa_testattr',
  'hide_empty' => true,
) );

// do what you need to do with them...

// remove the taxonomy if it was added temporarily 
if ($added_temp_taxonomy) {
  unregister_taxonomy('pa_testattr');
}

restore_current_blog();
person flickeringpixel    schedule 05.02.2021