Plugin Directory

Changeset 3343431


Ignore:
Timestamp:
08/12/2025 10:06:56 AM (6 months ago)
Author:
rwky
Message:

rewrite creation method for some cases where WC would fatal due to theme/plugin constrictions.

Location:
garantie-sgr/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • garantie-sgr/trunk/garantie-sgr.php

    r3343406 r3343431  
    44 * Plugin URL: https://uprise.ro
    55 * Description: Extensie WooCommerce pentru sistemul garanție-returnare SGR.
    6  * Version: 2.0.1
     6 * Version: 2.0.2
    77 * Author: Eduard V. Doloc
    88 * Author URI: https://uprise.ro
     
    1212 * WC requires at least: 7.9
    1313 * WC tested up to: 10.0
    14  * Stable tag: 2.0.1
     14 * Stable tag: 2.0.2
    1515 * Requires Plugins: woocommerce
    1616 * License: GPLv2 or later
     
    2222
    2323if ( ! defined( 'GARANTIE_SGR_VERSION' ) ) {
    24     define( 'GARANTIE_SGR_VERSION', '2.0.1' );
     24    define( 'GARANTIE_SGR_VERSION', '2.0.2' );
    2525}
    2626
     
    103103
    104104// create the product if it's missing
    105 function garantie_svg_product_create() {
    106     if ( ! class_exists( 'WC_Product_Simple' ) ) {
    107         return false;
    108     }
    109     // safeguard for older wc
    110     if ( ! function_exists( 'wc_get_product_id_by_sku' ) ) {
    111         return false;
    112     }
    113 
    114     $sgr_sku    = 'TAXA-SGR';
    115     $product_id = wc_get_product_id_by_sku( $sgr_sku );
    116 
    117     if ( $product_id ) {
    118         update_option( 'garantie_sgr_product_id', (int) $product_id );
    119 
    120         return (int) $product_id;
    121     }
    122 
    123     $product = new WC_Product_Simple();
    124     $product->set_name( __( 'Taxă SGR', 'garantie-sgr' ) );
    125     $product->set_sku( $sgr_sku );
    126     $product->set_regular_price( '0.50' );
    127     $product->set_price( '0.50' );
    128     $product->set_tax_status( 'none' );
    129     $product->set_description( __( 'Taxă garanție SGR', 'garantie-sgr' ) );
    130     $product->set_catalog_visibility( 'hidden' );
    131     $product->set_status( 'publish' );
    132     $product->set_virtual( true );
    133 
    134     $product_id = $product->save();
    135     if ( $product_id && ! is_wp_error( $product_id ) ) {
    136         // Backdate to Unix epoch start.
    137         wp_update_post( array(
    138             'ID'            => $product_id,
    139             'post_date'     => '1970-01-01 00:00:00',
    140             'post_date_gmt' => '1970-01-01 00:00:00',
    141         ) );
    142         update_option( 'garantie_sgr_product_id', (int) $product_id );
    143 
    144         $attach_id = sgr_image_check( 'produs-sgr.gif' );
     105function garantie_sgr_product_create() {
     106    if ( ! function_exists( 'wp_insert_post' ) ) {
     107        return false;
     108    }
     109
     110    $sgr_sku = 'TAXA-SGR';
     111
     112    if ( function_exists( 'wc_get_product_id_by_sku' ) ) {
     113        $existing_id = wc_get_product_id_by_sku( $sgr_sku );
     114        if ( $existing_id ) {
     115            update_option( 'garantie_sgr_product_id', (int) $existing_id );
     116
     117            return (int) $existing_id;
     118        }
     119    }
     120
     121    $postarr = array(
     122        'post_title'  => __( 'Taxă SGR', 'garantie-sgr' ),
     123        'post_type'   => 'product',
     124        'post_status' => 'publish',
     125        // Ensure product type exists *during* save_post so wc_get_product() works
     126        'tax_input'   => array(
     127            'product_type' => array( 'simple' ),
     128        ),
     129    );
     130
     131    $product_id = wp_insert_post( $postarr, true );
     132    if ( is_wp_error( $product_id ) || ! $product_id ) {
     133        return false;
     134    }
     135
     136    // Core Woo metas (no HTML, so sanitize_text_field is fine for scalar metas)
     137    update_post_meta( $product_id, '_sku', $sgr_sku );
     138    update_post_meta( $product_id, '_price', '0.50' );
     139    update_post_meta( $product_id, '_regular_price', '0.50' );
     140    update_post_meta( $product_id, '_tax_status', 'none' );
     141    update_post_meta( $product_id, '_virtual', 'yes' );
     142    update_post_meta( $product_id, '_manage_stock', 'no' );
     143
     144    // Hide from catalog (product_visibility term)
     145    if ( taxonomy_exists( 'product_visibility' ) ) {
     146        // Don’t touch other terms, just add exclude-from-catalog
     147        wp_set_object_terms( $product_id, 'exclude-from-catalog', 'product_visibility', true );
     148    }
     149
     150    // Backdate to Unix epoch start (creation-only)
     151    wp_update_post( array(
     152        'ID'            => $product_id,
     153        'post_date'     => '1970-01-01 00:00:00',
     154        'post_date_gmt' => '1970-01-01 00:00:00',
     155    ) );
     156
     157    // Thumbnail from bundled image (reuse if exists)
     158    if ( function_exists( 'set_post_thumbnail' ) ) {
     159        $attach_id = function_exists( 'sgr_image_check' ) ? sgr_image_check( 'produs-sgr.jpg' ) : false;
    145160        if ( $attach_id ) {
    146             set_post_thumbnail( $product_id, $attach_id );
    147         }
    148 
    149         return (int) $product_id;
    150     }
    151 
    152     return false;
     161            set_post_thumbnail( $product_id, (int) $attach_id );
     162        }
     163    }
     164
     165    update_option( 'garantie_sgr_product_id', (int) $product_id );
     166
     167    return (int) $product_id;
    153168}
    154169
     
    185200
    186201    if ( $needs ) {
    187         garantie_svg_product_create();
     202        garantie_sgr_product_create();
    188203        update_option( 'garantie_sgr_version', GARANTIE_SGR_VERSION );
    189204        delete_option( 'garantie_sgr_pending_setup' );
  • garantie-sgr/trunk/readme.txt

    r3343423 r3343431  
    88WC requires at least: 7.9
    99WC tested up to: 10.0
    10 Stable tag: 2.0.1
     10Stable tag: 2.0.2
    1111Requires Plugins: woocommerce
    1212License: GPLv2 or later
Note: See TracChangeset for help on using the changeset viewer.