WooC Commerce thêm thuộc tính vào sản phẩm theo chương trình

Xin chào. Tôi muốn thêm các giá trị mới vào thuộc tính sản phẩm theo cách lập trình, tôi đã thử với update_post_meta nhưng cách đó không đúng. Có giúp được gì không??

Đang xem 2 trả lời - 1 đến 2 [trong tổng số 2]

  • phi

    [@fullsupportedphil]

    Kỹ sư hạnh phúc tự động

    Xin chào @matshop – ý bạn là bạn muốn làm điều này với API REST phải không?

    Nếu vậy, họ có cuộc gọi cụ thể của riêng họ để truy cập và cập nhật

    Hỗ trợ plugin mouli a11n

    [@mouli]

    Đã được một thời gian kể từ khi chúng tôi nhận được phản hồi từ bạn, vì vậy tôi đánh dấu chủ đề này đã được giải quyết. Hy vọng rằng bạn đã có thể giải quyết vấn đề này, nhưng nếu bạn không giải quyết được, vui lòng mở một chủ đề mới và chúng tôi sẽ sẵn lòng trợ giúp

    Trong hướng dẫn này, tôi sẽ hướng dẫn bạn quy trình tạo sản phẩm trong WooC Commerce bằng mã

    Đó là cách chính xác nhất để tạo sản phẩm trong các phiên bản WooC Commerce mới nhất. Vì vậy, hãy quên các chức năng wp_insert_post[]

    $product->set_regular_price[ 500.00 ];
    $product->set_sale_price[ 250.00 ];
    // sale schedule
    $product->set_date_on_sale_from[ '2022-05-01' ];
    $product->set_date_on_sale_to[ '2022-05-31' ];
    0 đi. Có, tôi hoàn toàn nhớ rằng các sản phẩm là loại bài đăng tùy chỉnh của WordPress và giá sản phẩm là meta bài đăng nhưng dù sao thì nó cũng không cho phép chúng tôi sử dụng các chức năng đó vì có nhiều hơn thế một chút và nó có thể gây ra lỗi

    Trong hướng dẫn này, chúng tôi sẽ sử dụng các đối tượng CRUD đã được giới thiệu trong WooC Commerce 3. 0 [CRUD là viết tắt của Tạo, Đọc, Cập nhật, Xóa]. Các đối tượng CRUD chúng tôi quan tâm ngay bây giờ là

    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      1 – đối với các sản phẩm đơn giản,
    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      2 – đối với các sản phẩm bên ngoài,
    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      3 – đối với các sản phẩm được nhóm lại,
    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      4 – biến số

    Chúng tôi cũng sẽ sử dụng nhiều phương pháp của các đối tượng này, cho phép chúng tôi định cấu hình dữ liệu sản phẩm của mình

    Sản phẩm đơn giản

    Hãy bắt đầu với việc tạo ra một sản phẩm đơn giản. Tôi đang đề nghị bạn tưởng tượng, một sản phẩm đơn giản đã chết nên có những gì? . hình ảnh, phải không?

    // that's CRUD object
    $product = new WC_Product_Simple[];
    
    $product->set_name[ 'Wizard Hat' ]; // product title
    
    $product->set_slug[ 'medium-size-wizard-hat-in-new-york' ];
    
    $product->set_regular_price[ 500.00 ]; // in current shop currency
    
    $product->set_short_description[ '

    Here it is.. A WIZARD HAT!

    Only here and now.

    ' ]; // you can also add a full product description // $product->set_description[ 'long description here...' ]; $product->set_image_id[ 90 ]; // let's suppose that our 'Accessories' category has ID = 19 $product->set_category_ids[ array[ 19 ] ]; // you can also use $product->set_tag_ids[] for tags, brands etc $product->save[];

    Đoạn mã trên là đủ để tạo ra một sản phẩm như thế này

    Sản phẩm mới được tạo, chủ đề Mặt tiền cửa hàng

    Ngoài ra còn có các phương pháp mà bạn có thể thấy hữu ích

    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      5 – vượt qua
      $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      6 nếu bạn muốn sản phẩm này được đánh dấu là nổi bật
    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      7 – có thể chuyển nhiều ID hình ảnh dưới dạng một mảng tại đây
    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      8 – đơn đặt hàng sản phẩm thủ công dưới dạng số nguyên
    • $product->set_regular_price[ 500.00 ];
      $product->set_sale_price[ 250.00 ];
      // sale schedule
      $product->set_date_on_sale_from[ '2022-05-01' ];
      $product->set_date_on_sale_to[ '2022-05-31' ];
      9 – bất kỳ trạng thái bài đăng nào ở đây. Không muốn sản phẩm được công bố?
    • $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      1 – tổng doanh số sản phẩm có thể được chuyển vào đây dưới dạng giá trị số nguyên
    • $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      2 – khả năng hiển thị trong danh mục có thể được định cấu hình bằng phương pháp này, các giá trị
      $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      3,
      $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      4,
      $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      5 và
      $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      6 được chấp nhận
    • $product->set_sku[ 'wzrd-hat' ]; // Should be unique
      
      // You do not need it if you manage stock at product level [below]
      $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
      
      // Stock management at product level
      $product->set_manage_stock[ true ];
      $product->set_stock_quantity[ 5 ];
      $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
      $prodict->set_low_stock_amount[ 2 ];
      
      $product->set_sold_individually[ true ];
      7 – bất kỳ siêu dữ liệu sản phẩm nào, hãy chuyển một cặp khóa meta và giá trị meta làm tham số thứ nhất và thứ hai một cách thích hợp

    Sản phẩm đang giảm giá

    Bạn có thể sử dụng nó kết hợp với , nhưng đừng quên thêm các phương thức này trước dòng

    $product->set_sku[ 'wzrd-hat' ]; // Should be unique
    
    // You do not need it if you manage stock at product level [below]
    $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
    
    // Stock management at product level
    $product->set_manage_stock[ true ];
    $product->set_stock_quantity[ 5 ];
    $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
    $prodict->set_low_stock_amount[ 2 ];
    
    $product->set_sold_individually[ true ];
    8

    $product->set_regular_price[ 500.00 ];
    $product->set_sale_price[ 250.00 ];
    // sale schedule
    $product->set_date_on_sale_from[ '2022-05-01' ];
    $product->set_date_on_sale_to[ '2022-05-31' ];

    Giá sản phẩm đã được thay đổi và tùy theo theme bạn đang sử dụng Sale. có thể xuất hiện, nhân tiện, có một hướng dẫn khác về cách xóa hoặc sửa đổi nó

    Cài đặt khoảng không quảng cáo

    $product->set_sku[ 'wzrd-hat' ]; // Should be unique
    
    // You do not need it if you manage stock at product level [below]
    $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
    
    // Stock management at product level
    $product->set_manage_stock[ true ];
    $product->set_stock_quantity[ 5 ];
    $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
    $prodict->set_low_stock_amount[ 2 ];
    
    $product->set_sold_individually[ true ];

    Các thông số trên dẫn đến cấu hình Inventory này

    Kích thước và vận chuyển

    $product->set_weight[ 0.5 ];
    $product->set_length[ 50 ];
    $product->set_width[ 50 ];
    $product->set_height[ 30 ];
    
    $product->set_shipping_class_id[ 'hats-shipping' ];

    Trong trường hợp bạn đang thắc mắc ID lớp vận chuyển là gì và bạn có thể lấy nó ở đâu, tôi khuyên bạn nên xem hướng dẫn này

    Sản phẩm được liên kết

    Có hai phương thức – 

    $product->set_sku[ 'wzrd-hat' ]; // Should be unique
    
    // You do not need it if you manage stock at product level [below]
    $product->set_stock_status[ 'instock' ]; // 'instock', 'outofstock' or 'onbackorder'
    
    // Stock management at product level
    $product->set_manage_stock[ true ];
    $product->set_stock_quantity[ 5 ];
    $product->set_backorders[ 'no' ]; // 'yes', 'no' or 'notify'
    $prodict->set_low_stock_amount[ 2 ];
    
    $product->set_sold_individually[ true ];
    9 và
    $product->set_weight[ 0.5 ];
    $product->set_length[ 50 ];
    $product->set_width[ 50 ];
    $product->set_height[ 30 ];
    
    $product->set_shipping_class_id[ 'hats-shipping' ];
    0, cả hai đều chấp nhận một mảng ID sản phẩm

    $product->set_upsell_ids[ array[ 15, 17 ] ];
    
    // we can do the same for cross-sells
    // $product->set_cross_sell_ids[ array[ 15, 17, 19, 210 ] ];

    Thuộc tính

    Điều này sẽ rất thú vị

    Trước hết, tôi muốn nhắc bạn rằng có hai loại thuộc tính sản phẩm trong WooC Commerce – thuộc tính dựa trên phân loại được xác định trước [có thể được tạo trong Sản phẩm> Thuộc tính] và thuộc tính sản phẩm riêng lẻ

    Thứ hai, quên đi

    $product->set_weight[ 0.5 ];
    $product->set_length[ 50 ];
    $product->set_width[ 50 ];
    $product->set_height[ 30 ];
    
    $product->set_shipping_class_id[ 'hats-shipping' ];
    1 ở đây

    Bất kể bạn định thêm loại thuộc tính nào vào sản phẩm, bạn phải sử dụng một phương pháp duy nhất là

    $product->set_weight[ 0.5 ];
    $product->set_length[ 50 ];
    $product->set_width[ 50 ];
    $product->set_height[ 30 ];
    
    $product->set_shipping_class_id[ 'hats-shipping' ];
    2. Nhưng những gì chúng ta nên chuyển vào nó?

    // that's going to be an array of attributes we add to a product programmatically
    $attributes = array[];
    
    // add the first attribute
    $attribute = new WC_Product_Attribute[];
    $attribute->set_name[ 'Magical' ];
    $attribute->set_options[ array[ 'Yes', 'No' ] ];
    $attribute->set_position[ 0 ];
    $attribute->set_visible[ true ];
    $attribute->set_variation[ true ];
    $attributes[] = $attribute;
    
    // add the second attribute, it is predefined taxonomy-based attribute
    $attribute = new WC_Product_Attribute[];
    $attribute->set_id[ wc_attribute_taxonomy_id_by_name[ 'pa_color' ] ];
    $attribute->set_name[ 'pa_color' ];
    $attribute->set_options[ array[ 29, 31 ] ];
    $attribute->set_position[ 1 ];
    $attribute->set_visible[ true ];
    $attribute->set_variation[ false ];
    $attributes[] = $attribute;
    
    $product->set_attributes[ $attributes ];

    Khi tạo thuộc tính bạn phải nhớ hai điều

    • Sự khác biệt giữa việc tạo các thuộc tính tùy chỉnh hoặc dựa trên phân loại nằm ở những gì bạn chuyển vào các phương thức
      $product->set_weight[ 0.5 ];
      $product->set_length[ 50 ];
      $product->set_width[ 50 ];
      $product->set_height[ 30 ];
      
      $product->set_shipping_class_id[ 'hats-shipping' ];
      4 và
      $product->set_weight[ 0.5 ];
      $product->set_length[ 50 ];
      $product->set_width[ 50 ];
      $product->set_height[ 30 ];
      
      $product->set_shipping_class_id[ 'hats-shipping' ];
      5. Nó phải là một tên phân loại thuộc tính bên trong hàm
      $product->set_weight[ 0.5 ];
      $product->set_length[ 50 ];
      $product->set_width[ 50 ];
      $product->set_height[ 30 ];
      
      $product->set_shipping_class_id[ 'hats-shipping' ];
      6 và chỉ là một tên phân loại phù hợp
    • Khi bạn chuyển các ID thuật ngữ trong hàm
      $product->set_weight[ 0.5 ];
      $product->set_length[ 50 ];
      $product->set_width[ 50 ];
      $product->set_height[ 30 ];
      
      $product->set_shipping_class_id[ 'hats-shipping' ];
      7, bạn không nên làm điều đó quá sớm trong mã khi chưa đăng ký phân loại. Nó cũng áp dụng cho danh mục sản phẩm và thẻ

    Sản phẩm ảo và có thể tải xuống

    Giả sử rằng Mũ phù thủy của chúng tôi chỉ là một hình minh họa mà chúng tôi sẽ mua hoặc một phụ kiện trong một trò chơi như Lineage II. Làm thế nào để làm cho nó ảo và có thể tải xuống?

    Nó giống như cách chúng tôi tạo ra nó

    // Virtual product : YES
    $product->set_virtual[ true ];
    // Downloadable product : YES
    $product->set_downloadable[ true ];
    
    $downloads = array[];
    
    // Creating a download with.. yes, WC_Product_Download class
    $download = new WC_Product_Download[];
    $file_url = wp_get_attachment_url[ $attachment_id ]; // attachmend ID should be here
    $download->set_name[ 'wizard-hat-illustration' ];
    $download->set_id[ md5[ $file_url ] ];
    $download->set_file[ $file_url ];
    
    $downloads[] = $download;
    
    $product->set_downloads[ $downloads ];
    
    $product->set_download_limit[ 1 ]; // can be downloaded only once
    $product->set_download_expiry[ 7 ]; // expires in a week

    Các sản phẩm bên ngoài và được nhóm

    Tạo cả sản phẩm bên ngoài và sản phẩm được nhóm theo chương trình khá đơn giản. Nếu bạn đọc kỹ thì sẽ không có vấn đề gì với nó cả

    Trước hết, bạn phải chọn một sản phẩm phù hợp cấp PHP, đó là

    $product->set_regular_price[ 500.00 ];
    $product->set_sale_price[ 250.00 ];
    // sale schedule
    $product->set_date_on_sale_from[ '2022-05-01' ];
    $product->set_date_on_sale_to[ '2022-05-31' ];
    2 cho các sản phẩm bên ngoài và
    $product->set_regular_price[ 500.00 ];
    $product->set_sale_price[ 250.00 ];
    // sale schedule
    $product->set_date_on_sale_from[ '2022-05-01' ];
    $product->set_date_on_sale_to[ '2022-05-31' ];
    3 cho các sản phẩm được nhóm

    Sau đó, bạn phải sử dụng các phương pháp

    $product->set_upsell_ids[ array[ 15, 17 ] ];
    
    // we can do the same for cross-sells
    // $product->set_cross_sell_ids[ array[ 15, 17, 19, 210 ] ];
    0 và
    $product->set_upsell_ids[ array[ 15, 17 ] ];
    
    // we can do the same for cross-sells
    // $product->set_cross_sell_ids[ array[ 15, 17, 19, 210 ] ];
    1 để thêm liên kết đến trang web đối tác nơi sản phẩm bên ngoài này thực sự được liệt kê. Đối với các sản phẩm được nhóm, bạn chỉ phải sử dụng phương pháp
    $product->set_upsell_ids[ array[ 15, 17 ] ];
    
    // we can do the same for cross-sells
    // $product->set_cross_sell_ids[ array[ 15, 17, 19, 210 ] ];
    2

    sản phẩm biến

    Chà, nó sẽ thú vị trở lại đây. Nhưng như mọi khi, chúng tôi sẽ làm cho nó đơn giản nhất có thể

    // Creating a variable product
    $product = new WC_Product_Variable[];
    
    // Name and image would be enough
    $product->set_name[ 'Wizard Hat' ];
    $product->set_image_id[ 90 ];
    
    // one available for variation attribute
    $attribute = new WC_Product_Attribute[];
    $attribute->set_name[ 'Magical' ];
    $attribute->set_options[ array[ 'Yes', 'No' ] ];
    $attribute->set_position[ 0 ];
    $attribute->set_visible[ true ];
    $attribute->set_variation[ true ]; // here it is
    	
    $product->set_attributes[ array[ $attribute ] ];
    
    // save the changes and go on
    $product->save[];
    
    // now we need two variations for Magical and Non-magical Wizard hat
    $variation = new WC_Product_Variation[];
    $variation->set_parent_id[ $product->get_id[] ];
    $variation->set_attributes[ array[ 'magical' => 'Yes' ] ];
    $variation->set_regular_price[ 1000000 ]; // yep, magic hat is quite expensive
    $variation->save[];
    
    $variation = new WC_Product_Variation[];
    $variation->set_parent_id[ $product->get_id[] ];
    $variation->set_attributes[ array[ 'magical' => 'No' ] ];
    $variation->set_regular_price[ 500 ];
    $variation->save[];

    Tất nhiên, lớp

    $product->set_upsell_ids[ array[ 15, 17 ] ];
    
    // we can do the same for cross-sells
    // $product->set_cross_sell_ids[ array[ 15, 17, 19, 210 ] ];
    3 có nhiều phương thức hơn, gần giống như lớp
    $product->set_regular_price[ 500.00 ];
    $product->set_sale_price[ 250.00 ];
    // sale schedule
    $product->set_date_on_sale_from[ '2022-05-01' ];
    $product->set_date_on_sale_to[ '2022-05-31' ];
    1, bạn có thể tìm thấy tất cả chúng trong tài liệu chính thức của WooC Commerce

    Nhưng hiện tại – chúng tôi có sản phẩm biến đổi đơn giản này trong cửa hàng của chúng tôi

    Gần như quên mất, bạn có thể đặt biến thể mặc định bằng phương thức

    $product->set_upsell_ids[ array[ 15, 17 ] ];
    
    // we can do the same for cross-sells
    // $product->set_cross_sell_ids[ array[ 15, 17, 19, 210 ] ];
    5

    $product->set_default_attributes[ array[ 'magical' => 'No' ] ];

    # WooC Commerce, Sản phẩm

    Misha Rudrastyh

    Chào các bạn và chào mừng đến với trang web của tôi. Trong hơn 10 năm, tôi đã cố gắng hết sức để chia sẻ miễn phí với bạn một số hướng dẫn và mẹo tuyệt vời về WordPress

    Làm cách nào để thêm thuộc tính sản phẩm trong WooC Commerce theo lập trình?

    Trước tiên hãy tạo thuộc tính mới trong trang thuộc tính trong bảng quản trị Wordpress. Sau đó tìm tệp có tên hàm. php trong chủ đề Wordpress của bạn và thêm mã của chúng tôi. Thêm các thuộc tính bạn đã tạo vào tệp nếu thích hợp

    Một sản phẩm đơn giản có thể có thuộc tính WooC Commerce không?

    Điều hướng đến WooC Commerce>Cài đặt>Tùy chọn được cải thiện và nhấp vào tab Chung. Đặt tùy chọn Hỗ trợ lựa chọn thuộc tính thành Tất cả sản phẩm [Sản phẩm đơn giản] . Đặt hỗ trợ lựa chọn thuộc tính cho tất cả sản phẩm. Khi tùy chọn này được đặt, các sản phẩm đơn giản của chúng tôi đã sẵn sàng hoạt động.

    Làm cách nào để thêm hình ảnh sản phẩm vào WooC Commerce theo lập trình?

    Thêm hình ảnh thông qua Trang tổng quan WooC Commerce. Thêm một hình ảnh WooC Commerce theo lập trình. .
    Chuyển đến Giao diện> Tùy chỉnh
    Sau đó, WooC Commerce> Hình ảnh sản phẩm
    Viết chiều rộng mong muốn của bạn vào trường “Chiều rộng hình thu nhỏ”
    Đặt chiều cao của hình ảnh trong “Thumbnail Crop”
    Nhấp vào “Xuất bản”

Chủ Đề