| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Models; |
| 4 |
|
| 5 |
use StoreEngine\classes\AbstractModel; |
| 6 |
use StoreEngine\Utils\Helper; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @deprecated - Use `Variation` entity class instead. |
| 15 |
*/ |
| 16 |
class Variation extends AbstractModel { |
| 17 |
protected string $table = Helper::DB_PREFIX . 'product_variations'; |
| 18 |
protected string $primary_key = 'id'; |
| 19 |
|
| 20 |
public function get_variant_by_id( $variation_id ) { |
| 21 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- table is hardcoded. |
| 22 |
return $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $variation_id ) ); |
| 23 |
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- table is hardcoded. |
| 24 |
} |
| 25 |
|
| 26 |
public function get_product_variants( $product_id ) { |
| 27 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- table is hardcoded. |
| 28 |
return $this->wpdb->get_results( $this->wpdb->prepare( "SELECT * FROM {$this->table} WHERE product_id = %d", $product_id ) ); |
| 29 |
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- table is hardcoded. |
| 30 |
} |
| 31 |
|
| 32 |
public function get_all( $offset = 0, $per_page = 10, $status = 'any', $search = '' ) { |
| 33 |
global $wpdb; |
| 34 |
$query = "SELECT * FROM {$this->table}"; |
| 35 |
|
| 36 |
$query .= $wpdb->prepare( ' WHERE name LIKE %s', '%' . $wpdb->esc_like( $search ) . '%' ); |
| 37 |
$query .= ' ORDER BY id DESC LIMIT %d, %d'; |
| 38 |
|
| 39 |
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- prepared above. |
| 40 |
$variants = $wpdb->get_results( $wpdb->prepare( $query, $offset, $per_page ), ARRAY_A ); |
| 41 |
// phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- prepared above. |
| 42 |
|
| 43 |
return $variants ?? null; |
| 44 |
} |
| 45 |
|
| 46 |
public function get_variants_count() { |
| 47 |
global $wpdb; |
| 48 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table is hardcoded. |
| 49 |
return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$this->table}" ) ); |
| 50 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table is hardcoded. |
| 51 |
} |
| 52 |
|
| 53 |
public function save( array $args = [] ) { |
| 54 |
if ( empty( $args['product_id'] ) || empty( $args['name'] ) ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
$variation_id = $this->wpdb->insert( |
| 59 |
$this->table, |
| 60 |
[ |
| 61 |
'sku' => $args['sku'], |
| 62 |
'product_id' => $args['product_id'], |
| 63 |
'name' => $args['name'], |
| 64 |
'regular_price' => $args['regular_price'], |
| 65 |
'sale_price' => $args['sale_price'], |
| 66 |
'stock' => $args['stock'], |
| 67 |
|
| 68 |
], |
| 69 |
[ |
| 70 |
'%s', |
| 71 |
'%s', |
| 72 |
'%s', |
| 73 |
'%s', |
| 74 |
'%s', |
| 75 |
'%s', |
| 76 |
] |
| 77 |
); |
| 78 |
|
| 79 |
if ( ! $variation_id ) { |
| 80 |
return new WP_Error( 'failed-to-insert', $this->wpdb->last_error, 'storeengine' ); |
| 81 |
} |
| 82 |
|
| 83 |
return $variation_id; |
| 84 |
} |
| 85 |
|
| 86 |
public function update( int $id, array $args ) { |
| 87 |
if ( ! $id ) { |
| 88 |
return new WP_Error( 'failed-to-update', __( 'No ID provided', 'storeengine' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
return (bool) $this->wpdb->update( |
| 92 |
$this->table, |
| 93 |
$args, |
| 94 |
[ 'id' => $id ], |
| 95 |
[ |
| 96 |
'%s', |
| 97 |
'%s', |
| 98 |
'%s', |
| 99 |
'%s', |
| 100 |
'%s', |
| 101 |
'%s', |
| 102 |
], |
| 103 |
[ '%d' ] |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function delete( ?int $id = null ) { |
| 108 |
if ( ! $id ) { |
| 109 |
return new WP_Error( 'failed-to-delete', __( 'No ID provided', 'storeengine' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
return (bool) $this->wpdb->delete( $this->table, [ 'id' => $id ] ); |
| 113 |
} |
| 114 |
} |
| 115 |
|